5 #include <initializer_list>
31 JsonValue(picojson::value val) : v(std::move(val)) {}
118 picojson::value&
getValue() {
return isRef ? ref.get() : v; }
123 const picojson::value&
getValue()
const {
return isRef ? ref.get() : v; }
130 operator double()
const {
return get<double>(); }
146 operator float()
const {
return static_cast<float>(get<double>()); }
153 set(
static_cast<double>(f));
162 operator int()
const {
return static_cast<int>(get<double>()); }
169 set(
static_cast<double>(i));
178 operator std::string()
const {
return get<std::string>(); }
203 operator bool()
const {
return get<bool>(); }
232 operator JsonArray()
const;
266 std::reference_wrapper<picojson::value> ref;
288 JsonObject(picojson::object obj) : obj(std::move(obj)) {}
303 JsonValue operator[](
const std::string& key) {
319 JsonValue operator[](
const std::string& key)
const {
return obj.at(key); }
324 picojson::object& getObject() {
return obj; }
329 const picojson::object& getObject()
const {
return obj; }
335 std::string toString()
const {
return picojson::value(obj).serialize(); }
342 bool contains(
const std::string& key)
const {
343 return obj.find(key) != obj.end();
349 std::vector<std::string> getKeys()
const;
352 picojson::object obj;
365 JsonArray() =
default;
371 JsonArray(picojson::array arr) : arr(std::move(arr)) {}
381 JsonArray(
const std::initializer_list<JsonValue> ls) {
382 for (
const auto& val : ls) push(val);
389 explicit JsonArray(
int size) : arr(size) {}
394 picojson::array& getArray() {
return arr; }
399 const picojson::array& getArray()
const {
return arr; }
405 std::string toString()
const {
return picojson::value(arr).serialize(); }
437 JsonValue operator[](
int idx)
const {
return arr.at(idx); }
446 JsonValue at(
int idx)
const {
return arr.at(idx); }
462 int size()
const {
return arr.size(); }
468 void resize(
int size) { arr.resize(size); }
490 if (
this != &other) {
506 v = std::move(other.v);
515 v = std::move(other.v);
532 inline JsonValue::operator
JsonObject()
const {
533 return {get<picojson::object>()};
541 inline JsonValue::operator JsonArray()
const {
542 return {get<picojson::array>()};
549 inline std::vector<std::string> JsonObject::getKeys()
const {
550 std::vector<std::string> keys;
551 for (
const auto& kv : obj) {
552 keys.push_back(kv.first);
560 inline std::ostream& operator<<(std::ostream& os,
const JsonValue& val) {
568 inline std::ostream& operator<<(std::ostream& os,
const JsonObject& obj) {
569 os << obj.toString();
576 inline std::ostream& operator<<(std::ostream& os,
const JsonArray& array) {
577 os << array.toString();
Manages a picojson::object, works with JsonValue to provide implicit casting.
Manages a picojson::value, provides implicit casting for valid JSON types.
Definition: json.h:20
JsonValue(const JsonArray &a)
Create a JsonValue from a JsonArray.
JsonValue(double d)
Create a JsonValue from a double.
Definition: json.h:37
JsonValue(picojson::value val)
Create a JsonValue from an existing picojson::value (creates a copy)
Definition: json.h:31
picojson::value & getValue()
Definition: json.h:118
JsonValue(bool b)
Create a JsonValue from a bool.
Definition: json.h:61
static JsonValue fromReference(picojson::value &v)
Create a a JsonValue using a reference to an existing picojson::value. Instead of creating a copy of ...
JsonValue(const char *s)
Create a JsonValue from a string literal.
Definition: json.h:55
JsonValue(int i)
Create a JsonValue from an int.
Definition: json.h:43
JsonValue(JsonValue &&other)
Move constructor.
JsonValue & operator=(float f)
set this value to a float
Definition: json.h:152
JsonValue & operator=(const JsonValue &other)
Copy operator.
void set(const T &t)
Sets the value from the specific template.
Definition: json.h:260
const picojson::value & getValue() const
Definition: json.h:123
JsonValue & operator=(bool b)
set this value to a bool
Definition: json.h:209
JsonValue(const JsonObject &o)
Create a JsonValue from a JsonObject.
JsonValue & operator=(const JsonObject &o)
set this value to a JsonObject
JsonValue & operator=(JsonValue &&other) noexcept
Move operator.
JsonValue(const std::string &s)
Create a JsonValue from a string.
Definition: json.h:49
JsonValue & operator=(const std::string &s)
set this value to a string
Definition: json.h:184
T get() const
Gets the value from the specific template.
Definition: json.h:251
std::string toString() const
Serialize this to valid JSON text.
Definition: json.h:244
JsonValue()
Default constructor.
Definition: json.h:25
JsonValue & operator=(const char *s)
set this value to a string
Definition: json.h:193
JsonValue & operator=(int i)
set this value to an int
Definition: json.h:168
JsonValue(const JsonValue &other)
Copy constructor.
JsonValue & operator=(double d)
set this value to a double
Definition: json.h:136
JsonValue & operator=(const JsonArray &a)
set this value to a JsonArray