forked from gregjesl/simpleson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty_object.cpp
60 lines (54 loc) · 1.99 KB
/
pretty_object.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "json.h"
#include "test.h"
#include <math.h>
int main(void)
{
const char *input =
"{\n"
"\t\"number\": 123.456,\n"
"\t\"string\": \"hello \\\" world\",\n"
"\t\"array\": [\n"
"\t\t1,\n"
"\t\t2,\n"
"\t\t3\n"
"\t],\n"
"\t\"boolean\": true,\n"
"\t\"isnull\": null,\n"
"\t\"objarray\": [\n"
"\t\t{\n"
"\t\t\t\"key\": \"value\"\n"
"\t\t}\n"
"\t],\n"
"\t\"strarray\": [\n"
"\t\t\"hello\",\n"
"\t\t\"world\"\n"
"\t],\n"
"\t\"emptyarray\": []\n"
"}";
printf("Input: \n%s\n", input);
json::jobject result = json::jobject::parse(input);
TEST_FALSE(result.is_array());
TEST_STRING_EQUAL(result.get("number").c_str(), "123.456");
TEST_STRING_EQUAL(result.get("string").c_str(), "\"hello \\\" world\"");
TEST_STRING_EQUAL(result.get("array").c_str(), "[1,2,3]");
TEST_STRING_EQUAL(result.get("boolean").c_str(), "true");
TEST_STRING_EQUAL(result.get("isnull").c_str(), "null");
TEST_STRING_EQUAL(result.get("objarray").c_str(), "[{\"key\":\"value\"}]");
TEST_STRING_EQUAL(result.get("strarray").c_str(), "[\"hello\",\"world\"]");
TEST_STRING_EQUAL(result.get("emptyarray").c_str(), "[]");
TEST_TRUE(result.has_key("number"));
TEST_FALSE(result.has_key("nokey"));
TEST_STRING_EQUAL(result["objarray"].array(0).get("key").as_string().c_str(), "value");
std::vector<std::string> strarray = result["strarray"];
TEST_EQUAL(strarray.size(), 2);
TEST_STRING_EQUAL(strarray[0].c_str(), "hello");
TEST_STRING_EQUAL(strarray[1].c_str(), "world");
std::vector<std::string> emptyarray = result["emptyarray"];
TEST_EQUAL(emptyarray.size(), 0);
printf("Output\n%s\n", result.pretty().c_str());
// Test serialization
TEST_STRING_EQUAL(result.pretty().c_str(), input);
// Test copy constructor
json::jobject copy(result);
TEST_STRING_EQUAL(copy.as_string().c_str(), result.as_string().c_str());
}