5 static JST_Error save_element(
const JST_Element * elt, FILE * stream,
unsigned left_margin,
unsigned indent );
7 static JST_Error save_object(
const JST_Object *
object, FILE * stream,
unsigned left_margin,
unsigned indent ) {
9 fprintf( stream,
"%*s\"%s\": ", left_margin,
"", iter->name );
10 save_element( &(iter->element), stream, left_margin, indent );
11 fprintf( stream,
"%s", ( iter->next == NULL ) ?
"\n" :
",\n" );
16 static JST_Error save_array(
const JST_Array * array, FILE * stream,
unsigned left_margin,
unsigned indent ) {
17 for(
unsigned i = 0; i < array->
count; ++i ) {
18 fprintf( stream,
"%*s", left_margin,
"" );
19 save_element( &(array->
items[i]->
element), stream, left_margin, indent );
20 fprintf( stream,
"%s", ( i < array->count - 1 ) ?
",\n" :
"\n" );
25 static JST_Error save_element(
const JST_Element * elt, FILE * stream,
unsigned left_margin,
unsigned indent ) {
28 fprintf( stream,
"{\n" );
29 save_object( &(elt->
value.
object), stream, left_margin+indent, indent );
30 fprintf( stream,
"%*s}", left_margin,
"" );
33 fprintf( stream,
"[\n" );
34 save_array ( &(elt->
value.
array ), stream, left_margin+indent, indent );
35 fprintf( stream,
"%*s]", left_margin,
"" );
41 case JST_NULL : fprintf( stream,
"null" );
break;
48 if(( stream == NULL )||( root == NULL )) {
51 save_element( root, stream, 0, indent );
52 fprintf( stream,
"\n" );
57 if(( path == NULL )||( root == NULL )) {
60 FILE * stream = fopen( path,
"wt" );
61 if( stream == NULL ) {
71 fprintf( stream,
"\"%s\":", iter->name );
72 save_element_compact( &(iter->element), stream );
81 for(
unsigned i = 0; i < array->
count; ++i ) {
82 save_element_compact( &(array->
items[i]->
element), stream );
83 if( i < array->count - 1 ) {
93 fprintf( stream,
"{" );
94 save_object_compact( &(elt->
value.
object), stream );
95 fprintf( stream,
"}" );
98 fprintf( stream,
"[" );
99 save_array_compact( &(elt->
value.
array), stream );
100 fprintf( stream,
"]" );
106 case JST_NULL : fprintf( stream,
"null" );
break;
113 if(( stream == NULL )||( root == NULL )) {
116 save_element_compact( root, stream );
121 if(( path == NULL )||( root == NULL )) {
124 FILE * stream = fopen( path,
"wt" );
125 if( stream == NULL ) {
JST_Error JST_save_to_stream_compact(FILE *stream, const JST_Element *root)
Save the JSON tree to a stream, as a single line of text, without whitespaces.
A object attribute item has a parent and is a named-typed-value pair.
A JSON object, a sorted set of named-value pairs.
JST_Error JST_save_to_file_compact(const char *path, const JST_Element *root)
Save the JSON tree to a file, as a single line of text, without whitespaces.
struct JST_Pair_ * first
A linked list is mandatory to preserve the properties's order.
An element is a typed value.
JST_Error JST_save_to_stream(FILE *stream, const JST_Element *root, unsigned indent)
Save the JSON tree to a stream, pretty-printed.
struct JST_Pair_ * next
A linked list is mandatory to preserve the properties's order.
struct JST_ArrayItem_ ** items
Array of JST_ArrayItem.
unsigned count
Cardinality of the previous array.
JST_Error JST_save_to_file(const char *path, const JST_Element *root, unsigned indent)
Save the JSON tree to a file, pretty-printed.