![]() |
xchange v1.0
Structured data exchange for C/C++
|
A set of functions for parsing and creating JSON description of data. More...
Macros | |
#define | __XCHANGE_INTERNAL_API__ |
Use internal definitions. | |
#define | FALSE 0 |
Boolean 'false' in case it isn't already defined. | |
#define | TRUE 1 |
Boolean 'true' in case it isn't already defined. | |
Functions | |
char * | xjsonEscape (const char *src, int maxLength) |
char * | xjsonFieldToIndentedString (int indent, const XField *f) |
char * | xjsonFieldToString (const XField *f) |
int | xjsonGetIndent () |
XField * | xjsonParseField (const char *str, char **tail) |
XStructure * | xjsonParseFile (FILE *fp, size_t length) |
XStructure * | xjsonParsePath (const char *path) |
XStructure * | xjsonParseString (const char *str, char **tail) |
void | xjsonSetErrorStream (FILE *fp) |
void | xjsonSetIndent (int nchars) |
char * | xjsonToString (const XStructure *s) |
char * | xjsonUnescape (const char *str) |
A set of functions for parsing and creating JSON description of data.
char * xjsonEscape | ( | const char * | src, |
int | maxLength | ||
) |
Converts a native string to its JSON representation.
src | Pointer to the native (unescaped) string, which may contain special characters. |
maxLength | The number of characters in the input string if not terminated, or <=0 if always teminated arbitrary length string. |
References x_error().
char * xjsonFieldToIndentedString | ( | int | indent, |
const XField * | f | ||
) |
Converts an XField into its JSON representation, with the specified indentation of white spaces in front of every line. Conversion errors are reported to stderr or the altenate stream set by xjsonSetErrorStream().
indent | Number of white spaces to insert in front of each line. |
f | Pointer to field |
References FALSE, x_error(), xErrorDescription(), and xStringCopyOf().
char * xjsonFieldToString | ( | const XField * | f | ) |
Converts an XField into its JSON representation. Conversion errors are reported to stderr or the altenate stream set by xjsonSetErrorStream().
f | Pointer to field |
References xjsonFieldToIndentedString().
int xjsonGetIndent | ( | ) |
Returns the number of spaces per indentation when emitting JSON formatted output.
XField * xjsonParseField | ( | const char * | str, |
char ** | tail | ||
) |
Parses a JSON field from the given parse position, returning the field's data in the xchange format and updating the parse position. Parse errors are reported to stderr or the alternate stream set by xjsonSetErrorStream().
str | String pointer from which to parse. | |
[out] | tail | Pointer in which to return parse position after. |
References x_error().
XStructure * xjsonParseFile | ( | FILE * | fp, |
size_t | length | ||
) |
Parses a JSON object from the current position in a file, returning the described structured data. Parse errors are reported to stderr or the alternate stream set by xjsonSetErrorStream().
[in] | fp | File pointer, opened with read permission ("r"). |
[in] | length | [bytes] The number of bytes to parse / available, or 0 to read to the end of the file. (In the latter case the file must support fseek() with SEEK_END to automatically determine the length, or else this function will return NULL). |
References x_error().
XStructure * xjsonParsePath | ( | const char * | path | ) |
Parses a JSON object from the beginning of a file, returning the described structured data. Parse errors are reported to stderr or the alternate stream set by xjsonSetErrorStream().
[in] | path | File name/path to parse. |
References x_error(), xIsVerbose(), and xjsonParseFile().
XStructure * xjsonParseString | ( | const char * | str, |
char ** | tail | ||
) |
Parses a JSON object from the given parse position, returning the structured data and updating the parse position. Parse errors are reported to stderr or the alternate stream set by xjsonSetErrorStream().
str | Pointer to string from which to parse JSON | |
[out] | tail | Pointer to return parse position, or NULL if not required. |
References x_error().
void xjsonSetErrorStream | ( | FILE * | fp | ) |
void xjsonSetIndent | ( | int | nchars | ) |
Sets the number of spaces per indentation when emitting JSON formatted output.
nchars | (bytes) the new number of white space character of indentation to use. Negative values map to 0. |
References x_error().
char * xjsonToString | ( | const XStructure * | s | ) |
Converts structured data into its JSON representation. Conversion errors are reported to stderr or the altenate stream set by xjsonSetErrorStream().
s | Pointer to structured data |
References xErrorDescription(), and xStringCopyOf().
char * xjsonUnescape | ( | const char * | str | ) |
Converts a an escaped string in JSON representation to a native string
str | The JSON representation of the string, in which special characters appear in escaped form (without the surrounding double quotes). |
References x_error().