Skip to content

Commit c81f2ae

Browse files
committed
Improve usability
1 parent 6b8511a commit c81f2ae

File tree

1 file changed

+160
-9
lines changed

1 file changed

+160
-9
lines changed

cpp/include/jsonlite2.hpp

Lines changed: 160 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ namespace jsonlite2
119119
} m_d;
120120

121121
static inline jsonValue p_parse(const char * & it, const char * end);
122-
std::string p_dump(std::size_t depth, bool nonobj = false) const;
122+
inline std::string p_dump(std::size_t depth, bool nonobj = false) const;
123123

124124
public:
125125
jsonValue() noexcept = default;
@@ -168,6 +168,14 @@ namespace jsonlite2
168168
}
169169
return *this->m_d.string;
170170
}
171+
const std::string & getString() const
172+
{
173+
if (this->m_type != type::string)
174+
{
175+
throw std::runtime_error("Invalid JSON type!");
176+
}
177+
return *this->m_d.string;
178+
}
171179
bool & getBoolean()
172180
{
173181
if (this->m_type != type::boolean)
@@ -176,6 +184,14 @@ namespace jsonlite2
176184
}
177185
return this->m_d.boolean;
178186
}
187+
const bool & getBoolean() const
188+
{
189+
if (this->m_type != type::boolean)
190+
{
191+
throw std::runtime_error("Invalid JSON type!");
192+
}
193+
return this->m_d.boolean;
194+
}
179195
double & getNumber()
180196
{
181197
if (this->m_type != type::number)
@@ -184,6 +200,14 @@ namespace jsonlite2
184200
}
185201
return this->m_d.number;
186202
}
203+
const double & getNumber() const
204+
{
205+
if (this->m_type != type::number)
206+
{
207+
throw std::runtime_error("Invalid JSON type!");
208+
}
209+
return this->m_d.number;
210+
}
187211
jsonArray & getArray()
188212
{
189213
if (this->m_type != type::array)
@@ -192,6 +216,14 @@ namespace jsonlite2
192216
}
193217
return *this->m_d.array;
194218
}
219+
const jsonArray & getArray() const
220+
{
221+
if (this->m_type != type::array)
222+
{
223+
throw std::runtime_error("Invalid JSON type!");
224+
}
225+
return *this->m_d.array;
226+
}
195227
jsonObject & getObject()
196228
{
197229
if (this->m_type != type::object)
@@ -200,6 +232,24 @@ namespace jsonlite2
200232
}
201233
return *this->m_d.object;
202234
}
235+
const jsonObject & getObject() const
236+
{
237+
if (this->m_type != type::object)
238+
{
239+
throw std::runtime_error("Invalid JSON type!");
240+
}
241+
return *this->m_d.object;
242+
}
243+
244+
jsonKeyValue & operator[](const std::string & key);
245+
const jsonKeyValue & operator[](const std::string & key) const;
246+
jsonKeyValue & at(const std::string & key);
247+
const jsonKeyValue & at(const std::string & key) const;
248+
249+
jsonValue & operator[](std::size_t idx);
250+
const jsonValue & operator[](std::size_t idx) const;
251+
jsonValue & at(std::size_t idx);
252+
const jsonValue & at(std::size_t idx) const;
203253

204254
std::string dump() const
205255
{
@@ -216,10 +266,27 @@ namespace jsonlite2
216266
std::vector<jsonValue> m_vals;
217267

218268
static inline jsonArray p_parse(const char * & it, const char * end);
219-
std::string p_dump(std::size_t depth) const;
269+
inline std::string p_dump(std::size_t depth) const;
220270

221271
public:
222272

273+
jsonValue & operator[](std::size_t idx) noexcept
274+
{
275+
return this->m_vals[idx];
276+
}
277+
const jsonValue & operator[](std::size_t idx) const noexcept
278+
{
279+
return this->m_vals[idx];
280+
}
281+
jsonValue & at(std::size_t idx)
282+
{
283+
return this->m_vals.at(idx);
284+
}
285+
const jsonValue & at(std::size_t idx) const
286+
{
287+
return this->m_vals.at(idx);
288+
}
289+
223290
std::string dump() const
224291
{
225292
return this->p_dump(0);
@@ -237,7 +304,7 @@ namespace jsonlite2
237304
bool m_empty{ true };
238305

239306
static inline jsonKeyValue p_parse(const char * & it, const char * end);
240-
std::string p_dump(std::size_t depth) const;
307+
inline std::string p_dump(std::size_t depth) const;
241308

242309
public:
243310
jsonKeyValue() noexcept = default;
@@ -277,6 +344,14 @@ namespace jsonlite2
277344
return this->m_value;
278345
}
279346

347+
jsonValue * operator->()
348+
{
349+
return &this->m_value;
350+
}
351+
constexpr const jsonValue * operator->() const noexcept
352+
{
353+
return &this->m_value;
354+
}
280355

281356
std::string dump() const
282357
{
@@ -294,7 +369,7 @@ namespace jsonlite2
294369
std::unordered_map<std::string, std::size_t> m_map;
295370

296371
static inline jsonObject p_parse(const char * & it, const char * end);
297-
std::string p_dump(std::size_t depth) const;
372+
inline std::string p_dump(std::size_t depth) const;
298373

299374
public:
300375

@@ -455,6 +530,40 @@ namespace jsonlite2
455530
this->m_type = type::null;
456531
}
457532

533+
inline jsonKeyValue & jsonValue::operator[](const std::string & key)
534+
{
535+
return this->getObject()[key];
536+
}
537+
inline const jsonKeyValue & jsonValue::operator[](const std::string & key) const
538+
{
539+
return this->getObject()[key];
540+
}
541+
inline jsonKeyValue & jsonValue::at(const std::string & key)
542+
{
543+
return this->getObject().at(key);
544+
}
545+
inline const jsonKeyValue & jsonValue::at(const std::string & key) const
546+
{
547+
return this->getObject().at(key);
548+
}
549+
550+
inline jsonValue & jsonValue::operator[](std::size_t idx)
551+
{
552+
return this->getArray()[idx];
553+
}
554+
inline const jsonValue & jsonValue::operator[](std::size_t idx) const
555+
{
556+
return this->getArray()[idx];
557+
}
558+
inline jsonValue & jsonValue::at(std::size_t idx)
559+
{
560+
return this->getArray().at(idx);
561+
}
562+
inline const jsonValue & jsonValue::at(std::size_t idx) const
563+
{
564+
return this->getArray().at(idx);
565+
}
566+
458567
// Value parsing
459568

460569
inline jsonArray jsonArray::p_parse(const char * & it, const char * end)
@@ -729,7 +838,7 @@ namespace jsonlite2
729838
return obj;
730839
}
731840

732-
std::string jsonArray::p_dump(std::size_t depth) const
841+
inline std::string jsonArray::p_dump(std::size_t depth) const
733842
{
734843
std::string out(depth, '\t');
735844
out += "[\n";
@@ -749,7 +858,7 @@ namespace jsonlite2
749858

750859
return out;
751860
}
752-
std::string jsonValue::p_dump(std::size_t depth, bool nonobj) const
861+
inline std::string jsonValue::p_dump(std::size_t depth, bool nonobj) const
753862
{
754863
switch (this->m_type)
755864
{
@@ -780,7 +889,7 @@ namespace jsonlite2
780889
case type::number:
781890
{
782891
char temp[MAX_NUMBERLEN];
783-
auto stringLen = sprintf(temp, "%.15g", this->m_d.number);
892+
auto stringLen = sprintf_s(temp, MAX_NUMBERLEN, "%.15g", this->m_d.number);
784893

785894
if (stringLen <= 0)
786895
{
@@ -796,7 +905,7 @@ namespace jsonlite2
796905
}
797906
}
798907
}
799-
std::string jsonKeyValue::p_dump(std::size_t depth) const
908+
inline std::string jsonKeyValue::p_dump(std::size_t depth) const
800909
{
801910
std::string out(depth, '\t');
802911

@@ -818,7 +927,7 @@ namespace jsonlite2
818927

819928
return out;
820929
}
821-
std::string jsonObject::p_dump(std::size_t depth) const
930+
inline std::string jsonObject::p_dump(std::size_t depth) const
822931
{
823932
std::string out(depth, '\t');
824933

@@ -942,6 +1051,48 @@ namespace jsonlite2
9421051
return this->m_value;
9431052
}
9441053

1054+
constexpr const jsonValue * operator->() const noexcept
1055+
{
1056+
return &this->m_value;
1057+
}
1058+
jsonValue * operator->()
1059+
{
1060+
return &this->m_value;
1061+
}
1062+
1063+
jsonKeyValue & operator[](const std::string & key)
1064+
{
1065+
return this->get()[key];
1066+
}
1067+
const jsonKeyValue & operator[](const std::string & key) const
1068+
{
1069+
return this->get()[key];
1070+
}
1071+
jsonKeyValue & at(const std::string & key)
1072+
{
1073+
return this->get().at(key);
1074+
}
1075+
const jsonKeyValue & at(const std::string & key) const
1076+
{
1077+
return this->get().at(key);
1078+
}
1079+
1080+
jsonValue & operator[](std::size_t idx)
1081+
{
1082+
return this->get()[idx];
1083+
}
1084+
const jsonValue & operator[](std::size_t idx) const
1085+
{
1086+
return this->get()[idx];
1087+
}
1088+
jsonValue & at(std::size_t idx)
1089+
{
1090+
return this->get()[idx];
1091+
}
1092+
const jsonValue & at(std::size_t idx) const
1093+
{
1094+
return this->get()[idx];
1095+
}
9451096
};
9461097

9471098
// Value checking

0 commit comments

Comments
 (0)