Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 122 lines (118 sloc) 4.554 kB
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
1 //JsonMe++ C++ JSON parsing (wrapper) library.
2 //Copyright (C) Rob J Meijer 2011 <jsonme@polacanthus.net>
3 //
4 //This library is free software; you can redistribute it and/or
5 //modify it under the terms of the GNU Lesser General Public
6 //License as published by the Free Software Foundation; either
7 //version 2.1 of the License, or (at your option) any later version.
8 //
9 //This library 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 GNU
12 //Lesser General Public License for more details.
13 //
14 //You should have received a copy of the GNU Lesser General Public
15 //License along with this library; if not, write to the Free Software
16 //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
94baac2 @pibara Initial commit
authored
17 #include "GobjectImplFsTopNode.hpp"
18 #include "GobjectImplError.hpp"
19 #include "GobjectImplNode.hpp"
20 #include "GobjectImplScalar.hpp"
21 namespace jsonme {
22 namespace impl {
c4fcc91 @pibara Fixed memleak and added keys functionality.
authored
23 GobjectImplNode::GobjectImplNode(JsonNode * const node):mNode(node),mKeys(node) {
94baac2 @pibara Initial commit
authored
24 }
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
25 //Maps Glib JSON lib enum to JsonMe++ lib enum.
26ffa58 @pibara Addressed constness issues in the API.
authored
26 jsonme::nodetype GobjectImplNode::nodetype() const {
94baac2 @pibara Initial commit
authored
27 if (!mNode) {
28 return jsonme::INVALID;
29 }
30 switch (json_node_get_node_type(mNode)) {
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
31 case JSON_NODE_OBJECT: return jsonme::OBJECT;
32 case JSON_NODE_ARRAY: return jsonme::ARRAY;
33 case JSON_NODE_VALUE: return jsonme::SCALAR;
34 case JSON_NODE_NULL: return jsonme::INVALID;
94baac2 @pibara Initial commit
authored
35 }
36 return jsonme::INVALID;
37 }
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
38
26ffa58 @pibara Addressed constness issues in the API.
authored
39 Node GobjectImplNode::operator[](std::string const & name) const {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
40 //Calling this operator on a non object returns a wrapped NullNode.
94baac2 @pibara Initial commit
authored
41 if (!mNode) {
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
42 return Node();
94baac2 @pibara Initial commit
authored
43 }
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
44 switch (json_node_get_node_type(mNode)) {
45 case JSON_NODE_OBJECT: break;
46 case JSON_NODE_ARRAY: return Node();
47 case JSON_NODE_VALUE: return Node();
48 case JSON_NODE_NULL: return Node();
49 }
94baac2 @pibara Initial commit
authored
50 JsonObject *asobject=json_node_get_object(mNode);
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
51 if (asobject) {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
52 //Fetch the given property.
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
53 JsonNode *member=json_object_get_member(asobject,name.c_str());
54 if (member) {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
55 //Return the sub node.
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
56 return Node(new GobjectImplNode(member));
57 } else {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
58 //If there uis no such property return a wrapped NullNode
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
59 return Node();
60 }
61 } else {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
62 return Node(); //This is probably dead code, just there in case.
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
63 }
94baac2 @pibara Initial commit
authored
64 }
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
65
26ffa58 @pibara Addressed constness issues in the API.
authored
66 size_t GobjectImplNode::size() const {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
67 //Anything not an array has a zero size.
94baac2 @pibara Initial commit
authored
68 if (!mNode) {
69 return 0;
70 }
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
71 switch (json_node_get_node_type(mNode)) {
72 case JSON_NODE_OBJECT: return 0;
73 case JSON_NODE_ARRAY: break;
321ab6f @pibara Fixed typo's
authored
74 case JSON_NODE_VALUE: return 0;
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
75 case JSON_NODE_NULL: return 0;
76 }
94baac2 @pibara Initial commit
authored
77 JsonArray *asarray=json_node_get_array(mNode);
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
78 if (asarray) {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
79 return json_array_get_length(asarray); //Return the array length
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
80 } else {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
81 return 0; //This is probably dead code, just there in case.
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
82 }
94baac2 @pibara Initial commit
authored
83 }
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
84
26ffa58 @pibara Addressed constness issues in the API.
authored
85 Node GobjectImplNode::operator[](size_t index) const {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
86 //Calling this operator on a non array returns a wrapped NullNode.
94baac2 @pibara Initial commit
authored
87 if (!mNode) {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
88 return Node();
89 }
90 switch (json_node_get_node_type(mNode)) {
91 case JSON_NODE_OBJECT: return Node();
92 case JSON_NODE_ARRAY: break;
93 case JSON_NODE_VALUE: return Node();
94 case JSON_NODE_NULL: return Node();
94baac2 @pibara Initial commit
authored
95 }
96 JsonArray *asarray=json_node_get_array(mNode);
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
97 if (asarray) {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
98 //Fetch the indexed entity.
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
99 JsonNode *member=json_array_get_element(asarray,index);
100 if (member) {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
101 return Node(new GobjectImplNode(member)); //return the sub node.
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
102 } else {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
103 return Node(); //Or a wraped NullNode if we can't find it.
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
104 }
105 } else {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
106 return Node(); //This is probably dead code, just there in case.
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
107 }
94baac2 @pibara Initial commit
authored
108 }
26ffa58 @pibara Addressed constness issues in the API.
authored
109 GobjectImplNode::operator Scalar() const {
7bf4398 @pibara Added LGPL comments to the code, added comment and made a few robustn…
authored
110 //Only scalar type node's can be cast to a scalar. Other node's return a wrapped NullScalar.
111 if (!mNode) {
112 return Scalar();
113 }
977dec3 @pibara Fixed a bug in nodetype(), made things more robust by adding more tests.
authored
114 if (this->nodetype() == jsonme::SCALAR) {
115 return Scalar(new GobjectImplScalar(mNode));
116 } else {
117 return Scalar();
118 }
94baac2 @pibara Initial commit
authored
119 }
120 }
121 }
Something went wrong with that request. Please try again.