comparison C++/modules/Json/Json.cpp @ 334:0b576ee64d45

* Create brand new hierarchy * Rename DynLib to Dynlib * Remove some warnings
author David Demelier <markand@malikania.fr>
date Sun, 08 Mar 2015 14:26:33 +0100
parents C++/Json.cpp@b67b2da45bb0
children d5ec1174b707
comparison
equal deleted inserted replaced
333:412ca7a5e1ea 334:0b576ee64d45
1 /*
2 * Json.cpp -- jansson C++11 wrapper
3 *
4 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <stdexcept>
20
21 #include "Json.h"
22
23 /* --------------------------------------------------------
24 * JsonObject
25 * -------------------------------------------------------- */
26
27 JsonObject JsonValue::toObject() const noexcept
28 {
29 json_incref(m_handle.get());
30
31 return JsonObject(m_handle.get());
32 }
33
34 JsonArray JsonValue::toArray() const noexcept
35 {
36 json_incref(m_handle.get());
37
38 return JsonArray(m_handle.get());
39 }
40
41 /* --------------------------------------------------------
42 * JsonArray
43 * -------------------------------------------------------- */
44
45 JsonValue JsonArray::at(int index) const
46 {
47 auto value = json_array_get(m_handle.get(), index);
48
49 if (value == nullptr)
50 throw JsonError("index out of bounds");
51
52 json_incref(value);
53
54 return JsonValue{value};
55 }
56
57 JsonValue JsonArray::operator[](int index) const noexcept
58 {
59 auto value = json_array_get(m_handle.get(), index);
60
61 if (value == nullptr)
62 return JsonValue();
63
64 json_incref(value);
65
66 return JsonValue(value);
67 }
68
69 JsonArray::Ref JsonArray::operator[](int index) noexcept
70 {
71 auto value = json_array_get(m_handle.get(), index);
72
73 if (value == nullptr)
74 value = json_null();
75 else
76 json_incref(value);
77
78 return Ref(value, *this, index);
79 }
80
81 /* --------------------------------------------------------
82 * JsonObject
83 * -------------------------------------------------------- */
84
85 JsonObject::Ref JsonObject::operator[](const std::string &name)
86 {
87 if (typeOf() != JsonType::Object)
88 return Ref(JsonValue(), *this, name);
89
90 auto value = json_object_get(m_handle.get(), name.c_str());
91
92 json_incref(value);
93
94 return Ref(value, *this, name);
95 }
96
97 JsonValue JsonObject::operator[](const std::string &name) const
98 {
99 if (typeOf() != JsonType::Object)
100 return JsonValue();
101
102 auto value = json_object_get(m_handle.get(), name.c_str());
103
104 if (value == nullptr)
105 return JsonValue();
106
107 json_incref(value);
108
109 return JsonValue(value);
110 }
111
112 /* --------------------------------------------------------
113 * JsonDocument
114 * -------------------------------------------------------- */
115
116 JsonValue JsonDocument::read(std::string content, int flags) const
117 {
118 json_error_t error;
119 json_t *json = json_loads(content.c_str(), flags, &error);
120
121 if (json == nullptr)
122 throw JsonError(error);
123
124 return JsonValue(json);
125 }
126
127 JsonValue JsonDocument::read(std::ifstream &stream, int flags) const
128 {
129 if (!stream.is_open())
130 throw JsonError("File not opened");
131
132 stream.seekg(0, stream.end);
133 auto length = stream.tellg();
134 stream.seekg(0, stream.beg);
135
136 std::string buffer;
137 buffer.resize(length, ' ');
138
139 stream.read(&buffer[0], length);
140 stream.close();
141
142 return read(std::move(buffer), flags);
143 }
144
145 JsonDocument::JsonDocument(std::ifstream &stream, int flags)
146 {
147 m_value = read(stream, flags);
148 }
149
150 JsonDocument::JsonDocument(std::ifstream &&stream, int flags)
151 {
152 m_value = read(stream, flags);
153 }
154
155 JsonDocument::JsonDocument(std::string content, int flags)
156 {
157 m_value = read(std::move(content), flags);
158 }