LIPH's C++ Codes
json.h
Go to the documentation of this file.
1#ifndef LIPH_JSON_H_
2#define LIPH_JSON_H_
3
4#include <filesystem>
5#include <string>
6#include <unordered_map>
7#include <vector>
8
9namespace liph {
10
11class json {
12public:
13 // number: double or int64
14 // boolean literal: "true" or "false"
15 // null literal: "null"
17
18 // no explicit
20 json(bool b);
21 json(double d);
22 json(int32_t n);
23 json(int64_t n);
24 json(std::string str);
25 json(const char *str); // null type if str is nullptr
26 json(const std::vector<json>& arr);
27 json(const std::unordered_map<std::string, json>& obj);
28
29 json(const json& other);
30 json(json&& other);
31 json& operator=(const json& other);
32 json& operator=(json&& other);
33 ~json();
34
35 value_type type() const;
36
37 // for object, array and string, become empty
38 void clear();
39
40 // become null
41 void reset();
42
43 static json parse(const std::string& str);
44 static json load(const std::filesystem::path& path);
45 std::string to_string(int indent = 0, bool sort_keys = false) const;
46
47 bool operator==(const json& other) const;
48 bool operator!=(const json& other) const;
49
50 // for object
51 json& at(const std::string& key);
52 const json& at(const std::string& key) const;
53 json& operator[](const std::string& key); // if null, become object
54 const json& operator[](const std::string& key) const; // same as at()
55
56 // for array
57 json& at(std::size_t pos);
58 const json& at(std::size_t pos) const;
59 json& operator[](std::size_t pos); // if null, become array
60 const json& operator[](std::size_t pos) const; // same as at()
61
62 bool& bool_ref();
63 bool bool_ref() const;
64 double& double_ref();
65 double double_ref() const;
66 int64_t& i64_ref();
67 int64_t i64_ref() const;
68 std::string& string_ref();
69 const std::string& string_ref() const;
70 std::vector<json>& array_ref();
71 const std::vector<json>& array_ref() const;
72 std::unordered_map<std::string, json>& object_ref();
73 const std::unordered_map<std::string, json>& object_ref() const;
74
75 bool is_null() const;
76 bool is_boolean() const;
77 bool is_number() const;
78 bool is_string() const;
79 bool is_array() const;
80 bool is_object() const;
81
82private:
83 value_type type_{null};
84 bool is_double_{false};
85 union {
86 bool bool_;
87 double double_;
88 int64_t i64_;
89 std::string *string_;
90 std::vector<json> *array_;
91 std::unordered_map<std::string, json> *object_;
92 };
93};
94
95} // namespace liph
96
97static_assert(sizeof(int64_t) == sizeof(double));
98static_assert(sizeof(liph::json) == 16); // alignment
99
100#endif // LIPH_JSON_H_
Definition: json.h:11
json & operator[](const std::string &key)
Definition: json.cpp:436
json & at(const std::string &key)
Definition: json.cpp:434
std::string to_string(int indent=0, bool sort_keys=false) const
Definition: json.cpp:539
~json()
Definition: json.cpp:373
std::vector< json > & array_ref()
Definition: json.cpp:585
int64_t i64_
Definition: json.h:88
std::unordered_map< std::string, json > * object_
Definition: json.h:91
json(value_type type=null)
Definition: json.cpp:221
void reset()
Definition: json.cpp:393
bool is_string() const
Definition: json.cpp:602
value_type
Definition: json.h:16
@ array
Definition: json.h:16
@ boolean
Definition: json.h:16
@ string
Definition: json.h:16
@ object
Definition: json.h:16
@ null
Definition: json.h:16
@ number
Definition: json.h:16
json & operator=(const json &other)
Definition: json.cpp:313
std::string & string_ref()
Definition: json.cpp:578
bool is_boolean() const
Definition: json.cpp:600
bool operator==(const json &other) const
Definition: json.cpp:462
double & double_ref()
Definition: json.cpp:564
bool operator!=(const json &other) const
Definition: json.cpp:477
double double_
Definition: json.h:87
std::unordered_map< std::string, json > & object_ref()
Definition: json.cpp:592
bool is_array() const
Definition: json.cpp:603
std::string * string_
Definition: json.h:89
std::vector< json > * array_
Definition: json.h:90
static json parse(const std::string &str)
Definition: json.cpp:412
value_type type() const
Definition: json.cpp:375
bool bool_
Definition: json.h:86
bool is_null() const
Definition: json.cpp:599
static json load(const std::filesystem::path &path)
Definition: json.cpp:421
bool is_number() const
Definition: json.cpp:601
bool is_object() const
Definition: json.cpp:604
bool & bool_ref()
Definition: json.cpp:557
void clear()
Definition: json.cpp:377
int64_t & i64_ref()
Definition: json.cpp:571
Definition: algorithm.h:10