xchange v1.0
Structured data exchange for C/C++
|
Macros | |
#define | NULLDEV "/dev/null" |
null device on system | |
#define | XJSON_DEFAULT_INDENT 2 |
Number of characters to indent. | |
Functions | |
char * | xjsonEscape (const char *src, int maxLength) |
char * | xjsonFieldToIndentedString (int indent, const XField *f) |
char * | xjsonFieldToString (const XField *f) |
int | xjsonGetIndent () |
XStructure * | xjsonParseAt (char **src, int *lineNumber) |
XField * | xjsonParseFieldAt (char **src, int *lineNumber) |
XStructure * | xjsonParseFile (FILE *file, size_t length, int *lineNumber) |
XStructure * | xjsonParseFilename (const char *fileName, int *lineNumber) |
void | xjsonSetErrorStream (FILE *fp) |
void | xjsonSetIndent (int nchars) |
char * | xjsonToString (const XStructure *s) |
char * | xjsonUnescape (const char *json) |
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 xSetErrorStream().
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 xSetErrorStream().
f | Pointer to field |
References xjsonFieldToIndentedString().
int xjsonGetIndent | ( | ) |
Returns the number of spaces per indentation when emitting JSON formatted output.
References XJSON_DEFAULT_INDENT.
XStructure * xjsonParseAt | ( | char ** | pos, |
int * | lineNumber | ||
) |
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 xSetErrorStream().
[in,out] | pos | Pointer to current parse position, which will be updated to point to after the last character consumed by the JSON parser. |
[out] | lineNumber | Optional pointer that holds a line number of the parse position, or NULL if not required. Line numbers may be useful to report where the parser run into an error if the parsing failed. Line numbers start at 1, and are counted from the initial parse position. |
References x_error().
XField * xjsonParseFieldAt | ( | char ** | pos, |
int * | lineNumber | ||
) |
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 xSetErrorStream().
[in,out] | pos | Pointer to current parse position, which will be updated to point to after the last character consumed by the JSON parser. |
[out] | lineNumber | Optional pointer that holds a line number of the parse position, or NULL if not required. Line numbers may be useful to report where the parser run into an error if the parsing failed. Line numbers start at 1, and are counted from the initial parse position. |
References x_error().
XStructure * xjsonParseFile | ( | FILE * | fp, |
size_t | length, | ||
int * | lineNumber | ||
) |
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 xSetErrorStream().
[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). |
[out] | lineNumber | Optional pointer that holds a line number of the parse position, or NULL if not required. Line numbers may be useful to report where the parser run into an error if the parsing failed. Line numbers start at 1, and are counted from the initial parse position. |
References x_error().
XStructure * xjsonParseFilename | ( | const char * | fileName, |
int * | lineNumber | ||
) |
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 xSetErrorStream().
[in] | fileName | File name/path to parse. |
[out] | lineNumber | Optional pointer that holds a line number of the parse position, or NULL if not required. Line numbers may be useful to report where the parser run into an error if the parsing failed. Line numbers start at 1, and are counted from the initial parse position. |
References x_error(), xIsVerbose(), and xjsonParseFile().
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 xSetErrorStream().
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().