Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FX::FXJSON Class Reference

The FXJSON serializer loads or saves an FXVariant to a JSON text file. More...

#include <FXJSON.h>

Inheritance diagram for FX::FXJSON:

Public Types

enum  Error {
  ErrOK, ErrSave, ErrLoad, ErrToken,
  ErrColon, ErrComma, ErrBracket, ErrBrace,
  ErrQuotes, ErrQuote, ErrNumber, ErrEnd
enum  Flow { Stream, Compact, Pretty }
enum  Direction { Stop = 0, Save = 1, Load = 2 }

Public Member Functions

 Initialize JSON serializer with no buffer.
 FXJSON (FXchar *buffer, FXuval sz=8192, Direction d=Load)
 Construct JSON serializer with given buffer of size, and open it for direction d.
FXbool open (FXchar *buffer=NULL, FXuval sz=8192, Direction d=Load)
 Open JSON parse buffer with size and direction.
Direction direction () const
 Return direction in effect.
FXuval size () const
 Return size of parse buffer.
FXint getLine () const
 Return current line number.
FXint getColumn () const
 Return current column number.
FXlong getOffset () const
 Return offset from begin of file.
Error load (FXVariant &variant)
 Load a variant from JSON stream. More...
Error save (const FXVariant &variant)
 Save a variant to JSON stream. More...
void setNumericPrecision (FXint p)
 Floating point output precision control. More...
FXint getNumericPrecision () const
void setNumericFormat (FXint f)
 Floating point output format control. More...
FXint getNumericFormat () const
void setOutputFlow (FXint f)
 Change output flow format (Stream, Compact, Pretty). More...
FXint getOutputFlow () const
void setIndentation (FXint d)
 Change indentation level for pretty print flow, the amount of indentation applied for each level.
FXint getIndentation () const
void setLineWrap (FXint w)
 Change column at which lines are wrapped.
FXint getLineWrap () const
void setEscapeMode (FXint e)
 Change string escape mode. More...
FXint getEscapeMode () const
virtual FXival fill (FXival count)
 Read at least count bytes into buffer; return bytes available, or -1 for error.
virtual FXival flush (FXival count)
 Write at least count bytes from buffer; return space available, or -1 for error.
FXbool close ()
 Close stream and delete buffer, if owned.
virtual ~FXJSON ()
 Close JSON stream and clean up.

Static Public Member Functions

static const FXchar * getError (Error err)
 Returns error code for given error.

Protected Attributes

FXchar * begptr
FXchar * endptr
FXchar * wptr
FXchar * rptr
FXchar * sptr
FXlong offset
FXint column
FXint indent
FXint line
Direction dir
FXint token
FXint wrap
FXuchar flow
FXuchar prec
FXuchar fmt
FXuchar esc
FXuchar dent

Detailed Description

The FXJSON serializer loads or saves an FXVariant to a JSON text file.

Since FXVariant can contain an arbitrarily complex data structure, this provides applications with a convenient way to load and save state information in a well-defined and human-readable file format. The base class implements serialization/deserialization to/from an external buffer. Subclasses FXJSONFile and FXJSONString serialize from/to strings and disk- based files, respectively. The new JSON5 standard may also be parsed, allowing for single- and multi-line nested comments to be embedded in the input. Syntax errors in the input cause the parser to return an error, and allow diagnosis of the problem and its location in the file by line number, column number, and byte-offset from the start of the file. When writing a json stream, the generated output may be formatter in different ways. The flow-mode controls the overall layout of the resulting text output; when flow is set to Stream, all output is generated with no formatting to improve human legibility. This is the most space-friendly format possible. If flow is set to Compact, a human readable, compact format, aiming to maximize the amount of information on each line is generated. When flow is set to Pretty, a nicely indented, but extremely airy output results, and the resulting document will contain many, many lines with little data. Numeric values are printed with configurable precision; (default=15 digits which results in minimal information loss for real numbers). For Pretty flow format, output may be indented in multiples of the indent level (default=2). Depending on flow setting, lines may be wrapped at a maximum number of columns (default=80). Output strings containing reserved characters may be escaped; for UTF8 characters, there are 3 options for escaping. When escape mode is 0, UTF8 characters are passed unescaped. In escape mode 1, UTF8 characters are escaped as , and in escape mode 2, UTF8 will be escaed using Unicode escape sequences or (two surrogate-pairs escape codes for code points exceeding 16 bits). The default setting is to allow UTF8 characters in the output, but be aware that such outputs need UTF8-capable viewer software to be rendered properly.

Member Enumeration Documentation

◆ Direction


Not active.


Save to device.

◆ Error


No errors.


Unable to save.


Unable to load.


Illegal token.


Expected colon ':'.


Expected comma ','.


Expected closing bracket.


Expected closing brace.


Expected closing quotes.


Expected closing quote.


Numeric conversion.

Unexpected end of file

◆ Flow


Stream-of-consciousness output.


Compact, human readable output (default)

Pretty printed, indented output

Member Function Documentation

◆ load()

Error FX::FXJSON::load ( FXVariant variant)

Load a variant from JSON stream.

Return false if stream wasn't opened for loading, or syntax error.

◆ save()

Error FX::FXJSON::save ( const FXVariant variant)

Save a variant to JSON stream.

Return false if stream wasn't opened for saving, or disk was full.

◆ setEscapeMode()

void FX::FXJSON::setEscapeMode ( FXint  e)

Change string escape mode.

The escape mode is interpreted as follows:

0 Don't escape unicode in strings; 1 Escape unicode as 2 Escape unicode as or .

Default is to escape control characters only.

◆ setNumericFormat()

void FX::FXJSON::setNumericFormat ( FXint  f)

Floating point output format control.

The format mode is interpreted as follows:

0 No exponent. 1 Exponent. 2 Output exponent when required.

The default mode is 2.

◆ setNumericPrecision()

void FX::FXJSON::setNumericPrecision ( FXint  p)

Floating point output precision control.

This controls the number of significant digits written to the output. The default is 15.

◆ setOutputFlow()

void FX::FXJSON::setOutputFlow ( FXint  f)

Change output flow format (Stream, Compact, Pretty).

Stream is the most compact, but pretty much unreadable by humans; it aims to be compact. Compact is very human-readable while at the same time using minimum number of lines to represent the output. Pretty will print one item on each line, with indentation. It is very easily readable but produces large numbers of text lines.

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

Copyright © 1997-2020 Jeroen van der Zijp