Add json parse and stringify function to jerryscript c api (#2243)
JerryScript-DCO-1.0-Signed-off-by: Zsolt Raduska rzsolt@inf.u-szeged.hu
This commit is contained in:
committed by
László Langó
parent
0476523f1a
commit
78bd11e732
@@ -5921,3 +5921,73 @@ jerry_value_t jerry_get_typedarray_buffer (jerry_value_t value,
|
||||
**See also**
|
||||
|
||||
- [jerry_create_typedarray](#jerry_create_typedarray)
|
||||
|
||||
# JSON functions
|
||||
|
||||
## jerry_json_parse
|
||||
|
||||
**Summary**
|
||||
|
||||
Returns the same result as JSON.parse ecmascript function.
|
||||
|
||||
**Prototype**
|
||||
|
||||
```c
|
||||
jerry_value_t jerry_json_parse (const jerry_char_t *string_p, jerry_size_t string_size)
|
||||
```
|
||||
|
||||
- `string_p` - a JSON string
|
||||
- `string_size` - size of the string
|
||||
- return
|
||||
- jerry_value_t containing the same as json.parse()
|
||||
- jerry_value_t containing error massage
|
||||
|
||||
**Example**
|
||||
|
||||
```c
|
||||
{
|
||||
const char *data = "{\"name\": \"John\", \"age\": 5}";
|
||||
jerry_size_t str_length = (jerry_size_t)strlen (data);
|
||||
jerry_value_t parsed_json = jerry_json_parse ((jerry_char_t*)data, str_length);
|
||||
|
||||
// parsed_json now conatins all data stored in data_in_json
|
||||
|
||||
jerry_release_value (parsed_json);
|
||||
}
|
||||
```
|
||||
|
||||
## jerry_stringify
|
||||
|
||||
**Summary**
|
||||
|
||||
Returns the same value as JSON.stringify() ecmascript function.
|
||||
|
||||
**Prototype**
|
||||
|
||||
```c
|
||||
jerry_value_t jerry_json_stringfy (const jerry_value_t object_to_stringify)
|
||||
```
|
||||
|
||||
- `object_to_stringify` - a jerry_value_t object to stringify
|
||||
- return
|
||||
- jerry_value_t containing the same as json.stringify()
|
||||
- jerry_value_t containing error massage
|
||||
|
||||
**Example**
|
||||
|
||||
```c
|
||||
{
|
||||
jerry_value_t obj = jerry_create_object ();
|
||||
jerry_value_t key = jerry_create_string ((const jerry_char_t *) "name");
|
||||
jerry_value_t value = jerry_create_string ((const jerry_char_t *) "John");
|
||||
jerry_set_property (obj, key, value);
|
||||
jerry_value_t stringified = jerry_json_stringfy (obj);
|
||||
|
||||
//stringified now contains a json formated string
|
||||
|
||||
jerry_release_value (obj);
|
||||
jerry_release_value (key);
|
||||
jerry_release_value (value);
|
||||
jerry_release_value (stringified);
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user