File: JsonParser.cpp

package info (click to toggle)
ola 0.10.8.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: cpp: 132,274; python: 14,082; javascript: 6,774; sh: 4,616; ansic: 2,189; java: 518; xml: 253; makefile: 183
file content (220 lines) | stat: -rw-r--r-- 5,668 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * JsonParser.cpp
 * A Json Parser.
 * Copyright (C) 2014 Simon Newton
 */
#include "ola/web/JsonParser.h"

#define __STDC_LIMIT_MACROS  // for UINT8_MAX & friends

#include <memory>
#include <string>
#include "ola/Logging.h"
#include "ola/stl/STLUtils.h"
#include "ola/web/Json.h"
#include "ola/web/JsonLexer.h"

namespace ola {
namespace web {

using std::string;

void JsonParser::Begin() {
  m_error = "";
  m_root.reset();
  m_key = "";

  STLEmptyStack(&m_container_stack);
  STLEmptyStack(&m_array_stack);
  STLEmptyStack(&m_object_stack);
}

void JsonParser::End() {
  if (!m_container_stack.empty()) {
    OLA_WARN << "Json container stack is not empty";
  }
  STLEmptyStack(&m_container_stack);

  if (!m_array_stack.empty()) {
    OLA_WARN << "JsonArray stack is not empty";
  }
  STLEmptyStack(&m_array_stack);

  if (!m_object_stack.empty()) {
    OLA_WARN << "JsonObject stack is not empty";
  }
  STLEmptyStack(&m_object_stack);
}

void JsonParser::String(const string &value) {
  AddValue(new JsonString(value));
}

void JsonParser::Number(uint32_t value) {
  AddValue(new JsonUInt(value));
}

void JsonParser::Number(int32_t value) {
  AddValue(new JsonInt(value));
}

void JsonParser::Number(uint64_t value) {
  AddValue(new JsonUInt64(value));
}

void JsonParser::Number(int64_t value) {
  AddValue(new JsonInt64(value));
}

void JsonParser::Number(const JsonDouble::DoubleRepresentation &rep) {
  AddValue(new JsonDouble(rep));
}

void JsonParser::Number(double value) {
  AddValue(new JsonDouble(value));
}

void JsonParser::Bool(bool value) {
  AddValue(new JsonBool(value));
}

void JsonParser::Null() {
  AddValue(new JsonNull());
}

void JsonParser::OpenArray() {
  if (m_container_stack.empty()) {
    m_array_stack.push(new JsonArray());
    m_root.reset(m_array_stack.top());
  } else if (m_container_stack.top() == ARRAY && !m_array_stack.empty()) {
    m_array_stack.push(m_array_stack.top()->AppendArray());
  } else if (m_container_stack.top() == OBJECT && !m_object_stack.empty()) {
    m_array_stack.push(m_object_stack.top()->AddArray(m_key));
    m_key = "";
  } else {
    OLA_WARN << "Can't find where to start array";
    m_error = "Internal error";
  }
  m_container_stack.push(ARRAY);
}

void JsonParser::CloseArray() {
  if (m_container_stack.empty() || m_container_stack.top() != ARRAY ||
      m_array_stack.empty()) {
    OLA_WARN << "Mismatched CloseArray()";
    m_error = "Internal error";
    return;
  }

  m_container_stack.pop();
  m_array_stack.pop();
}

void JsonParser::OpenObject() {
  if (m_container_stack.empty()) {
    m_object_stack.push(new JsonObject());
    m_root.reset(m_object_stack.top());
  } else if (m_container_stack.top() == ARRAY && !m_array_stack.empty()) {
    m_object_stack.push(m_array_stack.top()->AppendObject());
  } else if (m_container_stack.top() == OBJECT && !m_object_stack.empty()) {
    m_object_stack.push(m_object_stack.top()->AddObject(m_key));
    m_key = "";
  } else {
    OLA_WARN << "Can't find where to start object";
    m_error = "Internal error";
  }
  m_container_stack.push(OBJECT);
}

void JsonParser::ObjectKey(const string &key) {
  if (!m_key.empty()) {
    OLA_WARN << "Json Key should be empty, was " << key;
  }
  m_key = key;
}

void JsonParser::CloseObject() {
  if (m_container_stack.empty() || m_container_stack.top() != OBJECT ||
      m_object_stack.empty()) {
    OLA_WARN << "Mismatched CloseObject()";
    m_error = "Internal error";
    return;
  }

  m_container_stack.pop();
  m_object_stack.pop();
}

void JsonParser::SetError(const string &error) {
  m_error = error;
}

string JsonParser::GetError() const {
  return m_error;
}

JsonValue *JsonParser::GetRoot() {
  return m_root.get();
}

JsonValue *JsonParser::ClaimRoot() {
  if (m_error.empty()) {
    return m_root.release();
  } else {
    return NULL;
  }
}

void JsonParser::AddValue(JsonValue *value) {
  if (!m_container_stack.empty() && m_container_stack.top() == ARRAY) {
    if (m_array_stack.empty()) {
      OLA_WARN << "Missing JsonArray, parsing is broken!";
      m_error = "Internal error";
      delete value;
    } else {
      m_array_stack.top()->Append(value);
    }
  } else if (!m_container_stack.empty() && m_container_stack.top() == OBJECT) {
    if (m_object_stack.empty()) {
      OLA_WARN << "Missing JsonObject, parsing is broken!";
      m_error = "Internal error";
      delete value;
    } else {
      m_object_stack.top()->AddValue(m_key, value);
      m_key = "";
    }
  } else if (!m_root.get()) {
    m_root.reset(value);
    return;
  } else {
    OLA_WARN << "Parse stack broken";
    m_error = "Internal error";
    delete value;
  }
}

JsonValue* JsonParser::Parse(const string &input, string *error) {
  JsonParser parser;
  if (JsonLexer::Parse(input, &parser)) {
    return parser.ClaimRoot();
  } else {
    *error = parser.GetError();
    return NULL;
  }
}
}  // namespace web
}  // namespace ola