/
GobjectImplNode.cpp
121 lines (118 loc) · 4.59 KB
/
GobjectImplNode.cpp
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
//JsonMe++ C++ JSON parsing (wrapper) library.
//Copyright (C) Rob J Meijer 2011 <jsonme@polacanthus.net>
//
//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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "GobjectImplFsTopNode.hpp"
#include "GobjectImplError.hpp"
#include "GobjectImplNode.hpp"
#include "GobjectImplScalar.hpp"
namespace jsonme {
namespace impl {
GobjectImplNode::GobjectImplNode(JsonNode * const node):mNode(node),mKeys(node) {
}
//Maps Glib JSON lib enum to JsonMe++ lib enum.
jsonme::nodetype GobjectImplNode::nodetype() const {
if (!mNode) {
return jsonme::INVALID;
}
switch (json_node_get_node_type(mNode)) {
case JSON_NODE_OBJECT: return jsonme::OBJECT;
case JSON_NODE_ARRAY: return jsonme::ARRAY;
case JSON_NODE_VALUE: return jsonme::SCALAR;
case JSON_NODE_NULL: return jsonme::INVALID;
}
return jsonme::INVALID;
}
Node GobjectImplNode::operator[](std::string const & name) const {
//Calling this operator on a non object returns a wrapped NullNode.
if (!mNode) {
return Node(0,this,name);
}
switch (json_node_get_node_type(mNode)) {
case JSON_NODE_OBJECT: break;
case JSON_NODE_ARRAY: return Node(0,this,name);
case JSON_NODE_VALUE: return Node(0,this,name);
case JSON_NODE_NULL: return Node(0,this,name);
}
JsonObject *asobject=json_node_get_object(mNode);
if (asobject) {
//Fetch the given property.
JsonNode *member=json_object_get_member(asobject,name.c_str());
if (member) {
//Return the sub node.
return Node(new GobjectImplNode(member));
} else {
//If there uis no such property return a wrapped NullNode
return Node(0,this,name);
}
} else {
return Node(0,this,name); //This is probably dead code, just there in case.
}
}
size_t GobjectImplNode::size() const {
//Anything not an array has a zero size.
if (!mNode) {
return 0;
}
switch (json_node_get_node_type(mNode)) {
case JSON_NODE_OBJECT: return 0;
case JSON_NODE_ARRAY: break;
case JSON_NODE_VALUE: return 0;
case JSON_NODE_NULL: return 0;
}
JsonArray *asarray=json_node_get_array(mNode);
if (asarray) {
return json_array_get_length(asarray); //Return the array length
} else {
return 0; //This is probably dead code, just there in case.
}
}
Node GobjectImplNode::operator[](size_t index) const {
//Calling this operator on a non array returns a wrapped NullNode.
if (!mNode) {
return Node(0,this,index);
}
switch (json_node_get_node_type(mNode)) {
case JSON_NODE_OBJECT: return Node(0,this,index);
case JSON_NODE_ARRAY: break;
case JSON_NODE_VALUE: return Node(0,this,index);
case JSON_NODE_NULL: return Node(0,this,index);
}
JsonArray *asarray=json_node_get_array(mNode);
if (asarray) {
//Fetch the indexed entity.
JsonNode *member=json_array_get_element(asarray,index);
if (member) {
return Node(new GobjectImplNode(member)); //return the sub node.
} else {
return Node(0,this,index); //Or a wraped NullNode if we can't find it.
}
} else {
return Node(0,this,index); //This is probably dead code, just there in case.
}
}
GobjectImplNode::operator Scalar() const {
//Only scalar type node's can be cast to a scalar. Other node's return a wrapped NullScalar.
if (!mNode) {
return Scalar(this);
}
if (this->nodetype() == jsonme::SCALAR) {
return Scalar(new GobjectImplScalar(mNode));
} else {
return Scalar(this);
}
}
}
}