JST: JSON tools  1.0.0
JSON tools dynamic library for reading, manipulating and writing JSON tree
Functions
JST_serialize.c File Reference
#include "JST_string.h"
#include <stdlib.h>
Include dependency graph for JST_serialize.c:

Go to the source code of this file.

Functions

JST_Error JST_serialize (JST_Element *root, char **dest, unsigned indent)
 Serialize the JSON tree to a string, pretty-printed. More...
 
JST_Error JST_serialize_compact (JST_Element *root, char **dest)
 Serialize the JSON tree to a string as a single line of text, without whitespaces. More...
 

Function Documentation

◆ JST_serialize()

JST_Error JST_serialize ( JST_Element root,
char **  dest,
unsigned  indent 
)

Serialize the JSON tree to a string, pretty-printed.

Parameters
filepaththe path of the target file
rootthe root of the tree to save
desta pointer to the char buffer allocated by this function, must be freed by the user
indentthe amout of space to pretty print the tree. When indent is negative, it means the JSON tree is printed with all the nodes on a single line.

Definition at line 112 of file JST_serialize.c.

112  {
113  if(( root == NULL )||( dest == NULL )) {
114  return JST_ERR_NULL_ARGUMENT;
115  }
116  JST_String string = JST_String_Zero;
117  if(( serialize_element( root, &string, 0, indent ) == JST_ERR_NONE )&& JST_String_append_char( &string, '\n' )) {
118  string.buffer[string.length] = '\0';
119  *dest = realloc( string.buffer, string.length + 1 ); // truncate, if necessary
120  if( *dest == NULL ) {
121  JST_String_delete( &string );
122  return JST_ERR_ERRNO;
123  }
124  string.buffer = NULL;
125  }
126  JST_String_delete( &string );
127  return JST_ERR_NONE;
128 }
char * buffer
Definition: JST_string.h:8
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
bool JST_String_append_char(JST_String *string, char c)
Definition: JST_string.c:8
JST_Error JST_String_delete(JST_String *string)
Definition: JST_string.c:45
Function misused.
Definition: jstools.h:110
const JST_String JST_String_Zero
Definition: JST_string.c:6

◆ JST_serialize_compact()

JST_Error JST_serialize_compact ( JST_Element root,
char **  dest 
)

Serialize the JSON tree to a string as a single line of text, without whitespaces.

Parameters
filepaththe path of the target file
rootthe root of the tree to save
desta pointer to the char buffer allocated by this function, must be freed by the user negative, it means the JSON tree is printed with all the nodes on a single line.

Definition at line 227 of file JST_serialize.c.

227  {
228  if(( root == NULL )||( dest == NULL )) {
229  return JST_ERR_NULL_ARGUMENT;
230  }
231  JST_String string = JST_String_Zero;
232  if(( serialize_element_compact( root, &string ) == JST_ERR_NONE )) {
233  string.buffer[string.length] = '\0';
234  *dest = realloc( string.buffer, string.length + 1 ); // truncate, if necessary
235  if( *dest == NULL ) {
236  JST_String_delete( &string );
237  return JST_ERR_ERRNO;
238  }
239  string.buffer = NULL;
240  }
241  JST_String_delete( &string );
242  return JST_ERR_NONE;
243 }
char * buffer
Definition: JST_string.h:8
strerror() or perror() can be used to show the operating system layer error
Definition: jstools.h:112
JST_Error JST_String_delete(JST_String *string)
Definition: JST_string.c:45
Function misused.
Definition: jstools.h:110
const JST_String JST_String_Zero
Definition: JST_string.c:6