INGOR
Loading...
Searching...
No Matches
ytJSON Class Reference

JSON parser, writer and Simple Print API. More...

Public Member Functions

ytObjectytJSON_parse (FILE *fp, int opt)
 Parses JSON format data from file stream.
 
ytObjectytJSON_parseStr (const char *str)
 Parses JSON format data from a string.
 
void ytJSON_printDouble (FILE *fp, double v)
 Prints the double value as a JSON-complied expression.
 
void ytJSON_print (FILE *fp, const ytObject *obj, int opt)
 Prints an ytObject instance in JSON format into the stream.
 
ytJSON_SP * ytJSON_SP_new (FILE *fp, int readable)
 Initializes the Simple Print API of JSON.
 
void ytJSON_SP_delete (ytJSON_SP *work)
 Finishes the outputting using Simple Print API.
 
void ytJSON_SP_beginObject (ytJSON_SP *work)
 Begins a new object block.
 
void ytJSON_SP_endObject (ytJSON_SP *work)
 Ends a object block.
 
void ytJSON_SP_beginArray (ytJSON_SP *work)
 Begins a new array.
 
void ytJSON_SP_endArray (ytJSON_SP *work)
 Ends a definition of array.
 
void ytJSON_SP_putStr (ytJSON_SP *work, const char *name, const char *value)
 Puts a string value with its name.
 
void ytJSON_SP_putInt (ytJSON_SP *work, const char *name, int value)
 Puts an integer value with its name.
 
void ytJSON_SP_putDouble (ytJSON_SP *work, const char *name, double value)
 Puts a double value with its name.
 
void ytJSON_SP_putDoubleArray (ytJSON_SP *work, const char *name, const ytDoubleArray *value)
 Puts a double array value with its name.
 
void ytJSON_SP_putIntArray (ytJSON_SP *work, const char *name, const ytIntArray *value)
 Puts a integer array value with its name.
 
void ytJSON_SP_putStrArray (ytJSON_SP *work, const char *name, const ytStrArray *value)
 Puts a string array value with its name.
 
void ytJSON_SP_putObject (ytJSON_SP *work, const char *name)
 Puts an object with its name.
 
void ytJSON_SP_putArray (ytJSON_SP *work, const char *name)
 Puts an array with its name.
 

Related Symbols

(Note that these are not member symbols.)

#define ytJSON_OPT_HYPHEN_BREAK
 Constant value for the option in ytJSON_parse().
 
#define ytJSON_OPT_FORMAT
 Constant value for the option in ytJSON_print().
 

Detailed Description

JSON parser, writer and Simple Print API.

This provides funtions for reading and outputing with JSON format. For outputing there are two types of interface: one for outputing a ytKeyValues object as JSON, one for outputing without using a ytKeyValues object.

How to use Simple Print API.

This is an example of the code.

{
"name1" : "value1",
"name2" : 10
}

If you want to output the above JSON object, write code as below.

FILE * fp;
// Open a file as fp here.
ytJSON_SP * json = ytJSON_SP_new(fp, 1);
ytJSON_SP_putStr(json, "name1", "value1");
ytJSON_SP_putInt(json, "name2", 10);
void ytJSON_SP_beginObject(ytJSON_SP *work)
Begins a new object block.
Definition ytJSON.c:979
void ytJSON_SP_putInt(ytJSON_SP *work, const char *name, int value)
Puts an integer value with its name.
Definition ytJSON.c:1076
ytJSON_SP * ytJSON_SP_new(FILE *fp, int readable)
Initializes the Simple Print API of JSON.
Definition ytJSON.c:905
void ytJSON_SP_endObject(ytJSON_SP *work)
Ends a object block.
Definition ytJSON.c:998
void ytJSON_SP_putStr(ytJSON_SP *work, const char *name, const char *value)
Puts a string value with its name.
Definition ytJSON.c:1056
void ytJSON_SP_delete(ytJSON_SP *work)
Finishes the outputting using Simple Print API.
Definition ytJSON.c:932

Member Function Documentation

◆ ytJSON_parse()

ytObject * ytJSON_parse ( FILE * fp,
int opt )

Parses JSON format data from file stream.

An object containing pairs of name-values is returned as ytKeyValues instance.

An array is converted to an instance of ytArray.

Any numerical value is converted to a double precision floating point value as an ytValue object with the ytType_DOUBLE type.

A Boolean value is converted to an integer value as an ytValue object with the ytType_INT type.

A null value is return as ytType_NULL value in a ytValue object.

Some optional settings can be accepted. Currently, only ytJSON_OPT_HYPHEN_BREAK is suppoted.

Parameters
fpFILE stream.
opt0 or ytJSON_OPT_HYPHEN_BREAK for enabling stopping parsing if a line with only "-" is found.
Returns
ytObject instance that is either ytKeyValues, ytValue with a ytType_DOUBLE primitive value, ytValue with a ytType_CHAR_P primitive value (string), or ytArray.

◆ ytJSON_parseStr()

ytObject * ytJSON_parseStr ( const char * str)

Parses JSON format data from a string.

See ytJSON_parse() for details.

◆ ytJSON_print()

void ytJSON_print ( FILE * fp,
const ytObject * obj,
int opt )

Prints an ytObject instance in JSON format into the stream.

The object can contain only ytKeyValues, ytType_INT, ytType_DOUBLE, ytType_CHAR_P (string), and ytArray instances.

Numerical values need to be ytType_DOUBLE values. ytType_INT values are regarded as Boolean values.

Parameters
objytObject instance to print.
fpFILE pointer.
opt0 or ytJSON_OPT_FORMAT for printing with human friendly formattings.

◆ ytJSON_printDouble()

void ytJSON_printDouble ( FILE * fp,
double v )

Prints the double value as a JSON-complied expression.

An infinity value is printed as "1e999999" or "-1e999999", and these expression will be parsed as an infinity when parsing.

A NaN value is printed as "null".

Parameters
fpFILE pointer
vdouble value to print.

◆ ytJSON_SP_beginArray()

void ytJSON_SP_beginArray ( ytJSON_SP * work)

Begins a new array.

Users mus call ytJSON_SP_endArray() to tell the end of the array definition. To output elements of the array, simply call put functions such as ytJSON_SP_putStr().

Parameters
workpointer to the ytJSON_SP instance.

◆ ytJSON_SP_beginObject()

void ytJSON_SP_beginObject ( ytJSON_SP * work)

Begins a new object block.

Users must call ytJSON_SP_endObject() to tell the end of object definition. Here an object block refers to an object defined as a pair of braces: { and } in JSON.

Parameters
workpointer to the ytJSON_SP instance.

◆ ytJSON_SP_delete()

void ytJSON_SP_delete ( ytJSON_SP * work)

Finishes the outputting using Simple Print API.

If the number of end functions such as ytJSON_SP_endObject() called is not enough for begin functions, then this automatically ends the particular object or array definition blocks.

Parameters
workpointer to the ytJSON_SP object.

◆ ytJSON_SP_endArray()

void ytJSON_SP_endArray ( ytJSON_SP * work)

Ends a definition of array.

Parameters
workpointer to the ytJSON_SP instance.

◆ ytJSON_SP_endObject()

void ytJSON_SP_endObject ( ytJSON_SP * work)

Ends a object block.

Parameters
workpointer to the ytJSON_SP instance.

◆ ytJSON_SP_new()

ytJSON_SP * ytJSON_SP_new ( FILE * fp,
int readable )

Initializes the Simple Print API of JSON.

Parameters
fpFile pointer to output.
readableSet 1 if indentation is enabled for human readablility. This increases the file size.

◆ ytJSON_SP_putArray()

void ytJSON_SP_putArray ( ytJSON_SP * work,
const char * name )

Puts an array with its name.

This begins a new array block. Therefore, users must close the array definition by ytJSON_SP_endArray() after putting all the elements of the array you want to put.

Parameters
namename of a value to output.

◆ ytJSON_SP_putDouble()

void ytJSON_SP_putDouble ( ytJSON_SP * work,
const char * name,
double value )

Puts a double value with its name.

Parameters
work
name
value

◆ ytJSON_SP_putDoubleArray()

void ytJSON_SP_putDoubleArray ( ytJSON_SP * work,
const char * name,
const ytDoubleArray * value )

Puts a double array value with its name.

Parameters
work
name
value

◆ ytJSON_SP_putInt()

void ytJSON_SP_putInt ( ytJSON_SP * work,
const char * name,
int value )

Puts an integer value with its name.

Parameters
work
name
value

◆ ytJSON_SP_putIntArray()

void ytJSON_SP_putIntArray ( ytJSON_SP * work,
const char * name,
const ytIntArray * value )

Puts a integer array value with its name.

Parameters
work
name
value

◆ ytJSON_SP_putObject()

void ytJSON_SP_putObject ( ytJSON_SP * work,
const char * name )

Puts an object with its name.

This begins a new object block. Therefore, users must close the object definition by ytJSON_SP_endObject() after putting the pairs of names and their values.

Parameters
namename of a value to output.

◆ ytJSON_SP_putStr()

void ytJSON_SP_putStr ( ytJSON_SP * work,
const char * name,
const char * value )

Puts a string value with its name.

Parameters
workpointer to the ytJSON_SP instance.
name

◆ ytJSON_SP_putStrArray()

void ytJSON_SP_putStrArray ( ytJSON_SP * work,
const char * name,
const ytStrArray * value )

Puts a string array value with its name.

Parameters
work
name
value

The documentation for this class was generated from the following file: