GClasses
GClasses::GDom Class Reference

Detailed Description

A Document Object Model. This represents a document as a hierarchy of objects. The DOM can be loaded-from or saved-to a file in JSON (JavaScript Object Notation) format. (See http://json.org.) In the future, support for XML and/or other formats may be added.

#include <GDom.h>

Public Member Functions

 GDom ()
 
 ~GDom ()
 
void clear ()
 Clears the DOM. More...
 
GHeapheap ()
 Returns a pointer to the heap used by this doc. More...
 
void loadJson (const char *szFilename)
 Load from the specified file in JSON format. (See http://json.org.) More...
 
GDomNodenewBool (bool b)
 Makes a new boolean node. More...
 
GDomNodenewDouble (double d)
 Makes a new double node. More...
 
GDomNodenewInt (long long n)
 Makes a new integer node. More...
 
GDomNodenewList ()
 Makes a new list node. More...
 
GDomNodenewNull ()
 Makes a new node to represent a null value. More...
 
GDomNodenewObj ()
 Makes a new object node. More...
 
GDomNodenewString (const char *szString)
 Makes a new string node from a null-terminated string. (If you want to parse a JSON string, call parseJson instead. This method just wraps the string in a node.) More...
 
GDomNodenewString (const char *pString, size_t len)
 Makes a new string node from the specified string segment. More...
 
void parseJson (const char *pJsonString, size_t len)
 Parses a JSON string. The resulting DOM can be retrieved by calling root(). More...
 
const GDomNoderoot () const
 Gets the root document node. More...
 
void saveJson (const char *szFilename) const
 Saves to a file in JSON format. (See http://json.org.) More...
 
const GDomNodesetRoot (const GDomNode *pNode)
 Sets the root document node. (Returns the same node that you pass in.) More...
 
void writeJson (std::ostream &stream) const
 Writes this doc to the specified stream in JSON format. (See http://json.org.) (If you want to write to a memory buffer, you can use open_memstream.) More...
 
void writeJsonCpp (std::ostream &stream) const
 Writes this doc to the specified stream as an inlined C++ string in JSON format. (This method would be useful for hard-coding a serialized object in a C++ program.) More...
 
void writeJsonPretty (std::ostream &stream) const
 Writes this doc to the specified stream in JSON format with indentation to make it human-readable. (If you want to write to a memory buffer, you can use open_memstream.) More...
 
void writeXml (std::ostream &stream) const
 Write as XML to the specified stream. More...
 

Static Public Member Functions

static void test ()
 Performs unit tests for this class. Throws an exception if there is a failure. More...
 

Protected Member Functions

GDomNodeloadJsonArray (GJsonTokenizer &tok)
 
GDomNodeloadJsonNumber (GJsonTokenizer &tok)
 
GDomNodeloadJsonObject (GJsonTokenizer &tok)
 
char * loadJsonString (GJsonTokenizer &tok)
 
GDomNodeloadJsonValue (GJsonTokenizer &tok)
 
GDomObjField * newField ()
 
GDomListItem * newItem ()
 

Protected Attributes

GHeap m_heap
 
size_t m_len
 
int m_line
 
const char * m_pDoc
 
const GDomNodem_pRoot
 

Friends

class GDomNode
 

Constructor & Destructor Documentation

GClasses::GDom::GDom ( )
GClasses::GDom::~GDom ( )

Member Function Documentation

void GClasses::GDom::clear ( )

Clears the DOM.

GHeap* GClasses::GDom::heap ( )
inline

Returns a pointer to the heap used by this doc.

void GClasses::GDom::loadJson ( const char *  szFilename)

Load from the specified file in JSON format. (See http://json.org.)

GDomNode* GClasses::GDom::loadJsonArray ( GJsonTokenizer &  tok)
protected
GDomNode* GClasses::GDom::loadJsonNumber ( GJsonTokenizer &  tok)
protected
GDomNode* GClasses::GDom::loadJsonObject ( GJsonTokenizer &  tok)
protected
char* GClasses::GDom::loadJsonString ( GJsonTokenizer &  tok)
protected
GDomNode* GClasses::GDom::loadJsonValue ( GJsonTokenizer &  tok)
protected
GDomNode* GClasses::GDom::newBool ( bool  b)

Makes a new boolean node.

GDomNode* GClasses::GDom::newDouble ( double  d)

Makes a new double node.

GDomObjField* GClasses::GDom::newField ( )
protected
GDomNode* GClasses::GDom::newInt ( long long  n)

Makes a new integer node.

GDomListItem* GClasses::GDom::newItem ( )
protected
GDomNode* GClasses::GDom::newList ( )

Makes a new list node.

GDomNode* GClasses::GDom::newNull ( )

Makes a new node to represent a null value.

GDomNode* GClasses::GDom::newObj ( )

Makes a new object node.

GDomNode* GClasses::GDom::newString ( const char *  szString)

Makes a new string node from a null-terminated string. (If you want to parse a JSON string, call parseJson instead. This method just wraps the string in a node.)

GDomNode* GClasses::GDom::newString ( const char *  pString,
size_t  len 
)

Makes a new string node from the specified string segment.

void GClasses::GDom::parseJson ( const char *  pJsonString,
size_t  len 
)

Parses a JSON string. The resulting DOM can be retrieved by calling root().

const GDomNode* GClasses::GDom::root ( ) const
inline

Gets the root document node.

void GClasses::GDom::saveJson ( const char *  szFilename) const

Saves to a file in JSON format. (See http://json.org.)

const GDomNode* GClasses::GDom::setRoot ( const GDomNode pNode)
inline

Sets the root document node. (Returns the same node that you pass in.)

static void GClasses::GDom::test ( )
static

Performs unit tests for this class. Throws an exception if there is a failure.

void GClasses::GDom::writeJson ( std::ostream &  stream) const

Writes this doc to the specified stream in JSON format. (See http://json.org.) (If you want to write to a memory buffer, you can use open_memstream.)

void GClasses::GDom::writeJsonCpp ( std::ostream &  stream) const

Writes this doc to the specified stream as an inlined C++ string in JSON format. (This method would be useful for hard-coding a serialized object in a C++ program.)

void GClasses::GDom::writeJsonPretty ( std::ostream &  stream) const

Writes this doc to the specified stream in JSON format with indentation to make it human-readable. (If you want to write to a memory buffer, you can use open_memstream.)

void GClasses::GDom::writeXml ( std::ostream &  stream) const

Write as XML to the specified stream.

Friends And Related Function Documentation

friend class GDomNode
friend

Member Data Documentation

GHeap GClasses::GDom::m_heap
protected
size_t GClasses::GDom::m_len
protected
int GClasses::GDom::m_line
protected
const char* GClasses::GDom::m_pDoc
protected
const GDomNode* GClasses::GDom::m_pRoot
protected