22#include "jsonparser.h"
25using namespace NSROOT;
33: m_value(sajson::TYPE_NULL, 0, 0)
42bool JSON::Node::IsNull()
const
44 return (m_value.get_type() == sajson::TYPE_NULL);
47bool JSON::Node::IsObject()
const
49 return (m_value.get_type() == sajson::TYPE_OBJECT);
52bool JSON::Node::IsArray()
const
54 return (m_value.get_type() == sajson::TYPE_ARRAY);
57bool JSON::Node::IsString()
const
59 return (m_value.get_type() == sajson::TYPE_STRING);
62bool JSON::Node::IsDouble()
const
64 return (m_value.get_type() == sajson::TYPE_DOUBLE);
67bool JSON::Node::IsInt()
const
69 return (m_value.get_type() == sajson::TYPE_INTEGER);
72bool JSON::Node::IsTrue()
const
74 return (m_value.get_type() == sajson::TYPE_TRUE);
77bool JSON::Node::IsFalse()
const
79 return (m_value.get_type() == sajson::TYPE_FALSE);
82std::string JSON::Node::GetStringValue()
const
84 if (m_value.get_type() == sajson::TYPE_STRING)
85 return m_value.as_string();
86 DBG(DBG_ERROR,
"%s: bad type (%d)\n", __FUNCTION__, (
int) m_value.get_type());
90size_t JSON::Node::GetStringSize()
const
92 if (m_value.get_type() == sajson::TYPE_STRING)
93 return m_value.get_string_length();
94 DBG(DBG_ERROR,
"%s: bad type (%d)\n", __FUNCTION__, (
int) m_value.get_type());
98double JSON::Node::GetDoubleValue()
const
100 if (m_value.get_type() == sajson::TYPE_DOUBLE)
101 return m_value.get_double_value();
102 DBG(DBG_ERROR,
"%s: bad type (%d)\n", __FUNCTION__, (
int) m_value.get_type());
106int64_t JSON::Node::GetBigIntValue()
const
108 if (m_value.get_type() == sajson::TYPE_DOUBLE || m_value.get_type() == sajson::TYPE_INTEGER)
109 return (int64_t) m_value.get_number_value();
110 DBG(DBG_ERROR,
"%s: bad type (%d)\n", __FUNCTION__, (
int) m_value.get_type());
114int32_t JSON::Node::GetIntValue()
const
116 if (m_value.get_type() == sajson::TYPE_INTEGER)
117 return (int32_t) m_value.get_integer_value();
118 DBG(DBG_ERROR,
"%s: bad type (%d)\n", __FUNCTION__, (
int) m_value.get_type());
122size_t JSON::Node::Size()
const
124 if (m_value.get_type() == sajson::TYPE_ARRAY || m_value.get_type() == sajson::TYPE_OBJECT)
125 return m_value.get_length();
126 DBG(DBG_ERROR,
"%s: bad type (%d)\n", __FUNCTION__, (
int) m_value.get_type());
130JSON::Node JSON::Node::GetArrayElement(
size_t index)
const
132 if (m_value.get_type() == sajson::TYPE_ARRAY)
133 return Node(m_value.get_array_element(index));
134 DBG(DBG_ERROR,
"%s: bad type (%d)\n", __FUNCTION__, (
int) m_value.get_type());
138std::string JSON::Node::GetObjectKey(
size_t index)
const
140 if (m_value.get_type() == sajson::TYPE_OBJECT)
141 return m_value.get_object_key(index).as_string();
142 DBG(DBG_ERROR,
"%s: bad type (%d)\n", __FUNCTION__, (
int) m_value.get_type());
143 return std::string();
146JSON::Node JSON::Node::GetObjectValue(
size_t index)
const
148 if (m_value.get_type() == sajson::TYPE_OBJECT)
149 return Node(m_value.get_object_value(index));
150 DBG(DBG_ERROR,
"%s: bad type (%d)\n", __FUNCTION__, (
int) m_value.get_type());
154JSON::Node JSON::Node::GetObjectValue(
const char *key)
const
156 if (m_value.get_type() == sajson::TYPE_OBJECT)
158 size_t idx = m_value.find_object_key(sajson::literal(key));
159 if (idx < m_value.get_length())
160 return Node(m_value.get_object_value(idx));
163 DBG(DBG_ERROR,
"%s: bad type (%d)\n", __FUNCTION__, (
int) m_value.get_type());
172JSON::Document::Document(NSROOT::WSResponse& resp)
177 content.reserve(resp.GetContentLength());
181 while ((s = resp.ReadContent(buf,
sizeof(buf))) > 0)
182 content.append(buf, s);
183 if (s == 0 && !content.empty())
185 DBG(DBG_PROTO,
"%s: %s\n", __FUNCTION__, content.c_str());
187 m_document = new sajson::document(sajson::parse(sajson::string(content.c_str(), content.length())));
189 DBG(DBG_ERROR,
"%s: memory allocation failed\n", __FUNCTION__);
190 else if (!m_document->is_valid())
191 DBG(DBG_ERROR,
"%s: failed to parse: %d: %s\n", __FUNCTION__, (int)m_document->get_error_line(), m_document->get_error_message().c_str());
197 DBG(DBG_ERROR,
"%s: read error\n", __FUNCTION__);
201JSON::Document::Document(
const std::string& content)
205 if (!content.empty())
207 DBG(DBG_PROTO,
"%s: %s\n", __FUNCTION__, content.c_str());
209 m_document = new sajson::document(sajson::parse(sajson::string(content.c_str(), content.length())));
211 DBG(DBG_ERROR,
"%s: memory allocation failed\n", __FUNCTION__);
212 else if (!m_document->is_valid())
213 DBG(DBG_ERROR,
"%s: failed to parse: %d: %s\n", __FUNCTION__, (int)m_document->get_error_line(), m_document->get_error_message().c_str());
219 DBG(DBG_ERROR,
"%s: read error\n", __FUNCTION__);
226 return Node(m_document->get_root());