summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjstebbins <[email protected]>2015-04-02 15:58:02 +0000
committerjstebbins <[email protected]>2015-04-02 15:58:02 +0000
commit7853e46f29f9379fcfbeda917101819c37f1a7eb (patch)
tree9d810615fe87ff57396743bb9a5c80d1d663ea2b
parent47adcf2e93b19ea525d01d9f9a7a9df32f7fe145 (diff)
libhb: add conversions hb_value_t <-> json
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@7036 b64f7644-9d1e-0410-96f1-a4d463321fa5
-rw-r--r--libhb/hb_dict.c50
-rw-r--r--libhb/hb_dict.h6
2 files changed, 44 insertions, 12 deletions
diff --git a/libhb/hb_dict.c b/libhb/hb_dict.c
index d8bbc8713..c3c17e380 100644
--- a/libhb/hb_dict.c
+++ b/libhb/hb_dict.c
@@ -68,6 +68,29 @@ hb_value_t * hb_value_bool(int value)
return json_boolean(value);
}
+hb_value_t * hb_value_json(const char *json)
+{
+ json_error_t error;
+ hb_value_t *val = json_loads(json, 0, &error);
+ if (val == NULL)
+ {
+ hb_error("hb_value_json: Failed, error %s", error.text);
+ }
+ return val;
+}
+
+hb_value_t * hb_value_read_json(const char *path)
+{
+ json_error_t error;
+ hb_value_t *val = json_load_file(path, 0, &error);
+ if (val == NULL)
+ {
+ hb_error("hb_value_read_json: Failed, path (%s), error %s",
+ path, error.text);
+ }
+ return val;
+}
+
static hb_value_t* xform_null(hb_value_type_t type)
{
switch (type)
@@ -170,10 +193,9 @@ static hb_value_t* xform_string(const hb_value_t *value, hb_value_type_t type)
}
case HB_VALUE_TYPE_BOOL:
{
- if (s != NULL &&
- (!strcasecmp(s, "true") ||
- !strcasecmp(s, "yes") ||
- !strcasecmp(s, "1")))
+ if (!strcasecmp(s, "true") ||
+ !strcasecmp(s, "yes") ||
+ !strcasecmp(s, "1"))
{
return json_true();
}
@@ -181,17 +203,11 @@ static hb_value_t* xform_string(const hb_value_t *value, hb_value_type_t type)
}
case HB_VALUE_TYPE_INT:
{
- json_int_t i = 0;
- if (s != NULL)
- strtoll(s, NULL, 0);
- return json_integer(i);
+ return json_integer(strtoll(s, NULL, 0));
}
case HB_VALUE_TYPE_DOUBLE:
{
- double d = 0.0;
- if (s != NULL)
- d = strtod(s, NULL);
- return json_real(d);
+ return json_real(strtod(s, NULL));
}
case HB_VALUE_TYPE_STRING:
{
@@ -399,6 +415,16 @@ hb_value_get_string_xform(const hb_value_t *value)
return result;
}
+char * hb_value_get_json(hb_value_t *value)
+{
+ return json_dumps(value, JSON_INDENT(4) | JSON_SORT_KEYS);
+}
+
+int hb_value_write_json(hb_value_t *value, const char *path)
+{
+ return json_dump_file(value, path, JSON_INDENT(4) | JSON_SORT_KEYS);
+}
+
void hb_dict_free(hb_dict_t **_dict)
{
hb_value_free(_dict);
diff --git a/libhb/hb_dict.h b/libhb/hb_dict.h
index 7df1a56fa..88b9dea1c 100644
--- a/libhb/hb_dict.h
+++ b/libhb/hb_dict.h
@@ -92,6 +92,8 @@ hb_value_t * hb_value_string(const char *value);
hb_value_t * hb_value_int(json_int_t value);
hb_value_t * hb_value_double(double value);
hb_value_t * hb_value_bool(int value);
+hb_value_t * hb_value_json(const char *json);
+hb_value_t * hb_value_read_json(const char *path);
/* Transform hb_value_t from one type to another */
hb_value_t * hb_value_xform(const hb_value_t *value, int type);
@@ -106,6 +108,10 @@ int hb_value_get_bool(const hb_value_t *value);
/* converts value type and returns an allocated string representation.
* caller must free the returned string */
char * hb_value_get_string_xform(const hb_value_t *value);
+/* converts value to json string */
+char * hb_value_get_json(hb_value_t *value);
+/* write json representation to a file */
+int hb_value_write_json(hb_value_t *value, const char *path);
/* specialized dict functions */
/*