Comments (7)
Thanks Michal for explaining it in very detailed way.
This is what exactly I was looking for :)
Thanks,
Sajeesh.
from parson.
I have made a wrapper (maybe there should be public one...) for parson, but I think you will understand how it goes. Showing only the essential stuff. I call objects records in my wrapper. You can treat the object inserted into the array as a parent object and insert another array or object into it. This allows nesting of objects and arrays.
Reading is the other way around :-)
(I have cut and pasted from my code so I hope I did not skip something important)
JSON_Value *root;
JSON_Value *arrayRecord;
json_set_allocation_functions(malloc, free);
root = json_value_init_object();
//initialize object to insert into array
arrayRecord = json_value_init_object();
//insert array
putArray(root, "array"); //calling json_object_dotset_value(object, arrayName, json_value_init_array())
//populate object
putString(arrayRecord, "name", Michal);
putHex(arrayRecord, "offset", 100);
putInt(arrayRecord, "size", 200);
//append object to array (insert as many as you like)
appendArrayRecord(root, "array", arrayRecord); //as shown in my previous post
json_serialize_to_file_pretty(root, filename);
from parson.
I don't think I understand the problem, could you share the code that didn't work?
from parson.
Yes there is:
/**
* adds and records to an array
* void *root : root of the parsed config as returned by initRecord
* const char *arrayName : array name The value name can be hierarchical as a dot separated string.
* void *record : record to add
*/
err_t appendArrayRecord(const JSON_Value *root, const char *arrayName, JSON_Value *record)
{
err_t ret = err_ok;
JSON_Object *object;
JSON_Array *array;
ret = getRootObject(root, &object);
if(ret != err_ok){ goto exit; }
array = json_object_dotget_array(object, arrayName);
if(json_array_append_value(array, record) == JSONFailure)
{
ret = err_writeError;
goto exit;
}
exit:
return ret;
}
static err_t getRootObject(const JSON_Value *root, JSON_Object **object)
{
err_t ret = err_ok;
if(root == NULL)
{
ret = err_parsingError;
goto exit;
}
*object = json_object(root);
if(*object == NULL)
{
ret = err_parsingError;
goto exit;
}
exit:
return ret;
}
Regards
Michal
from parson.
@mrkozmic Is this code in response to my or sajeeshknair's comment?
from parson.
Thanks Michal for your response.
I am new to JSON and I am using parson for my project work.
What I am trying here is to put the JSON Objects in a JSON Array and then it can be used as senml format.
Initially, I have created the JSON Objects:-
json_object_set_string(root_object, "n", "sensor");
json_object_set_number(root_object, "volt", 25);
json_object_set_string(root_object, "unit", "v");
This will form a tuple : {"n": "sensor", "volt": 25, "unit": "v"}
I want to put these tuples in an array, so that these tuples will be stored in the following way
"data" :[
{n": "sensor1", "volt": 25, "unit": "v"},
{n": "sensor2", "volt": 28, "unit": "v"},
{n": "sensor3", "volt": 15, "unit": "v"}
]
Initially, I wrote following code snippet to print the above format using a JSON_Array but couldn't succeed.
Here I converted a JSON_Object to a string and appended that string to an array. Later I converted array back to a string. But Its not serving my purpose, as the output string not in the required format.
What I would like to do is to add JSON_Objects in an array.
void test_json_array(void) {
JSON_Value _root_value = json_value_init_object();
JSON_Value *test_value = json_value_init_array();
JSON_Object *root_object = json_value_get_object(root_value);
/_JSON_Object test_object = json_value_get_object(test_value);/
JSON_Array *jarray = json_value_get_array(test_value);
char *serialized_string = NULL;
char *test_string = NULL;
size_t index;
json_object_set_string(root_object, "n", "sensor");
json_object_set_number(root_object, "volt", 25);
json_object_set_string(root_object, "unit", "v");
serialized_string = json_serialize_to_string_pretty(root_value);
test_value =json_parse_string(serialized_string);
json_array_append_string(jarray, serialized_string);
json_array_append_value(jarray, test_value);
test_value = json_array_get_value(jarray, 0);
test_string = json_serialize_to_string_pretty(test_value);
puts(serialized_string);
puts(test_string);
json_free_serialized_string(serialized_string);
json_free_serialized_string(test_string);
json_value_free(root_value);
json_value_free(test_value);
}
I hope problem is more clear now. Let me know your suggestions on this.
Thanks,
Sajeesh.
from parson.
Hello,
I am reopening this old thread.
`void serialization_example1(void) {
JSON_Value *root = json_value_init_object();
JSON_Value *arrayRecord = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root);
char *buf_ext = NULL;
json_object_dotset_value(root_object, "log_data", json_value_init_array());
json_object_set_string(root_object, "name", "Michal");
json_object_set_number(root_object, "offset", 100);
appendArrayRecord(root, "log_data", arrayRecord);
buf_ext = json_serialize_to_string_pretty(root);
puts(buf_ext);
return;
}
`
this is my code but my output looks like:
{
"log_data": [
{}
],
"name": "Michal",
"offset": 100
}
I would like instead the "name" and "offset" to be nested inside log_data, as the original poster was asking. What am I missing in the code?
Many thanks
from parson.
Related Issues (20)
- undeclared identifier 'SIZE_MAX' HOT 4
- CModule fails on my CentOS device with opennssl error HOT 2
- LLVM clang 4.0.3 compliation - "%g is unsupported" HOT 4
- Excess precision for small floating point numbers. HOT 16
- kgabis.github.com/parson returns 404 HOT 1
- Is it possible to add option to ignoring the last comma? HOT 3
- Build using meson HOT 13
- JSON Merge HOT 1
- Question on errors - Get information about what failed HOT 1
- why not check the "processed" in parson.c
- clublog-users.json.zip HOT 1
- scan-build bugs
- Accepting invalid array HOT 1
- EOF not enforced HOT 1
- Completion of error handling
- Improve exception handling
- Segmentation fault after invoking json_object_clear() and json_object_remove() back to back. HOT 1
- What if enclosingn [] is missing? HOT 3
- sprintf hard-deprecated on MacOS prevents compiling HOT 3
- Validate JSON inputs HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parson.