-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
76 lines (73 loc) · 1.99 KB
/
main.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include "briefJson.h"
static void print(json_object *data, int n,wchar_t *key)
{
for (int i = 0; i < n; ++i)
wprintf(L" |");
wprintf(L"-");
if(key) wprintf(L"key:%ls\t", key);
switch (data->type)
{
case TABLE:
{
wprintf(L"type:map\n");
json_object *item = (json_object *)data->value.item;
while (item)
{
print(item, n + 1, item->key);
item = item->next;
}
return;
}
case ARRAY:
{
wprintf(L"type:array\n");
json_object *item = (json_object *)data->value.item;
while (item)
{
print(item, n + 1, 0);
item = item->next;
}
return;
}
case BOOLEAN:
{
wprintf(L"type:bool\tvalue:%ls\n",data->value.boolean?L"true":L"false");
break;
}
case DECIMAL:
{
wprintf(L"type:decimal\tvalue:%lf\n", data->value.decimal);
break;
}
case INTEGER:
{
wprintf(L"type:integer\tvalue:%lld\n", data->value.integer);
break;
}
case TEXT:
{
wprintf(L"type:text\tvalue:%ls\n", data->value.text);
break;
}
case NONE:
{
wprintf(L"type:null\n");
break;
}
default:
break;
}
}
int main()
{
wchar_t json[] = L"{\"programmers\":[{\"firstName\":\"Brett\",\"lastName\":\"McLaughlin\",\"email\":\"aaaa\",\"age\":3.3},{\"firstName\":\"Jason\",\"lastName\":\"Hunter\",\"email\":\"bbbb\",\"age\":25},{\"firstName\":\"Elliotte\",\"lastName\":\"Harold\",\"email\":\"cccc\",\"age\":30}],\"authors\":[{\"firstName\":\"Isaac\",\"lastName\":\"Asimov\",\"genre\":\"sciencefiction\",\"age\":53},{\"firstName\":\"Tad\",\"lastName\":\"Williams\",\"genre\":\"fantasy\",\"age\":47},{\"firstName\":\"Frank\",\"lastName\":\"Peretti\",\"genre\":\"christianfiction\",\"age\":28}],\"musicians\":[{\"firstName\":\"Eric\",\"lastName\":\"Clapton\",\"instrument\":\"guitar\",\"age\":19},{\"firstName\":\"Sergei\",\"lastName\":\"Rachmaninoff\",\"instrument\":\"piano\",\"age\":26}]}";
json_object item = json_parse(json,0,0);
print(&item, 0, 0);
wprintf(L"\n");
wchar_t *text = json_serialize(&item);
wprintf(L"%ls\n", text);
json_object_free(&item);
json_text_free(text);
return 0;
}