TSDuck v3.40-3963
MPEG Transport Stream Toolkit
|
XML-to-JSON converter. More...
#include <tsxmlJSONConverter.h>
Public Member Functions | |
JSONConverter (Report &report=(ts::NullReport::Instance())) | |
Default constructor. | |
virtual | ~JSONConverter () override |
Destructor. | |
size_t | childrenCount () const |
Get the number of children. | |
virtual void | clear () |
Clear the content of the node. | |
virtual Node * | clone () const override |
Clone the content of the node in a dynamically allocated object. | |
json::ValuePtr | convertToJSON (const Document &source, bool force_root=false) const |
Convert an XML document into a JSON object. | |
bool | convertToXML (const json::Value &source, Document &destination, bool auto_validate) const |
Convert a JSON object into an XML document. | |
UString | debug () const |
Build a debug string for the node. | |
size_t | depth () const |
Get the depth of an XML element. | |
Document * | document () |
Get the document into which the node is located. | |
const Document * | document () const |
Get the document into which the node is located. | |
Node * | firstChild () |
Get the first child of a node. | |
const Node * | firstChild () const |
Get the first child of a node. | |
Element * | firstChildElement () |
Get the first child Element of a node. | |
const Element * | firstChildElement () const |
Get the first child Element of a node. | |
bool | hasChildren () const |
Check if the node has children. | |
Element * | initialize (const UString &rootName, const UString &declaration=UString()) |
Initialize the document. | |
Node * | lastChild () |
Get the last child. | |
const Node * | lastChild () const |
Get the last child. | |
size_t | lineNumber () const |
Get the line number in input document. | |
bool | load (const UString &fileName, bool search=true) |
Load and parse an XML file. | |
bool | load (std::istream &strm) |
Load and parse an XML file. | |
void | move (Node *newSibling, bool before) |
Move the node before or after another node, potentially to a new parent. | |
void | moveAfter (Node *newSibling) |
Move the node after another node, potentially to a new parent. | |
void | moveBefore (Node *newSibling) |
Move the node before another node, potentially to a new parent. | |
Node * | nextSibling () |
Get the next sibling node. | |
const Node * | nextSibling () const |
Get the next sibling node. | |
Element * | nextSiblingElement () |
Find the next sibling element. | |
const Element * | nextSiblingElement () const |
Find the next sibling element. | |
virtual UString | oneLiner () const |
Format the value as a one-liner XML text. | |
Node * | parent () |
Get the parent's node. | |
const Node * | parent () const |
Get the parent's node. | |
bool | parse (const UString &text) |
Parse an XML document. | |
bool | parse (const UStringList &lines) |
Parse an XML document. | |
bool | preserveSpace () const |
Check if the node or one of its ancestors has attribute xml:space="preserve". | |
Node * | previousSibling () |
Get the previous sibling node. | |
const Node * | previousSibling () const |
Get the previous sibling node. | |
Element * | previousSiblingElement () |
Find the previous sibling element. | |
const Element * | previousSiblingElement () const |
Find the previous sibling element. | |
virtual void | print (TextFormatter &output, bool keepNodeOpen=false) const override |
Format the node for an output XML document. | |
virtual void | printClose (TextFormatter &output, size_t levels=std::numeric_limits< size_t >::max()) const override |
Print the closing tags for the node. | |
void | removeComments (bool recurse) |
Remove all comments in the XML node. | |
void | reparent (Node *newParent, bool last=true) |
Attach the node to a new parent. | |
Report & | report () const |
Get a reference to the report object for the XML node. | |
bool | ringAlone () const |
Check if the node is alone in its own ring. | |
void | ringInsertAfter (RingNode *o) |
Insert this object in a ring after the specified node. | |
void | ringInsertBefore (RingNode *o) |
Insert this object in a ring before the specified node. | |
template<typename T > | |
T * | ringNext () |
Get the next node in the ring. | |
template<typename T > | |
const T * | ringNext () const |
Get the next node in the ring. | |
template<typename T > | |
T * | ringPrevious () |
Get the previous node in the ring. | |
template<typename T > | |
const T * | ringPrevious () const |
Get the previous node in the ring. | |
void | ringRemove () |
Remove the node from the ring it belongs to and creates its own ring. | |
size_t | ringSize () const |
Count the number of element in the ring. | |
void | ringSwap (RingNode *o) |
Swap this object and another one in their rings. | |
Element * | rootElement () |
Get the root element of the document. | |
const Element * | rootElement () const |
Get the root element of the document. | |
bool | save (const fs::path &fileName, size_t indent=2) |
Save an XML file. | |
void | setTweaks (const Tweaks &tw) |
Set the global XML parsing and formatting tweaks for the document. | |
void | setValue (const UString &value) |
Set the value of the node. | |
virtual bool | stickyOutput () const |
Check if the text shall be stuck to other elements in XML output. | |
virtual UString | toString () const override |
Convert to a string object. | |
virtual const Tweaks & | tweaks () const override |
Get a constant reference to the global XML parsing and formatting tweaks for the document. | |
virtual UString | typeName () const override |
Return a node type name, mainly for debug purpose. | |
bool | validate (const Document &doc) const |
Validate an XML document. | |
const UString & | value () const |
Get the value of the node. | |
Static Public Member Functions | |
static UString | DisplayFileName (const UString &name, bool stdInputIfEmpty=false) |
Get a suitable display name for an XML file name or inline content. | |
static bool | IsInlineXML (const UString &name) |
Check if a "file name" is in fact inline XML content instead of a file name. | |
Static Public Attributes | |
static const UString | HashName |
The string "#name" which is used to hold the name of an XML element in a JSON object. | |
static const UString | HashNodes |
The string "#nodes" which is used to hold the children of an XML element in a JSON object. | |
static const UString | HashUnnamed |
The string "_unnamed" which is used for reverse JSON-toXML conversion for unnamed objects. | |
Protected Member Functions | |
const Element * | findModelElement (const Element *elem, const UString &name) const |
Find a child element by name in an XML model element. | |
Node * | identifyNextNode (TextParser &parser) |
Identify the next token in the document. | |
virtual bool | parseChildren (TextParser &parser) |
Parse children nodes and add them to the node. | |
virtual bool | parseNode (TextParser &parser, const Node *parent) override |
Parse the node. | |
void | setPreserveSpace (bool on) |
Called by the subclass when its spaces shall be preserved. | |
XML-to-JSON converter.
An XML-to-JSON converter is a model document which is used to convert an XML document into a JSON object.
In this class, the XML model is not used to validate the XML document. The model is optional (it can be empty). It is only used as a hint to infer the type of XML attributes and text nodes in the source document.
There is no standard way to convert XML to JSON. Several tools exist and each of them has its own conversion rules. Here, we use the following rules:
ts::xml::JSONConverter::JSONConverter | ( | Report & | report = (ts::NullReport::Instance()) | ) |
Default constructor.
[in,out] | report | Where to report errors. |
json::ValuePtr ts::xml::JSONConverter::convertToJSON | ( | const Document & | source, |
bool | force_root = false |
||
) | const |
Convert an XML document into a JSON object.
[in] | source | The source XML document to convert. |
[in] | force_root | If true, force the option --x2j-include-root. |
bool ts::xml::JSONConverter::convertToXML | ( | const json::Value & | source, |
Document & | destination, | ||
bool | auto_validate | ||
) | const |
Convert a JSON object into an XML document.
Not all JSON values can be converted. Basically, only JSON objects which were previously converted from XML are guaranteed to be converted back. For other values, a best-effort conversion is applied, without guarantee.
[in] | source | The source JSON value to convert. If this is a JSON object, it becomes the root of the XML document. If this is an array, the XML root is taken from the model and the array elements are converted inside that root. |
[out] | destination | The converted XML document. |
[in] | auto_validate | If true, the converted document is validated according to the modeL. |
|
inherited |
Validate an XML document.
[in] | doc | The document to validate according to the model. |
|
protectedinherited |
Find a child element by name in an XML model element.
[in] | elem | An XML element in a model document. |
[in] | name | Name of the child element to search. |
|
inherited |
Parse an XML document.
[in] | lines | List of text lines forming the XML document. |
|
inherited |
Parse an XML document.
[in] | text | The XML document. |
|
inherited |
Load and parse an XML file.
[in] | fileName | Name of the XML file to load. If fileName is empty or "-", read the standard input. If fileName starts with "<?xml", this is considered as "inline XML content". The document is loaded from this string instead of reading a file. |
[in] | search | If true, search the XML file in the TSDuck configuration directories if fileName is not found and does not contain any directory part. |
|
inherited |
Load and parse an XML file.
[in,out] | strm | A standard text stream in input mode. |
|
inherited |
Save an XML file.
[in] | fileName | Name of the XML file to save. If fileName is empty or "-", writes to the standard output. |
[in] | indent | Indentation width of each level. |
|
staticinherited |
Check if a "file name" is in fact inline XML content instead of a file name.
[in] | name | A file name string. |
|
staticinherited |
Get a suitable display name for an XML file name or inline content.
[in] | name | A file name string. |
[in] | stdInputIfEmpty | If true and if fileName is empty, reads the standard input. |
|
inlineinherited |
Get the root element of the document.
|
inlineinherited |
Get the root element of the document.
|
inherited |
Initialize the document.
The initial declaration and root are created.
[in] | rootName | Name of the root element to create. |
[in] | declaration | Optional XML declaration. When omitted, the standard declaration is used, specifying UTF-8 as format. |
|
overridevirtualinherited |
Get a constant reference to the global XML parsing and formatting tweaks for the document.
Reimplemented from ts::xml::Node.
|
inlineinherited |
Set the global XML parsing and formatting tweaks for the document.
[in] | tw | The new global XML tweaks. |
|
overridevirtualinherited |
Convert to a string object.
Implements ts::StringifyInterface.
|
overridevirtualinherited |
Clone the content of the node in a dynamically allocated object.
This virtual method must be implemented by subclasses to allocate a object of the right class.
Implements ts::xml::Node.
|
overridevirtualinherited |
|
overridevirtualinherited |
Format the node for an output XML document.
[in,out] | output | The output object to format the XML document. |
[in] | keepNodeOpen | If true, keep the node open so that children may be printed later. |
Implements ts::xml::Node.
|
overridevirtualinherited |
Print the closing tags for the node.
Typically used after print() when keepNodeOpen was true. The default implementation is to do nothing. Subclasses may replace this.
[in,out] | output | The output object to format the XML document. |
[in] | levels | Number of levels to close. By default, close the complete document. If zero, no output is produced. |
Reimplemented from ts::xml::Node.
|
overrideprotectedvirtualinherited |
Parse the node.
[in,out] | parser | The document parser. On input, the current position of the parser after the tag which identified the node ("<?", "<!- -", etc.) On output, it must be after the last character of the node. |
[in] | parent | Candidate parent node, for information only, do not modify. Can be null. |
Implements ts::xml::Node.
|
inlineinherited |
Get the line number in input document.
|
virtualinherited |
Clear the content of the node.
The node becomes empty but remains attached to its parent.
Reimplemented in ts::xml::Element.
|
inherited |
Attach the node to a new parent.
The node is first detached from its previous parent.
[in,out] | newParent | New parent. If zero, the node becomes orphan. In that case, the node will no longer be freed by its parent and must be explicitly deleted. |
[in] | last | If true, the child is added at the end of the list of children. If false, it is added at the beginning. |
|
inlineinherited |
Move the node before another node, potentially to a new parent.
[in,out] | newSibling | A new sibling. The node will be moved before this one. |
|
inlineinherited |
Move the node after another node, potentially to a new parent.
[in,out] | newSibling | A new sibling. The node will be moved after this one. |
|
inherited |
Move the node before or after another node, potentially to a new parent.
[in,out] | newSibling | A new sibling. The node will be moved before or after this one. |
[in] | before | If true, move the node before newSibling, after it otherwise. |
|
inlineinherited |
Get the parent's node.
|
inlineinherited |
Get the parent's node.
|
inlineinherited |
Get the document into which the node is located.
|
inherited |
Get the document into which the node is located.
|
inherited |
Get the depth of an XML element.
|
inherited |
Check if the node or one of its ancestors has attribute xml:space="preserve".
|
inlineinherited |
Check if the node has children.
|
inlineinherited |
Get the number of children.
|
inlineinherited |
Get the first child of a node.
|
inlineinherited |
Get the first child of a node.
|
inlineinherited |
Get the last child.
|
inlineinherited |
Get the last child.
|
inlineinherited |
Get the next sibling node.
|
inherited |
Get the next sibling node.
|
inlineinherited |
Get the previous sibling node.
|
inherited |
Get the previous sibling node.
|
inlineinherited |
|
inherited |
|
inlineinherited |
Find the next sibling element.
|
inherited |
Find the next sibling element.
|
inlineinherited |
Find the previous sibling element.
|
inherited |
Find the previous sibling element.
|
inlineinherited |
Get the value of the node.
The semantic of the value depends on the node subclass:
|
inlineinherited |
|
inherited |
Remove all comments in the XML node.
[in] | recurse | If true, apply recursively to all children nodes. |
|
virtualinherited |
Format the value as a one-liner XML text.
|
virtualinherited |
Check if the text shall be stuck to other elements in XML output.
Reimplemented in ts::xml::Text.
|
inherited |
Build a debug string for the node.
|
inlineinherited |
Get a reference to the report object for the XML node.
|
protectedinherited |
Identify the next token in the document.
[in,out] | parser | The document parser. |
|
protectedvirtualinherited |
Parse children nodes and add them to the node.
Stop either at end of document or before a "</" sequence or on error.
[in,out] | parser | The document parser. |
|
inlineprotectedinherited |
Called by the subclass when its spaces shall be preserved.
Typically called when xml:space="preserve" is encountered.
[in] | on | True is spaces shall be preserved. |
|
inlineinherited |
Check if the node is alone in its own ring.
|
inherited |
Insert this object in a ring after the specified node.
[in] | o | A node of a ring. This object is inserted after o in the ring. |
|
inherited |
Insert this object in a ring before the specified node.
[in] | o | A node of a ring. This object is inserted before o in the ring. |
|
inherited |
Swap this object and another one in their rings.
If the two objects belong to the same ring, their positions are swapped. If they belong to distinct rings, they also move to each other's ring.
[in] | o | The node to swap with. |
|
inlineinherited |
Get the next node in the ring.
T | A superclass of RingNode, the expected type of the next object in the ring. |
|
inlineinherited |
Get the next node in the ring.
T | A superclass of RingNode, the expected type of the next object in the ring. |
|
inlineinherited |
Get the previous node in the ring.
T | A superclass of RingNode, the expected type of the previous object in the ring. |
|
inlineinherited |
Get the previous node in the ring.
T | A superclass of RingNode, the expected type of the previous object in the ring. |
|
inherited |
Count the number of element in the ring.
Warning: This method has a linear response time, avoid using it when possible.