C++ Native Functions: com.teracloud.streams.json
- <tuple T> public T extractFromJSON(rstring jsonString, mutable T value)
-
Extract values from JSON string accordingly to a given tuple.
Limitations- Blob, complex, xml, timestamp and decimal type attributes are not supported.
- Collections don't support nesting.
- Optional types are supported for primitive types and list and set of primitive types only.
- Optional bounded types are not supported.
Example
public composite ExtractFromJSON { type Flattened_t = rstring name, int32 age, list< rstring> relatives, rstring street, rstring city; Nested_t = tuple< rstring name, int32 age, list< rstring> relatives> person, tuple< rstring street, rstring city> address; graph () as Test = Custom() { logic onProcess : { rstring jsonString = '{"person" : {"name" : "John", "age" : 42, "relatives" : ["Jane","Mike"]}, "address" : {"street" : "Allen Street", "city" : "New York City"}}'; mutable Flattened_t flattenedTuple = {}; println( extractFromJSON(jsonString, flattenedTuple)); mutable Nested_t nestedTuple = {}; println( extractFromJSON(jsonString, nestedTuple)); } } }- Parameters
-
- jsonString
-
The input JSON string.
- value
-
A mutable tuple to save extracted values.
- Returns
-
Reference to the input tuple.
- public rstring getParseError(JsonParseStatus.status status)
-
Get parse error string.
- Parameters
-
- status
-
a status of the parser to translate to a string.
- Returns
-
Error string.
- <string S, any T> public rstring mapToJSON(map<S, T> m)
-
Convert a map to JSON object encoded as a serialized JSON string. Blob, complex and xml values are converted to nulls. Timestamp is converted to a date string representation.
Example
public composite SPLToJSONSample { type // Provide any hierarchical tuple TestTuple = tuple<rstring str, float64 num, list<int32> nums, map<rstring,timestamp> dates, tuple<boolean flag, enum{YES,NO} answer> additionalTypes>; // Provide a map with string key and any composite value to mapToJSON function TestMap = map<rstring, TestTuple>; graph () as Test = Custom() { logic onProcess : { TestTuple testTuple = {str = "testTuple", num = 42.42, nums = [1,2,3,4], dates = {"date1":getTimestamp()}, additionalTypes = {flag = true, answer = YES}}; TestMap testMap = {"testMap" : testTuple}; printStringLn( mapToJSON(testMap)); } } }- Parameters
-
- m
-
Map containing key-value pairs to be converted to JSON.
- Returns
-
Serialized JSON object containing all name-value pairs in m.
- <string S, any T> public rstring mapToJSON(map<S, T> m, rstring prefixToIgnore)
-
Convert a map to JSON object encoded as a serialized JSON string. Blob, complex and xml values are converted to nulls.
- Parameters
-
- m
-
Map containing key-value pairs to be converted to JSON.
- prefixToIgnore
-
rstring prefix to ignore in attribute name .
- Returns
-
Serialized JSON object containing all name-value pairs in m.
- <enum E> public uint32 parseJSON(rstring jsonString, E jsonIndex)
-
Parse JSON string (used in conjunction with queryJSON function).
Threading limitations
Call to parseJSON should not be placed in param section or state of the operator (internally a json object is shared via the thread local storage).
Example
uint32 rc = parseJSON(jsonString, JsonIndex._1); if (rc != 0) { // queryJSON using JsonIndex._1 }- Parameters
-
- jsonString
-
The input JSON string.
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
Error code (0 - no error).
- <enum E> public boolean parseJSON(rstring jsonString, mutable JsonParseStatus.status status, mutable uint32 offset, E jsonIndex)
-
Parse JSON string (used in conjunction with queryJSON function).
Threading limitations
Call to parseJSON should not be placed in param section or state of the operator (internally a json object is shared via the thread local storage).
Example
mutable uint32 offset = 0u; mutable JsonStatus.status status = JsonStatus.FOUND; boolean success = parseJSON(jsonString, status, offset, JsonIndex._1); if (success) { // queryJSON using JsonIndex._1 }- Parameters
-
- jsonString
-
The input JSON string.
- status
-
indicates a status of the parser (enum JsonParseStatus.status).
- offset
-
returns the offset in JSON string where parse error occured (use when status returns error).
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
True if successfully parsed; false if parsing error. Status and offset will be updated with more info.
- <floatingpoint T, enum E> public T queryJSON(rstring jsonPath, T defaultVal, E jsonIndex)
-
Query JSON object for floating point value with a given path (parseJSON function should be run before).
Example
// parseJson(..., JsonIndex._1) float32 cash = queryJSON("/person/cash", (float32) 10.25, JsonIndex._1);- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <integral T, enum E> public T queryJSON(rstring jsonPath, T defaultVal, E jsonIndex)
-
Query JSON object for integral value with a given path (parseJSON function should be run before).
Example
// parseJson(..., JsonIndex._1) uint8 age = queryJSON("/person/age", (uint8) 55, JsonIndex._1);- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <string T, enum E> public T queryJSON(rstring jsonPath, T defaultVal, E jsonIndex)
-
Query JSON object for string value with a given path (parseJSON function should be run before).
Example
// parseJson(..., JsonIndex._1) queryJSON("address/city", "", JsonIndex._1);- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <floatingpoint T, enum E> public T queryJSON(rstring jsonPath, T defaultVal, mutable JsonStatus.status status, E jsonIndex)
-
Query JSON object for floating point value with a given path (parseJSON function should be run before).
Example
// parseJson(..., JsonIndex._1) mutable JsonStatus.status status = JsonStatus.FOUND; float32 cash = queryJSON("/person/cash", (float32) 10.25, status, JsonIndex._1); // check status- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- status
-
indicates a status of the query (enum JsonStatus.status).
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <integral T, enum E> public T queryJSON(rstring jsonPath, T defaultVal, mutable JsonStatus.status status, E jsonIndex)
-
Query JSON object for integral value with a given path (parseJSON function should be run before).
Example
// parseJson(..., JsonIndex._1) mutable JsonStatus.status status = JsonStatus.FOUND; uint8 age = queryJSON("/person/age", (uint8) 55, status, JsonIndex._1); // check status- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- status
-
indicates a status of the query (enum JsonStatus.status).
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <string T, enum E> public T queryJSON(rstring jsonPath, T defaultVal, mutable JsonStatus.status status, E jsonIndex)
-
Query JSON object for string value with a given path (parseJSON function should be run before).
Example
// parseJson(..., JsonIndex._1) mutable JsonStatus.status status = JsonStatus.FOUND; queryJSON("address/city", "", status, JsonIndex._1); // check status- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- status
-
indicates a status of the query (enum JsonStatus.status).
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <enum E> public boolean queryJSON(rstring jsonPath, boolean defaultVal, E jsonIndex)
-
Query JSON object for boolean value with a given path (parseJSON function should be run before).
Threading limitations*
Call to queryJSON should not be placed in param section or state of the operator (internally a json object is shared via the thread local storage).
Example
// parseJson(..., JsonIndex._1) boolean hasGum = queryJSON("/person/hasGum", false, JsonIndex._1);- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <enum E> public boolean queryJSON(rstring jsonPath, boolean defaultVal, mutable JsonStatus.status status, E jsonIndex)
-
Query JSON object value with a given path (parseJSON function should be run before).
Example
// parseJson(..., JsonIndex._1) mutable JsonStatus.status status = JsonStatus.FOUND; boolean hasGum = queryJSON("/person/hasGum", false, status, JsonIndex._1); // check status- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- status
-
indicates a status of the query (enum JsonStatus.status).
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <floatingpoint T, enum E> public list<T> queryJSON(rstring jsonPath, list<T> defaultVal, E jsonIndex)
-
Query JSON object for list of floating point values with a given path (parseJSON function should be run before).
- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <integral T, enum E> public list<T> queryJSON(rstring jsonPath, list<T> defaultVal, E jsonIndex)
-
Query JSON object for list of integrals with a given path (parseJSON function should be run before).
- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <string T, enum E> public list<T> queryJSON(rstring jsonPath, list<T> defaultVal, E jsonIndex)
-
Query JSON object for list of strings with a given path (parseJSON function should be run before).
- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <floatingpoint T, enum E> public list<T> queryJSON(rstring jsonPath, list<T> defaultVal, mutable JsonStatus.status status, E jsonIndex)
-
Query JSON object for list of floating point values with a given path (parseJSON function should be run before).
- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- status
-
indicates a status of the query (enum JsonStatus.status).
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <integral T, enum E> public list<T> queryJSON(rstring jsonPath, list<T> defaultVal, mutable JsonStatus.status status, E jsonIndex)
-
Query JSON object for list of integrals with a given path (parseJSON function should be run before).
- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- status
-
indicates a status of the query (enum JsonStatus.status).
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <string T, enum E> public list<T> queryJSON(rstring jsonPath, list<T> defaultVal, mutable JsonStatus.status status, E jsonIndex)
-
Query JSON object for list of strings with a given path (parseJSON function should be run before).
- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- status
-
indicates a status of the query (enum JsonStatus.status).
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <enum E> public list<boolean> queryJSON(rstring jsonPath, list<boolean> defaultVal, E jsonIndex)
-
Query JSON object for list of booleans with a given path (parseJSON function should be run before).
- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <enum E> public list<boolean> queryJSON(rstring jsonPath, list<boolean> defaultVal, mutable JsonStatus.status status, E jsonIndex)
-
Query JSON object for list of booleans with a given path (parseJSON function should be run before).
- Parameters
-
- jsonPath
-
Path to a JSON attribute.
- defaultVal
-
Default value to apply when an attribute not found.
- status
-
indicates a status of the query (enum JsonStatus.status).
- jsonIndex
-
Json index of enum type (e.g. enum{_1}).
- Returns
-
JSON value.
- <string S, any T> public rstring toJSON(S key, T value)
-
Convert a value to JSON object with a single key encoded as a serialized JSON string. Blob, complex and xml values are converted to nulls. An input value of type optional being null will generate also null in JSON.
Example
public composite SPLToJSONSample { type // Provide any hierarchical tuple TestTuple = tuple<rstring str, float64 num, list<int32> nums, map<rstring,timestamp> dates, tuple<boolean flag, enum{YES,NO} answer> additionalTypes>; // Provide a string key and any SPL value to toJSON function TestList = list<TestTuple>; graph () as Test = Custom() { logic onProcess : { TestTuple testTuple = {str = "testTuple", num = 42.42, nums = [1,2,3,4], dates = {"date1":getTimestamp()}, additionalTypes = {flag = true, answer = YES}}; TestList testList = [testTuple]; printStringLn( toJSON("testList", testList)); } } }- Parameters
-
- key
-
Key for name-value pair to be converted to JSON.
- value
-
Value for key.
- Returns
-
Serialized JSON object containing single name-value pair.
- <string S, any T> public rstring toJSON(S key, T value, rstring prefixToIgnore)
-
Convert a value to JSON object with a single key encoded as a serialized JSON string. Blob, complex and xml values are converted to nulls.
- Parameters
-
- key
-
Key for name-value pair to be converted to JSON.
- value
-
Value for key.
- prefixToIgnore
-
rstring prefix to ignore in attribute name .
- Returns
-
Serialized JSON object containing single name-value pair.
- <tuple T> public rstring tupleToJSON(T t)
-
Convert a tuple to JSON object encoded as a serialized JSON String.
Blob, complex and xml values are converted to nulls. Timestamp is converted to a date string representation. Optional attributes having null value are converted to null in JSON.
Example
public composite SPLToJSONSample { type // Provide any hierarchical tuple to tupleToJSON function TestTuple = tuple<rstring str, float64 num, list<int32> nums, map<rstring,timestamp> dates, tuple<boolean flag, enum{YES,NO} answer> additionalTypes>; graph () as Test = Custom() { logic onProcess : { TestTuple testTuple = {str = "testTuple", num = 42.42, nums = [1,2,3,4], dates = {"date1":getTimestamp()}, additionalTypes = {flag = true, answer = YES}}; printStringLn( tupleToJSON(testTuple)); } } }- Parameters
-
- t
-
Tuple to be converted to JSON.
- Returns
-
Tuple encoded as a serialized JSON object.
- <tuple T> public rstring tupleToJSON(T t, rstring prefixToIgnore)
-
Convert a tuple to JSON object encoded as a serialized JSON String. Blob, complex and xml values are converted to nulls.
- Parameters
-
- t
-
Tuple to be converted to JSON.
- prefixToIgnore
-
rstring prefix to ignore in attribute name .
- Returns
-
Tuple encoded as a serialized JSON object.