json2xml master
C++ header-only json to xml converter
Loading...
Searching...
No Matches
ert::JsonSaxConsumer Class Reference

#include <json2xml.hpp>

Inheritance diagram for ert::JsonSaxConsumer:
Collaboration diagram for ert::JsonSaxConsumer:

Public Member Functions

 JsonSaxConsumer (int tabSpaces=4, char attrPrefix='@')
 
const std::stringstream & getXmlStream () const
 
std::string getXmlString () const
 
const std::string & getError () const
 
bool null () override
 
bool boolean (bool val) override
 
bool number_integer (number_integer_t val) override
 
bool number_unsigned (number_unsigned_t val) override
 
bool number_float (number_float_t, const string_t &s) override
 
bool string (string_t &val) override
 
bool binary (binary_t &) override
 
bool start_object (std::size_t) override
 
bool end_object () override
 
bool start_array (std::size_t) override
 
bool end_array () override
 
bool key (string_t &val) override
 
bool parse_error (std::size_t, const std::string &, const nlohmann::json::exception &ex) override
 

Detailed Description

Json SAX consumer building the XML representation of the parsed json document.

The json layout is the one of python xmltodict, restricted to elements and attributes:

  • The top level json value must be an object: each of its keys is a root element (normally one; several give an XML fragment).
  • A key with an object value is an element: its prefixed keys ('@' by default) with scalar values (string, number, boolean) are the attributes, and the other keys the child elements. Attributes are written first, wherever they are in the object.
  • A key with an array value is a repeated element: one per item (objects, or nulls for empty elements).
  • A key with a null value, or an empty object, is an empty element.

The conversion fails (sax_parse() returns false and getError() tells why) on json parse errors and on what is not supported: text content (an element key with a scalar value, or an array of scalars), nested arrays, attributes at top level, with object/array/null values or repeated, invalid XML names, and characters not allowed in XML 1.0. Attribute values are escaped ('&', '<', '>', '"', tab, line feed and carriage return).

Usage:

if (nlohmann::json::sax_parse(jsonString, &consumer)) xml = consumer.getXmlString();
else error = consumer.getError();

A consumer can be reused: every parse starts from scratch.

Definition at line 84 of file json2xml.hpp.

Constructor & Destructor Documentation

◆ JsonSaxConsumer()

ert::JsonSaxConsumer::JsonSaxConsumer ( int  tabSpaces = 4,
char  attrPrefix = '@' 
)
inline

Default constructor

Parameters
tabSpacesIndentation spaces for xml representation, 4 by default. Must be >= 1 or 4 will be asigned.
attrPrefixYou could set any other value for attribute prefix, '@' by default.

Definition at line 256 of file json2xml.hpp.

Member Function Documentation

◆ binary()

bool ert::JsonSaxConsumer::binary ( binary_t &  )
inlineoverride

Definition at line 316 of file json2xml.hpp.

◆ boolean()

bool ert::JsonSaxConsumer::boolean ( bool  val)
inlineoverride

Definition at line 291 of file json2xml.hpp.

◆ end_array()

bool ert::JsonSaxConsumer::end_array ( )
inlineoverride

Definition at line 364 of file json2xml.hpp.

◆ end_object()

bool ert::JsonSaxConsumer::end_object ( )
inlineoverride

Definition at line 336 of file json2xml.hpp.

◆ getError()

const std::string & ert::JsonSaxConsumer::getError ( ) const
inline

Returns the reason of the last failed conversion (empty on success)

Definition at line 277 of file json2xml.hpp.

◆ getXmlStream()

const std::stringstream & ert::JsonSaxConsumer::getXmlStream ( ) const
inline

Returns the resulting xml stringstream representation (empty on failure)

Definition at line 263 of file json2xml.hpp.

◆ getXmlString()

std::string ert::JsonSaxConsumer::getXmlString ( ) const
inline

Returns the resulting xml string representation (empty on failure)

Definition at line 270 of file json2xml.hpp.

◆ key()

bool ert::JsonSaxConsumer::key ( string_t &  val)
inlineoverride

Definition at line 370 of file json2xml.hpp.

◆ null()

bool ert::JsonSaxConsumer::null ( )
inlineoverride

Definition at line 282 of file json2xml.hpp.

◆ number_float()

bool ert::JsonSaxConsumer::number_float ( number_float_t  ,
const string_t &  s 
)
inlineoverride

Definition at line 306 of file json2xml.hpp.

◆ number_integer()

bool ert::JsonSaxConsumer::number_integer ( number_integer_t  val)
inlineoverride

Definition at line 296 of file json2xml.hpp.

◆ number_unsigned()

bool ert::JsonSaxConsumer::number_unsigned ( number_unsigned_t  val)
inlineoverride

Definition at line 301 of file json2xml.hpp.

◆ parse_error()

bool ert::JsonSaxConsumer::parse_error ( std::size_t  ,
const std::string &  ,
const nlohmann::json::exception &  ex 
)
inlineoverride

Definition at line 385 of file json2xml.hpp.

◆ start_array()

bool ert::JsonSaxConsumer::start_array ( std::size_t  )
inlineoverride

Definition at line 352 of file json2xml.hpp.

◆ start_object()

bool ert::JsonSaxConsumer::start_object ( std::size_t  )
inlineoverride

Definition at line 322 of file json2xml.hpp.

◆ string()

bool ert::JsonSaxConsumer::string ( string_t &  val)
inlineoverride

Definition at line 311 of file json2xml.hpp.


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