22 #include "jsonparser.h" 33 : m_value(
sajson::TYPE_NULL, 0, 0)
42 bool JSON::Node::IsNull()
const 44 return (m_value.get_type() == sajson::TYPE_NULL);
47 bool JSON::Node::IsObject()
const 49 return (m_value.get_type() == sajson::TYPE_OBJECT);
52 bool JSON::Node::IsArray()
const 54 return (m_value.get_type() == sajson::TYPE_ARRAY);
57 bool JSON::Node::IsString()
const 59 return (m_value.get_type() == sajson::TYPE_STRING);
62 bool JSON::Node::IsDouble()
const 64 return (m_value.get_type() == sajson::TYPE_DOUBLE);
67 bool JSON::Node::IsInt()
const 69 return (m_value.get_type() == sajson::TYPE_INTEGER);
72 bool JSON::Node::IsTrue()
const 74 return (m_value.get_type() == sajson::TYPE_TRUE);
77 bool JSON::Node::IsFalse()
const 79 return (m_value.get_type() == sajson::TYPE_FALSE);
82 std::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());
90 size_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());
98 double 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());
106 int64_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());
114 int32_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());
122 size_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());
130 JSON::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());
138 std::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();
146 JSON::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());
154 JSON::Node JSON::Node::GetObjectValue(
const char *key)
const 156 if (m_value.get_type() == sajson::TYPE_OBJECT)
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());
177 content.reserve(resp.GetContentLength());
181 while ((s = resp.ReadContent(buf,
sizeof(buf))))
182 content.append(buf, s);
183 if (!content.empty())
185 DBG(DBG_PROTO,
"%s: %s\n", __FUNCTION__, content.c_str());
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__);
204 return Node(m_document->get_root());