CPPMyth
Library to interoperate with MythTV server
jsonparser.cpp
1 /*
2  * Copyright (C) 2014 Jean-Luc Barriere
3  *
4  * This Program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This Program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; see the file COPYING. If not, write to
16  * the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301 USA
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21 
22 #include "jsonparser.h"
23 #include "debug.h"
24 
25 using namespace NSROOT;
26 
31 
32 JSON::Node::Node()
33 : m_value(sajson::TYPE_NULL, 0, 0)
34 {
35 }
36 
37 JSON::Node::Node(const sajson::value& value)
38 : m_value(value)
39 {
40 }
41 
42 bool JSON::Node::IsNull() const
43 {
44  return (m_value.get_type() == sajson::TYPE_NULL);
45 }
46 
47 bool JSON::Node::IsObject() const
48 {
49  return (m_value.get_type() == sajson::TYPE_OBJECT);
50 }
51 
52 bool JSON::Node::IsArray() const
53 {
54  return (m_value.get_type() == sajson::TYPE_ARRAY);
55 }
56 
57 bool JSON::Node::IsString() const
58 {
59  return (m_value.get_type() == sajson::TYPE_STRING);
60 }
61 
62 bool JSON::Node::IsDouble() const
63 {
64  return (m_value.get_type() == sajson::TYPE_DOUBLE);
65 }
66 
67 bool JSON::Node::IsInt() const
68 {
69  return (m_value.get_type() == sajson::TYPE_INTEGER);
70 }
71 
72 bool JSON::Node::IsTrue() const
73 {
74  return (m_value.get_type() == sajson::TYPE_TRUE);
75 }
76 
77 bool JSON::Node::IsFalse() const
78 {
79  return (m_value.get_type() == sajson::TYPE_FALSE);
80 }
81 
82 std::string JSON::Node::GetStringValue() const
83 {
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());
87  return std::string();
88 }
89 
90 size_t JSON::Node::GetStringSize() const
91 {
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());
95  return 0;
96 }
97 
98 double JSON::Node::GetDoubleValue() const
99 {
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());
103  return 0.0;
104 }
105 
106 int64_t JSON::Node::GetBigIntValue() const
107 {
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());
111  return 0;
112 }
113 
114 int32_t JSON::Node::GetIntValue() const
115 {
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());
119  return 0;
120 }
121 
122 size_t JSON::Node::Size() const
123 {
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());
127  return 0;
128 }
129 
130 JSON::Node JSON::Node::GetArrayElement(size_t index) const
131 {
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());
135  return Node();
136 }
137 
138 std::string JSON::Node::GetObjectKey(size_t index) const
139 {
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();
144 }
145 
146 JSON::Node JSON::Node::GetObjectValue(size_t index) const
147 {
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());
151  return Node();
152 }
153 
154 JSON::Node JSON::Node::GetObjectValue(const char *key) const
155 {
156  if (m_value.get_type() == sajson::TYPE_OBJECT)
157  {
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));
161  return Node();
162  }
163  DBG(DBG_ERROR, "%s: bad type (%d)\n", __FUNCTION__, (int) m_value.get_type());
164  return Node();
165 }
166 
171 
172 JSON::Document::Document(NSROOT::WSResponse& resp)
173 : m_isValid(false)
174 , m_document(NULL)
175 {
176  std::string content;
177  content.reserve(resp.GetContentLength());
178  // Read content response
179  size_t s;
180  char buf[4000];
181  while ((s = resp.ReadContent(buf, sizeof(buf))))
182  content.append(buf, s);
183  if (!content.empty())
184  {
185  DBG(DBG_PROTO, "%s: %s\n", __FUNCTION__, content.c_str());
186  // Parse JSON content
187  m_document = new sajson::document(sajson::parse(sajson::string(content.c_str(), content.length())));
188  if (!m_document)
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());
192  else
193  m_isValid = true;
194  }
195  else
196  {
197  DBG(DBG_ERROR, "%s: read error\n", __FUNCTION__);
198  }
199 }
200 
201 JSON::Node JSON::Document::GetRoot() const
202 {
203  if (m_document)
204  return Node(m_document->get_root());
205  return Node();
206 }
207 
Definition: sajson.h:48