-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_helpers.cpp
More file actions
133 lines (123 loc) · 3.58 KB
/
json_helpers.cpp
File metadata and controls
133 lines (123 loc) · 3.58 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "stdafx.h"
#include "json_helpers.h"
#include "entities.h"
void assert_is_object(json_t *element) {
if (!json_is_object(element)) {
parser_exception ex;
ex << "Object assertion failed.";
throw ex;
}
}
void assert_is_array(json_t *element) {
if (!json_is_array(element)) {
parser_exception ex;
ex << "Array assertion failed.";
throw ex;
}
}
pfc::string8 JSONString(json_t *element) {
pfc::string8 s = "";
if (json_is_string(element)) {
s = decode_html_entities_utf8(json_string_value(element));
}
return s;
}
pfc::string8 JSONAttributeString(json_t *element, const char* attribute) {
pfc::string8 s = "";
if (json_is_object(element)) {
json_t *attr = json_object_get(element, attribute);
if (json_is_string(attr)) {
s = JSONString(attr);
}
// this allows us to read "id" attributes into strings
else if (json_is_integer(attr)) {
s << json_integer_value(attr);
}
// and ratings
else if (json_is_number(attr)) {
s << json_number_value(attr);
if (s.find_first('.') != pfc::infinite_size) {
s = rtrim(s, "0");
}
if (s[s.length() - 1] == '.') {
s += "0";
}
}
}
return s;
}
pfc::array_t<pfc::string8> JSONAttributeStringArray(json_t *root, const char* attribute) {
pfc::array_t<pfc::string8> v;
json_t *array = json_object_get(root, attribute);
if (json_is_array(array)) {
for (unsigned int i = 0; i < json_array_size(array); i++) {
json_t *s = json_array_get(array, i);
pfc::string8 ss = JSONString(s);
if (ss.get_length() != 0) {
v.append_single(ss);
}
}
}
return v;
}
pfc::string8 JSONAttributeObjectAttributeString(json_t *root, const char *object, const char *key) {
json_t *obj = json_object_get(root, object);
if (json_is_object(obj)) {
json_t *s = json_object_get(obj, key);
return JSONString(s);
}
return "";
}
unsigned int JSONAttributeObjectAttributeInt(json_t *root, const char *object, const char *key) {
json_t *obj = json_object_get(root, object);
if (json_is_object(obj)) {
json_t *s = json_object_get(obj, key);
if (json_is_integer(s)) {
return (unsigned int)json_integer_value(s);
}
}
return pfc::infinite_size;
}
pfc::string8 JSONAttributeObjectAttributeObjectAttributeString(json_t *root, const char *object, const char *object2, const char *attribute) {
json_t *obj = json_object_get(root, object);
if (json_is_object(obj)) {
json_t *obj2 = json_object_get(obj, object2);
if (json_is_object(obj2)) {
json_t *s = json_object_get(obj2, attribute);
return JSONString(s);
}
}
return "";
}
pfc::array_t<pfc::string8> JSONAttributeObjectArrayAttributeString(json_t *root, const char* object, const char* attribute) {
pfc::array_t<pfc::string8> v;
json_t *array = json_object_get(root, object);
if (json_is_array(array)) {
for (unsigned int i = 0; i < json_array_size(array); i++) {
json_t *obj = json_array_get(array, i);
if (json_is_object(obj)) {
json_t *s = json_object_get(obj, attribute);
pfc::string8 ss = JSONString(s);
if (ss.get_length() != 0) {
v.append_single(ss);
}
}
}
}
return v;
}
pfc::string8 JSONAttributeObjectArrayAttributeStringWhere(json_t *root, const char* object, const char* attribute, const char* where_attr, const char* where_value) {
json_t *array = json_object_get(root, object);
if (json_is_array(array)) {
for (unsigned int i = 0; i < json_array_size(array); i++) {
json_t *obj = json_array_get(array, i);
if (json_is_object(obj)) {
json_t *s = json_object_get(obj, where_attr);
if (STR_EQUAL(JSONString(s), where_value)) {
return JSONString(json_object_get(obj, attribute));
}
}
}
}
return "";
}