• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <iostream>
2 #include <deque>
3 #include <list>
4 #include <forward_list>
5 #include <set>
6 #include <unordered_map>
7 #include <unordered_set>
8 #include <valarray>
9 #include <nlohmann/json.hpp>
10 
11 using json = nlohmann::json;
12 
main()13 int main()
14 {
15     // ============
16     // object types
17     // ============
18 
19     // create an object from an object_t value
20     json::object_t object_value = { {"one", 1}, {"two", 2} };
21     json j_object_t(object_value);
22 
23     // create an object from std::map
24     std::map<std::string, int> c_map
25     {
26         {"one", 1}, {"two", 2}, {"three", 3}
27     };
28     json j_map(c_map);
29 
30     // create an object from std::unordered_map
31     std::unordered_map<const char*, double> c_umap
32     {
33         {"one", 1.2}, {"two", 2.3}, {"three", 3.4}
34     };
35     json j_umap(c_umap);
36 
37     // create an object from std::multimap
38     std::multimap<std::string, bool> c_mmap
39     {
40         {"one", true}, {"two", true}, {"three", false}, {"three", true}
41     };
42     json j_mmap(c_mmap); // only one entry for key "three" is used
43 
44     // create an object from std::unordered_multimap
45     std::unordered_multimap<std::string, bool> c_ummap
46     {
47         {"one", true}, {"two", true}, {"three", false}, {"three", true}
48     };
49     json j_ummap(c_ummap); // only one entry for key "three" is used
50 
51     // serialize the JSON objects
52     std::cout << j_object_t << '\n';
53     std::cout << j_map << '\n';
54     std::cout << j_umap << '\n';
55     std::cout << j_mmap << '\n';
56     std::cout << j_ummap << "\n\n";
57 
58 
59     // ===========
60     // array types
61     // ===========
62 
63     // create an array from an array_t value
64     json::array_t array_value = {"one", "two", 3, 4.5, false};
65     json j_array_t(array_value);
66 
67     // create an array from std::vector
68     std::vector<int> c_vector {1, 2, 3, 4};
69     json j_vec(c_vector);
70 
71     // create an array from std::valarray
72     std::valarray<short> c_valarray {10, 9, 8, 7};
73     json j_valarray(c_valarray);
74 
75     // create an array from std::deque
76     std::deque<double> c_deque {1.2, 2.3, 3.4, 5.6};
77     json j_deque(c_deque);
78 
79     // create an array from std::list
80     std::list<bool> c_list {true, true, false, true};
81     json j_list(c_list);
82 
83     // create an array from std::forward_list
84     std::forward_list<std::int64_t> c_flist {12345678909876, 23456789098765, 34567890987654, 45678909876543};
85     json j_flist(c_flist);
86 
87     // create an array from std::array
88     std::array<unsigned long, 4> c_array {{1, 2, 3, 4}};
89     json j_array(c_array);
90 
91     // create an array from std::set
92     std::set<std::string> c_set {"one", "two", "three", "four", "one"};
93     json j_set(c_set); // only one entry for "one" is used
94 
95     // create an array from std::unordered_set
96     std::unordered_set<std::string> c_uset {"one", "two", "three", "four", "one"};
97     json j_uset(c_uset); // only one entry for "one" is used
98 
99     // create an array from std::multiset
100     std::multiset<std::string> c_mset {"one", "two", "one", "four"};
101     json j_mset(c_mset); // both entries for "one" are used
102 
103     // create an array from std::unordered_multiset
104     std::unordered_multiset<std::string> c_umset {"one", "two", "one", "four"};
105     json j_umset(c_umset); // both entries for "one" are used
106 
107     // serialize the JSON arrays
108     std::cout << j_array_t << '\n';
109     std::cout << j_vec << '\n';
110     std::cout << j_valarray << '\n';
111     std::cout << j_deque << '\n';
112     std::cout << j_list << '\n';
113     std::cout << j_flist << '\n';
114     std::cout << j_array << '\n';
115     std::cout << j_set << '\n';
116     std::cout << j_uset << '\n';
117     std::cout << j_mset << '\n';
118     std::cout << j_umset << "\n\n";
119 
120 
121     // ============
122     // string types
123     // ============
124 
125     // create string from a string_t value
126     json::string_t string_value = "The quick brown fox jumps over the lazy dog.";
127     json j_string_t(string_value);
128 
129     // create a JSON string directly from a string literal
130     json j_string_literal("The quick brown fox jumps over the lazy dog.");
131 
132     // create string from std::string
133     std::string s_stdstring = "The quick brown fox jumps over the lazy dog.";
134     json j_stdstring(s_stdstring);
135 
136     // serialize the JSON strings
137     std::cout << j_string_t << '\n';
138     std::cout << j_string_literal << '\n';
139     std::cout << j_stdstring << "\n\n";
140 
141 
142     // ============
143     // number types
144     // ============
145 
146     // create a JSON number from number_integer_t
147     json::number_integer_t value_integer_t = -42;
148     json j_integer_t(value_integer_t);
149 
150     // create a JSON number from number_unsigned_t
151     json::number_integer_t value_unsigned_t = 17;
152     json j_unsigned_t(value_unsigned_t);
153 
154     // create a JSON number from an anonymous enum
155     enum { enum_value = 17 };
156     json j_enum(enum_value);
157 
158     // create values of different integer types
159     short n_short = 42;
160     int n_int = -23;
161     long n_long = 1024;
162     int_least32_t n_int_least32_t = -17;
163     uint8_t n_uint8_t = 8;
164 
165     // create (integer) JSON numbers
166     json j_short(n_short);
167     json j_int(n_int);
168     json j_long(n_long);
169     json j_int_least32_t(n_int_least32_t);
170     json j_uint8_t(n_uint8_t);
171 
172     // create values of different floating-point types
173     json::number_float_t v_ok = 3.141592653589793;
174     json::number_float_t v_nan = NAN;
175     json::number_float_t v_infinity = INFINITY;
176 
177     // create values of different floating-point types
178     float n_float = 42.23;
179     float n_float_nan = 1.0f / 0.0f;
180     double n_double = 23.42;
181 
182     // create (floating point) JSON numbers
183     json j_ok(v_ok);
184     json j_nan(v_nan);
185     json j_infinity(v_infinity);
186     json j_float(n_float);
187     json j_float_nan(n_float_nan);
188     json j_double(n_double);
189 
190     // serialize the JSON numbers
191     std::cout << j_integer_t << '\n';
192     std::cout << j_unsigned_t << '\n';
193     std::cout << j_enum << '\n';
194     std::cout << j_short << '\n';
195     std::cout << j_int << '\n';
196     std::cout << j_long << '\n';
197     std::cout << j_int_least32_t << '\n';
198     std::cout << j_uint8_t << '\n';
199     std::cout << j_ok << '\n';
200     std::cout << j_nan << '\n';
201     std::cout << j_infinity << '\n';
202     std::cout << j_float << '\n';
203     std::cout << j_float_nan << '\n';
204     std::cout << j_double << "\n\n";
205 
206 
207     // =============
208     // boolean types
209     // =============
210 
211     // create boolean values
212     json j_truth = true;
213     json j_falsity = false;
214 
215     // serialize the JSON booleans
216     std::cout << j_truth << '\n';
217     std::cout << j_falsity << '\n';
218 }
219