FreeRDP
Loading...
Searching...
No Matches
TestJson.c
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include <winpr/crt.h>
6#include <winpr/file.h>
7#include <winpr/json.h>
8#include <winpr/path.h>
9
10int TestJson(int argc, char* argv[])
11{
12 static const char jsonData[] = "{\r\n \"value\": \"test\"\r\n}\r\n";
13 int rc = -1;
14 FILE* fp = nullptr;
15 WINPR_JSON* json = nullptr;
16 char* path = nullptr;
17
18 WINPR_UNUSED(argc);
19 WINPR_UNUSED(argv);
20
21 path = GetCombinedPath(TEST_BINARY_PATH, "TestJson-crlf.json");
22 if (!path)
23 goto fail;
24
25 fp = winpr_fopen(path, "wb");
26 if (!fp)
27 goto fail;
28
29 if (fwrite(jsonData, sizeof(char), sizeof(jsonData) - 1, fp) != sizeof(jsonData) - 1)
30 goto fail;
31
32 if (fclose(fp) != 0)
33 goto fail;
34 fp = nullptr;
35
36 json = WINPR_JSON_ParseFromFile(path);
37 if (!json)
38 goto fail;
39
40 const WINPR_JSON* value = WINPR_JSON_GetObjectItem(json, "value");
41 if (!WINPR_JSON_IsString(value))
42 goto fail;
43
44 const char* str = WINPR_JSON_GetStringValue(value);
45 if (!str || (strcmp(str, "test") != 0))
46 goto fail;
47
48 rc = 0;
49
50fail:
51 if (fp)
52 (void)fclose(fp);
54 if (path)
55 (void)winpr_DeleteFile(path);
56 free(path);
57 return rc;
58}
WINPR_API WINPR_JSON * WINPR_JSON_ParseFromFile(const char *filename)
Parse a JSON string read from a file filename.
Definition json.c:27
WINPR_ATTR_NODISCARD WINPR_API WINPR_JSON * WINPR_JSON_GetObjectItem(const WINPR_JSON *object, const char *string)
Return a pointer to an JSON object item.
Definition c-json.c:122
WINPR_ATTR_NODISCARD WINPR_API BOOL WINPR_JSON_IsString(const WINPR_JSON *item)
Check if JSON item is of type String.
Definition c-json.c:182
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
Definition c-json.c:103
WINPR_ATTR_NODISCARD WINPR_API const char * WINPR_JSON_GetStringValue(WINPR_JSON *item)
Return the String value of a JSON item.
Definition c-json.c:142