forked from HISONA/https_client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parser.c
More file actions
66 lines (57 loc) · 1.85 KB
/
Copy pathjson_parser.c
File metadata and controls
66 lines (57 loc) · 1.85 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
#include "json_parser.h"
#include <stdio.h>
#include <string.h>
#include "cJSON/cJSON.h"
const char *example_json = "{\"month\": \"2\", \"num\": 2430, \"link\": \"\", \"year\": \"2021\", \"news\": \"\", \"safe_title\": \"Post-Pandemic Hat\", \"transcript\": \"\", \"alt\": \"Plus a shirt that says \\\"it feels like you're making eye contact.\\\"\", \"img\": \"https://imgs.xkcd.com/comics/post_pandemic_hat.png\", \"title\": \"Post-Pandemic Hat\", \"day\": \"26\"}";
int json_parser_get_number(const char *json, const char *field, int *out)
{
cJSON *json_obj = cJSON_Parse(json);
if (json_obj == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
printf(stderr, "Error before: %s\n", error_ptr);
}
cJSON_Delete(json_obj);
return -1;
}
cJSON *value = cJSON_GetObjectItemCaseSensitive(json_obj, field);
if (!cJSON_IsNumber(value))
{
cJSON_Delete(json_obj);
return -1;
}
*out = (int)value->valueint;
cJSON_Delete(json_obj);
return 0;
}
int json_parser_get_string(const char *json, const char *field, char *out, size_t size)
{
cJSON *json_obj = cJSON_Parse(json);
if (json_obj == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
printf(stderr, "Error before: %s\n", error_ptr);
}
cJSON_Delete(json_obj);
return -1;
}
cJSON *value = cJSON_GetObjectItem(json_obj, field);
if (!cJSON_IsString(value))
{
cJSON_Delete(json_obj);
return -1;
}
int len = strlen(value->valuestring);
if (len >= size - 1) {
printf("json value too long: %d\n", len);
cJSON_Delete(json_obj);
return -1;
}
strncpy(out, value->valuestring, size);
cJSON_Delete(json_obj);
return 0;
}