diff --git a/src/modules/jansson/README b/src/modules/jansson/README index 15005d0ee49..815826d24cd 100644 --- a/src/modules/jansson/README +++ b/src/modules/jansson/README @@ -30,23 +30,25 @@ Carsten Bock 3. Functions 3.1. jansson_get(key/path, src, dst) - 3.2. jansson_set(type, key/path, value, result) - 3.3. jansson_append(type, key/path, value, result) - 3.4. jansson_array_size(key/path, src, dst) - 3.5. jansson_xdecode(json, xavp) - 3.6. jansson_xencode(xavp, pv) - 3.7. jansson_get_field(src, field_name, dst) + 3.2. jansson_pv_get(key/path, srcvar, dst) + 3.3. jansson_set(type, key/path, value, result) + 3.4. jansson_append(type, key/path, value, result) + 3.5. jansson_array_size(key/path, src, dst) + 3.6. jansson_xdecode(json, xavp) + 3.7. jansson_xencode(xavp, pv) + 3.8. jansson_get_field(src, field_name, dst) List of Examples 1.1. jansson_get usage - 1.2. jansson_set usage - 1.3. jansson_append usage - 1.4. jansson_array_size usage - 1.5. array concatenation - 1.6. jansson_xdecode usage - 1.7. jansson_xencode usage - 1.8. jansson_get_field usage + 1.2. jansson_pv_get usage + 1.3. jansson_set usage + 1.4. jansson_append usage + 1.5. jansson_array_size usage + 1.6. array concatenation + 1.7. jansson_xdecode usage + 1.8. jansson_xencode usage + 1.9. jansson_get_field usage Chapter 1. Admin Guide @@ -61,12 +63,13 @@ Chapter 1. Admin Guide 3. Functions 3.1. jansson_get(key/path, src, dst) - 3.2. jansson_set(type, key/path, value, result) - 3.3. jansson_append(type, key/path, value, result) - 3.4. jansson_array_size(key/path, src, dst) - 3.5. jansson_xdecode(json, xavp) - 3.6. jansson_xencode(xavp, pv) - 3.7. jansson_get_field(src, field_name, dst) + 3.2. jansson_pv_get(key/path, srcvar, dst) + 3.3. jansson_set(type, key/path, value, result) + 3.4. jansson_append(type, key/path, value, result) + 3.5. jansson_array_size(key/path, src, dst) + 3.6. jansson_xdecode(json, xavp) + 3.7. jansson_xencode(xavp, pv) + 3.8. jansson_get_field(src, field_name, dst) 1. Overview @@ -92,17 +95,19 @@ Chapter 1. Admin Guide 3. Functions 3.1. jansson_get(key/path, src, dst) - 3.2. jansson_set(type, key/path, value, result) - 3.3. jansson_append(type, key/path, value, result) - 3.4. jansson_array_size(key/path, src, dst) - 3.5. jansson_xdecode(json, xavp) - 3.6. jansson_xencode(xavp, pv) - 3.7. jansson_get_field(src, field_name, dst) + 3.2. jansson_pv_get(key/path, srcvar, dst) + 3.3. jansson_set(type, key/path, value, result) + 3.4. jansson_append(type, key/path, value, result) + 3.5. jansson_array_size(key/path, src, dst) + 3.6. jansson_xdecode(json, xavp) + 3.7. jansson_xencode(xavp, pv) + 3.8. jansson_get_field(src, field_name, dst) 3.1. jansson_get(key/path, src, dst) Copy the value at the location 'path' from the json object 'src' and - store it in pvar 'dst'. + store it in pvar 'dst'. The 'src' can be a static string or a dynamic + string with variables. The path string supports dot delimited notation (e.g. foo.bar.baz), array notation (e.g. [0]), or a combination of the two (e.g. @@ -120,13 +125,28 @@ Chapter 1. Admin Guide Example 1.1. jansson_get usage ... -if(!jansson_get("inner.deep.list[3]", $var(myjson), "$var(n)")) { +if(!jansson_get("inner.deep.list[3]", "$var(myjson)", "$var(n)")) { + xlog("L_ERR", "Can't parse json data"); +} +xlog("L_INFO", "foo is $var(n)"); +... +jansson_get("test", "{\"test\":\"abc\",\"idx\":20}", "$var(n)") +... + +3.2. jansson_pv_get(key/path, srcvar, dst) + + Similar to jansson_get(), but the 'srcvar' parameter can be only a + variable name. + + Example 1.2. jansson_pv_get usage +... +if(!jansson_pv_get("inner.deep.list[3]", "$var(myjson)", "$var(n)")) { xlog("L_ERR", "Can't parse json data"); } xlog("L_INFO", "foo is $var(n)"); ... -3.2. jansson_set(type, key/path, value, result) +3.3. jansson_set(type, key/path, value, result) Insert 'value' as 'type' at location 'path' into 'result'. @@ -141,7 +161,7 @@ xlog("L_INFO", "foo is $var(n)"); C-Integer boundaries (e.g. C-type long), then the number can be provided as a string. - Example 1.2. jansson_set usage + Example 1.3. jansson_set usage ... # create a new json object and put a string in it at key "mystr" jansson_set("string", "mystr", "my input string", "$var(myjson)"); @@ -165,7 +185,7 @@ jansson_set("str", "myobj.foo", "baz", "$var(myjson)"); :3.14159, "myobj":{"foo":"baz"}}' ... -3.3. jansson_append(type, key/path, value, result) +3.4. jansson_append(type, key/path, value, result) Like jansson_set but can be used to append to arrays. It can also be used to combine two json objects. @@ -174,7 +194,7 @@ jansson_set("str", "myobj.foo", "baz", "$var(myjson)"); is a key that is shared between the two objects, that value will be overwritten by the new object. - Example 1.3. jansson_append usage + Example 1.4. jansson_append usage ... # create a new json array and append values to it $var(myarray) = '[]'; @@ -199,14 +219,14 @@ jansson_append('obj', "", '{"a":1, "b":100}', "$var(newobj)"); # $var(newobj) == '{"a":1,"b":100","c":3}'; ... -3.4. jansson_array_size(key/path, src, dst) +3.5. jansson_array_size(key/path, src, dst) Puts the size of the array in 'src' at location 'path' into the pvar 'dst'. This is particularly useful for looping through an array. See example. - Example 1.4. jansson_array_size usage + Example 1.5. jansson_array_size usage ... $var(array) = "{\"loopme\":[0,1,2,3,4,5]}"; $var(count) = 0; @@ -218,7 +238,7 @@ while($var(count) < $var(size)) { } ... - Example 1.5. array concatenation + Example 1.6. array concatenation ... $var(count) = 0; $var(appendme) = '[0,1]'; @@ -231,31 +251,31 @@ while($var(count) < $var(appendme_size)) { } ... -3.5. jansson_xdecode(json, xavp) +3.6. jansson_xdecode(json, xavp) Parse a JSON string in 'json' and store the elements in xapv 'xavp'. Top-level JSON must be an object or an array of objects. Nested arrays and objects are not decoded but stored as string. - Example 1.6. jansson_xdecode usage + Example 1.7. jansson_xdecode usage ... jansson_xdecode('{"foo":"bar"}', "js"); xlog("foo is $xavp(js=>foo)"); ... -3.6. jansson_xencode(xavp, pv) +3.7. jansson_xencode(xavp, pv) Encode the items in the xavp 'xavp' as JSON and store the result in a pv. Nested xavps's are not supported. - Example 1.7. jansson_xencode usage + Example 1.8. jansson_xencode usage ... $xavp(a=>foo) = "bar"; jansson_xencode("a", "$var(js)"); # $var(js) = '{"foo":"bar"}' ... -3.7. jansson_get_field(src, field_name, dst) +3.8. jansson_get_field(src, field_name, dst) Copy field 'field_name' from json object 'src' and store it in pvar 'dst'. @@ -264,7 +284,7 @@ jansson_xencode("a", "$var(js)"); now it is just a wrapper around jansson_get, and its functionality is the same. - Example 1.8. jansson_get_field usage + Example 1.9. jansson_get_field usage ... jansson_get_field("{'foo':'bar'}", "foo", "$var(foo)"); xlog("foo is $var(foo)");