Electroneum
JsonReader Class Reference

Represents a JSON reader which implements Archiver concept. More...

#include <archiver.h>

Public Member Functions

 JsonReader (const char *json)
 Constructor. More...
 
 ~JsonReader ()
 Destructor. More...
 
 operator bool () const
 
JsonReaderStartObject ()
 
JsonReaderMember (const char *name)
 
bool HasMember (const char *name) const
 
JsonReaderEndObject ()
 
JsonReaderStartArray (size_t *size=0)
 
JsonReaderEndArray ()
 
JsonReaderoperator & (bool &b)
 
JsonReaderoperator & (unsigned &u)
 
JsonReaderoperator & (int &i)
 
JsonReaderoperator & (double &d)
 
JsonReaderoperator & (std::string &s)
 
JsonReaderSetNull ()
 

Static Public Attributes

static const bool IsReader = true
 
static const bool IsWriter = !IsReader
 

Detailed Description

Represents a JSON reader which implements Archiver concept.

Definition at line 56 of file archiver.h.

Constructor & Destructor Documentation

◆ JsonReader()

JsonReader::JsonReader ( const char *  json)

Constructor.

Parameters
jsonA non-const source json string for in-situ parsing.
Note
in-situ means the source JSON string will be modified after parsing.

Definition at line 31 of file archiver.cpp.

31  : mDocument(), mStack(), mError(false) {
32  mDocument = new Document;
33  DOCUMENT->Parse(json);
34  if (DOCUMENT->HasParseError())
35  mError = true;
36  else {
37  mStack = new JsonReaderStack;
39  }
40 }
#define DOCUMENT
Definition: archiver.cpp:26
#define STACK
Definition: archiver.cpp:27
An object/array is in the stack but it is not yet called by StartObject()/StartArray().
Definition: archiver.cpp:12
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition: document.h:2512
std::stack< JsonReaderStackItem > JsonReaderStack
Definition: archiver.cpp:24
rapidjson::Document json
Definition: transport.cpp:49

◆ ~JsonReader()

JsonReader::~JsonReader ( )

Destructor.

Definition at line 42 of file archiver.cpp.

42  {
43  delete DOCUMENT;
44  delete STACK;
45 }
#define DOCUMENT
Definition: archiver.cpp:26
#define STACK
Definition: archiver.cpp:27

Member Function Documentation

◆ EndArray()

JsonReader & JsonReader::EndArray ( )

Definition at line 109 of file archiver.cpp.

109  {
110  if (!mError) {
111  if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::Closed)
112  Next();
113  else
114  mError = true;
115  }
116  return *this;
117 }
An array is closed after read all element, but before EndArray().
Definition: archiver.cpp:14
#define TOP
Definition: archiver.cpp:28
#define CURRENT
Definition: archiver.cpp:29

◆ EndObject()

JsonReader & JsonReader::EndObject ( )

Definition at line 58 of file archiver.cpp.

58  {
59  if (!mError) {
60  if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started)
61  Next();
62  else
63  mError = true;
64  }
65  return *this;
66 }
An object/array is called by StartObject()/StartArray().
Definition: archiver.cpp:13
#define TOP
Definition: archiver.cpp:28
#define CURRENT
Definition: archiver.cpp:29

◆ HasMember()

bool JsonReader::HasMember ( const char *  name) const

Definition at line 83 of file archiver.cpp.

83  {
84  if (!mError && CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started)
85  return CURRENT.HasMember(name);
86  return false;
87 }
An object/array is called by StartObject()/StartArray().
Definition: archiver.cpp:13
const char * name
#define TOP
Definition: archiver.cpp:28
#define CURRENT
Definition: archiver.cpp:29

◆ Member()

JsonReader & JsonReader::Member ( const char *  name)

Definition at line 68 of file archiver.cpp.

68  {
69  if (!mError) {
70  if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started) {
71  Value::ConstMemberIterator memberItr = CURRENT.FindMember(name);
72  if (memberItr != CURRENT.MemberEnd())
74  else
75  mError = true;
76  }
77  else
78  mError = true;
79  }
80  return *this;
81 }
#define STACK
Definition: archiver.cpp:27
An object/array is called by StartObject()/StartArray().
Definition: archiver.cpp:13
An object/array is in the stack but it is not yet called by StartObject()/StartArray().
Definition: archiver.cpp:12
const char * name
#define TOP
Definition: archiver.cpp:28
#define CURRENT
Definition: archiver.cpp:29
(Constant) member iterator for a JSON object value
Definition: document.h:99

◆ operator &() [1/5]

JsonReader& JsonReader::operator& ( bool b)

◆ operator &() [2/5]

JsonReader& JsonReader::operator& ( unsigned &  u)

◆ operator &() [3/5]

JsonReader& JsonReader::operator& ( int &  i)

◆ operator &() [4/5]

JsonReader& JsonReader::operator& ( double &  d)

◆ operator &() [5/5]

JsonReader& JsonReader::operator& ( std::string &  s)

◆ operator bool()

JsonReader::operator bool ( ) const
inline

Definition at line 70 of file archiver.h.

70 { return !mError; }

◆ SetNull()

JsonReader & JsonReader::SetNull ( )

Definition at line 179 of file archiver.cpp.

179  {
180  // This function is for JsonWriter only.
181  mError = true;
182  return *this;
183 }

◆ StartArray()

JsonReader & JsonReader::StartArray ( size_t *  size = 0)

Definition at line 89 of file archiver.cpp.

89  {
90  if (!mError) {
91  if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::BeforeStart) {
93  if (size)
94  *size = CURRENT.Size();
95 
96  if (!CURRENT.Empty()) {
97  const Value* value = &CURRENT[TOP.index];
99  }
100  else
102  }
103  else
104  mError = true;
105  }
106  return *this;
107 }
#define STACK
Definition: archiver.cpp:27
An object/array is called by StartObject()/StartArray().
Definition: archiver.cpp:13
An object/array is in the stack but it is not yet called by StartObject()/StartArray().
Definition: archiver.cpp:12
An array is closed after read all element, but before EndArray().
Definition: archiver.cpp:14
#define TOP
Definition: archiver.cpp:28
#define CURRENT
Definition: archiver.cpp:29
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225

◆ StartObject()

JsonReader & JsonReader::StartObject ( )

Definition at line 48 of file archiver.cpp.

48  {
49  if (!mError) {
50  if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::BeforeStart)
52  else
53  mError = true;
54  }
55  return *this;
56 }
An object/array is called by StartObject()/StartArray().
Definition: archiver.cpp:13
An object/array is in the stack but it is not yet called by StartObject()/StartArray().
Definition: archiver.cpp:12
#define TOP
Definition: archiver.cpp:28
#define CURRENT
Definition: archiver.cpp:29

Member Data Documentation

◆ IsReader

const bool JsonReader::IsReader = true
static

Definition at line 88 of file archiver.h.

◆ IsWriter

const bool JsonReader::IsWriter = !IsReader
static

Definition at line 89 of file archiver.h.


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