-
Notifications
You must be signed in to change notification settings - Fork 14
/
WindowsJson.h
287 lines (235 loc) · 8.4 KB
/
WindowsJson.h
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#pragma once
#include <windows.h>
#include <windows.data.json.h>
#include <stdint.h>
#include <stdbool.h>
// interface
typedef __x_ABI_CWindows_CData_CJson_CIJsonObject JsonObject;
typedef __x_ABI_CWindows_CData_CJson_CIJsonArray JsonArray;
typedef __FIIterator_1___FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue JsonIterator;
#define JsonCSTR(Name) (HSTRING)&(JsonHSTRING){ 1, sizeof(Name) - 1, 0, 0, L ## Name }
#define JsonRelease(Obj) do { if (Obj) IUnknown_Release((IUnknown*)Obj); } while (0)
// NOTE: make sure you have called RoInitialize() before using any of functions
// if zero terminated, can set Length to -1
static JsonObject* JsonObject_Parse(LPCSTR String, int Length);
static JsonObject* JsonObject_ParseW(LPCWSTR String, int Length);
static JsonObject* JsonObject_ParseStr(HSTRING String);
static JsonObject* JsonObject_GetObject (JsonObject* Object, HSTRING Name);
static JsonArray* JsonObject_GetArray (JsonObject* Object, HSTRING Name);
static HSTRING JsonObject_GetString (JsonObject* Object, HSTRING Name);
static double JsonObject_GetNumber (JsonObject* Object, HSTRING Name);
static bool JsonObject_GetBoolean(JsonObject* Object, HSTRING Name);
static JsonIterator* JsonObject_GetIterator(JsonObject* Object);
static HSTRING JsonIterator_GetKey (JsonIterator* Iterator);
static JsonObject* JsonIterator_GetValue(JsonIterator* Iterator);
static bool JsonIterator_Next (JsonIterator* Iterator);
static uint32_t JsonArray_GetCount (JsonArray* Array);
static JsonObject* JsonArray_GetObject (JsonArray* Array, UINT32 Index);
static JsonArray* JsonArray_GetArray (JsonArray* Array, UINT32 Index);
static HSTRING JsonArray_GetString (JsonArray* Array, UINT32 Index);
static double JsonArray_GetNumber (JsonArray* Array, UINT32 Index);
static bool JsonArray_GetBoolean(JsonArray* Array, UINT32 Index);
// implementation
#ifndef Assert
#define Assert(Cond) ((void)(Cond))
#endif
#ifndef HR
#define HR(hr) ((void)(hr))
#endif
typedef struct {
DWORD Flags;
DWORD Length;
DWORD Padding1;
DWORD Padding2;
LPCWSTR Ptr;
} JsonHSTRING;
DEFINE_GUID(IID_IJsonObject, 0x2289f159, 0x54de, 0x45d8, 0xab, 0xcc, 0x22, 0x60, 0x3f, 0xa0, 0x66, 0xa0);
DEFINE_GUID(IID_IVector_IJsonValue, 0xd44662bc, 0xdce3, 0x59a8, 0x92, 0x72, 0x4b, 0x21, 0x0f, 0x33, 0x90, 0x8b);
DEFINE_GUID(IID_IMap_IJsonValue, 0xdfabb6e1, 0x0411, 0x5a8f, 0xaa, 0x87, 0x35, 0x4e, 0x71, 0x10, 0xf0, 0x99);
JsonObject* JsonObject_Parse(LPCSTR String, int Length)
{
if (Length < 0) Length = lstrlenA(String);
int WideLength = MultiByteToWideChar(CP_UTF8, 0, String, Length, NULL, 0);
Assert(WideLength != 0);
WCHAR* Wide = LocalAlloc(0, WideLength * sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, String, Length, Wide, WideLength);
JsonObject* Result = JsonObject_ParseW(Wide, WideLength);
LocalFree(Wide);
return Result;
}
JsonObject* JsonObject_ParseW(LPCWSTR String, int Length)
{
JsonHSTRING WideString = { 1, Length < 0 ? lstrlenW(String) : Length, 0, 0, String };
return JsonObject_ParseStr((HSTRING)&WideString);
}
JsonObject* JsonObject_ParseStr(HSTRING String)
{
__x_ABI_CWindows_CData_CJson_CIJsonObjectStatics* ObjectStatics;
HR(RoGetActivationFactory(JsonCSTR("Windows.Data.Json.JsonObject"), &IID_IJsonObject, (void**)&ObjectStatics));
JsonObject* Object;
HRESULT hr;
if (FAILED(hr = __x_ABI_CWindows_CData_CJson_CIJsonObjectStatics_Parse(ObjectStatics, String, &Object)))
{
Object = NULL;
}
__x_ABI_CWindows_CData_CJson_CIJsonObjectStatics_Release(ObjectStatics);
return Object;
}
JsonObject* JsonObject_GetObject(JsonObject* Object, HSTRING Name)
{
JsonObject* Result;
if (!Object || FAILED(__x_ABI_CWindows_CData_CJson_CIJsonObject_GetNamedObject(Object, Name, &Result)))
{
Result = NULL;
}
return Result;
}
JsonArray* JsonObject_GetArray(JsonObject* Object, HSTRING Name)
{
JsonArray* Result;
if (!Object || FAILED(__x_ABI_CWindows_CData_CJson_CIJsonObject_GetNamedArray(Object, Name, &Result)))
{
Result = NULL;
}
return Result;
}
HSTRING JsonObject_GetString(JsonObject* Object, HSTRING Name)
{
HSTRING Result;
if (!Object || FAILED(__x_ABI_CWindows_CData_CJson_CIJsonObject_GetNamedString(Object, Name, &Result)))
{
Result = NULL;
}
return Result;
}
double JsonObject_GetNumber(JsonObject* Object, HSTRING Name)
{
double Result;
if (!Object || FAILED(__x_ABI_CWindows_CData_CJson_CIJsonObject_GetNamedNumber(Object, Name, &Result)))
{
Result = 0;
}
return Result;
}
bool JsonObject_GetBoolean(JsonObject* Object, HSTRING Name)
{
boolean Result;
if (!Object || FAILED(__x_ABI_CWindows_CData_CJson_CIJsonObject_GetNamedBoolean(Object, Name, &Result)))
{
Result = 0;
}
return !!Result;
}
JsonIterator* JsonObject_GetIterator(JsonObject* Object)
{
__FIIterator_1___FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue* Iterator = NULL;
__FIIterable_1___FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue* Iterable;
if (SUCCEEDED(__x_ABI_CWindows_CData_CJson_CIJsonObject_QueryInterface(Object, &IID_IMap_IJsonValue, (void**)&Iterable)))
{
HR(__FIIterable_1___FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue_First(Iterable, &Iterator));
boolean HasCurrent;
HR(__FIIterator_1___FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue_get_HasCurrent(Iterator, &HasCurrent));
if (!HasCurrent)
{
__FIIterator_1___FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue_Release(Iterator);
Iterator = NULL;
}
__FIIterable_1___FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue_Release(Iterable);
}
return Iterator;
}
HSTRING JsonIterator_GetKey(JsonIterator* Iterator)
{
HSTRING Result = NULL;
__FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue* Current;
if (SUCCEEDED(__FIIterator_1___FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue_get_Current(Iterator, &Current)))
{
HR(__FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue_get_Key(Current, &Result));
__FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue_Release(Current);
}
return Result;
}
JsonObject* JsonIterator_GetValue(JsonIterator* Iterator)
{
JsonObject* Result = NULL;
__FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue* Current;
if (SUCCEEDED(__FIIterator_1___FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue_get_Current(Iterator, &Current)))
{
__x_ABI_CWindows_CData_CJson_CIJsonValue* Value;
HR(__FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue_get_Value(Current, &Value));
__FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue_Release(Current);
HR(__x_ABI_CWindows_CData_CJson_CIJsonValue_GetObject(Value, &Result));
__x_ABI_CWindows_CData_CJson_CIJsonValue_Release(Value);
}
return Result;
}
bool JsonIterator_Next(JsonIterator* Iterator)
{
boolean Result;
if (FAILED(__FIIterator_1___FIKeyValuePair_2_HSTRING_Windows__CData__CJson__CIJsonValue_MoveNext(Iterator, &Result)))
{
Result = 0;
}
return !!Result;
}
uint32_t JsonArray_GetCount(JsonArray* Array)
{
if (!Array)
{
return 0;
}
UINT32 Count;
__FIVector_1_Windows__CData__CJson__CIJsonValue* Vector;
HR(__x_ABI_CWindows_CData_CJson_CIJsonArray_QueryInterface(Array, &IID_IVector_IJsonValue, (void**)&Vector));
if (FAILED(__FIVector_1_Windows__CData__CJson__CIJsonValue_get_Size(Vector, &Count)))
{
Count = 0;
}
__FIVector_1_Windows__CData__CJson__CIJsonValue_Release(Vector);
return Count;
}
JsonObject* JsonArray_GetObject(JsonArray* Array, UINT32 Index)
{
JsonObject* Result;
if (!Array || FAILED(__x_ABI_CWindows_CData_CJson_CIJsonArray_GetObjectAt(Array, Index, &Result)))
{
Result = NULL;
}
return Result;
}
JsonArray* JsonArray_GetArray(JsonArray* Array, UINT32 Index)
{
JsonArray* Result;
if (!Array || FAILED(__x_ABI_CWindows_CData_CJson_CIJsonArray_GetArrayAt(Array, Index, &Result)))
{
Result = NULL;
}
return Result;
}
HSTRING JsonArray_GetString(JsonArray* Array, UINT32 Index)
{
HSTRING Result;
if (!Array || FAILED(__x_ABI_CWindows_CData_CJson_CIJsonArray_GetStringAt(Array, Index, &Result)))
{
Result = NULL;
}
return Result;
}
double JsonArray_GetNumber(JsonArray* Array, UINT32 Index)
{
double Result;
if (!Array || FAILED(__x_ABI_CWindows_CData_CJson_CIJsonArray_GetNumberAt(Array, Index, &Result)))
{
Result = 0;
}
return Result;
}
bool JsonArray_GetBoolean(JsonArray* Array, UINT32 Index)
{
boolean Result;
if (!Array || FAILED(__x_ABI_CWindows_CData_CJson_CIJsonArray_GetBooleanAt(Array, Index, &Result)))
{
Result = 0;
}
return !!Result;
}