Replies: 3 comments 2 replies
-
DemosMock DataMock JSON response for r.report {
"location": "nc_spm_08_grass7",
"created": "Fri Dec 6 17:00:21 2013",
"region": {
"north": 279073.97546639,
"south": 113673.97546639,
"east": 798143.31179672,
"west": 595143.31179672,
"sn-res": 200,
"ew-res": 200
},
"mask": null,
"maps": [
{
"name": "South-West Wake county",
"description": "geology derived from vector map",
"layer": "geology_30m",
"type": "raster"
}
],
"category": {
"categories": [
{
"category": 217,
"description": "CZfg",
"sqmi": 27.78,
"acres": 17781.703,
"categories": [
{
"category": 1,
"description": "developed",
"sqmi": 18,
"acres": 17781.703,
}
]
}
],
"total": {
"sqmi": 77.60,
"acres": 49668.182
}
}
} parsonvoid serialization_example(void) {
// Create root object
JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_value);
char *serialized_string = NULL;
json_object_set_string(root_object, "location", "nc_spm_08_grass7");
json_object_set_string(root_object, "created", "Fri Dec 6 17:00:21 2013");
json_object_dotset_number(root_object, "region.north", 279073.97546639);
json_object_dotset_number(root_object, "region.south", 113673.97546639);
// Create array and append map object to array
json_object_set_value(root_object, "maps", json_value_init_array());
maps_arr = json_object_get_array(root_object, "maps");
// Loop the maps used by r.report to create object and add to array
for (i = 0; i < nmaps; i++) {
// Create map object
JSON_Value *root_map_value = json_value_init_object();
JSON_Object *root_map_object = json_value_get_object(root_map_value);
json_object_set_string(root_map_object, "name", "South-West Wake county");
json_object_set_string(root_root_map_objectobject, "description", "geology derived from vector map");
json_object_set_string(root_map_object, "layer", "geology_30m");
json_object_set_string(root_map_object, "type", "raster");
json_array_append_value(maps_arr, root_map_value);
}
serialized_string = json_serialize_to_string_pretty(root_value);
puts(serialized_string);
json_free_serialized_string(serialized_string);
json_value_free(root_value);
} json-c#include <json.h>
void json_c_example(void) {
// Create root object
json_object * jobj = json_object_new_object();
json_object *location_string = json_object_new_string("nc_spm_08_grass7");
json_object_object_add(jobj, "location", location_string);
json_object *created_string = json_object_new_string("Fri Dec 6 17:00:21 2013");
json_object_object_add(jobj, "created", created_string);
// Add region.north
json_object * region_obj = json_object_new_object();
json_object *region_north = json_object_new_double(279073.97546639);
json_object_object_add(region_obj, "north", region_north);
// Add region.south
json_object * region_obj = json_object_new_object();
json_object *region_south = json_object_new_double(113673.97546639);
json_object_object_add(region_obj, "south", region_south);
// Create maps array
json_object *maps_array = json_object_new_array();
for (i = 0; i < nmaps; i++) {
// Create map object
json_object * map_object = json_object_new_object();
// Add key value paris
json_object *map_name_string = json_object_new_string("South-West Wake county");
json_object_object_add(map_object, "name", map_name_string);
json_object *map_desc_string = json_object_new_string("geology derived from vector map");
json_object_object_add(map_object, "description", map_desc_string);
json_object *map_layer_string = json_object_new_string("geology_30m");
json_object_object_add(map_object, "layer", map_layer_string);
json_object *map_type_string = json_object_new_string("raster");
json_object_object_add(map_object, "type", map_type_string);
// Add object to maps array
json_object_array_add(maps_array, map_object);
}
// Add maps array to root object
json_object_object_add(jobj, "maps", maps_array);
// Now printing the json object
printf ("The json object created: %sn",json_object_to_json_string(jobj));
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
The GitHub Insights looks better for json-c and parson than for the other two JSON libraries. parson has one very long file, but readable. json-c is many shorter files, readable, but I have seen assembler in random generation. |
Beta Was this translation helpful? Give feedback.
1 reply
-
parson is the winner! |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In an effort add additional output data formats to modules (e.g., r.report, r.info, etc..) we are discussing adding new library dependencies to aid in JSON and YAML encoding.
Please share your feedback on the following candidate dependencies.
JSON
json-c
https://github.com/json-c/json-c#using
License: GNU General Public License v3.0
Current release: 0.16 (2022-04-14)
JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects. It aims to conform to RFC 8259.
jansson
https://github.com/akheron/jansson
Current release: 2.14 (2021-09-09)
Jansson is a C library for encoding, decoding and manipulating JSON data. Its main features and design principles are:
cJSON
https://github.com/DaveGamble/cJSON
Current release: 1.7.15 (2021-08-25)
cJSON aims to be the dumbest possible parser that you can get your job done with. It's a single file of C, and a single header file.
parson
https://github.com/kgabis/parson/
License: MIT
Current release: 1.5.2 (2023)
Parson is a lightweight json library written in C.
YAML
libyaml
https://github.com/yaml/libyaml
Current release: 1.2.2 (2021-10-01)
LibYAML is a YAML parser and emitter library.
Related Issues and PRs
Candidate Modules
Which modules should have JSON & YAML output support?
General
Raster
3d Raster
Vector
Temporal
Database
Beta Was this translation helpful? Give feedback.
All reactions