-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjjp_wrap.c
128 lines (79 loc) · 3.09 KB
/
jjp_wrap.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
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
#include <jjp_wrap.h>
#include <jsonpath.h>
#include <stdlib.h>
#include <errno.h>
float jjp_float( const char * json, jsmntok_t * tokens, unsigned int tokens_count, const char * jsonpath, unsigned int current_object, int * success ) {
float r;
int i;
i = jjp_jsonpath_first( json, tokens, tokens_count, jsonpath, current_object );
check( i >= 0 && tokens[i].type == JSMN_PRIMITIVE, final_cleanup );
errno = 0;
r = strtof( json + tokens[i].start, NULL );
check( errno == 0, final_cleanup );
if( success ) *success = 0;
return r;
final_cleanup:
if( success ) *success = -1;
return -1;
}
double jjp_double( const char * json, jsmntok_t * tokens, unsigned int tokens_count, const char * jsonpath, unsigned int current_object, int * success ) {
double r;
int i;
i = jjp_jsonpath_first( json, tokens, tokens_count, jsonpath, current_object );
check( i >= 0 && tokens[i].type == JSMN_PRIMITIVE, final_cleanup );
errno = 0;
r = strtod( json + tokens[i].start, NULL );
check( errno == 0, final_cleanup );
if( success ) *success = 0;
return r;
final_cleanup:
if( success ) *success = -1;
return -1;
}
int jjp_int( const char * json, jsmntok_t * tokens, unsigned int tokens_count, const char * jsonpath, unsigned int current_object, int * success ) {
int r;
int i;
i = jjp_jsonpath_first( json, tokens, tokens_count, jsonpath, current_object );
check( i >= 0 && tokens[i].type == JSMN_PRIMITIVE, final_cleanup );
errno = 0;
r = strtol( json + tokens[i].start, NULL, 10 );
check( errno == 0, final_cleanup );
if( success ) *success = 0;
return r;
final_cleanup:
if( success ) *success = -1;
return -1;
}
long jjp_long( const char * json, jsmntok_t * tokens, unsigned int tokens_count, const char * jsonpath, unsigned int current_object, int * success ) {
long r;
int i;
i = jjp_jsonpath_first( json, tokens, tokens_count, jsonpath, current_object );
check( i >= 0 && tokens[i].type == JSMN_PRIMITIVE, final_cleanup );
errno = 0;
r = strtoll( json + tokens[i].start, NULL, 10 );
check( errno == 0, final_cleanup );
if( success ) *success = 0;
return r;
final_cleanup:
if( success ) *success = -1;
return -1;
}
int jjp_boolean( const char * json, jsmntok_t * tokens, unsigned int tokens_count, const char * jsonpath, unsigned int current_object, int * success ) {
int r;
int i;
char res;
i = jjp_jsonpath_first( json, tokens, tokens_count, jsonpath, current_object );
check( i >= 0 && tokens[i].type == JSMN_PRIMITIVE, final_cleanup );
res = *(json + tokens[i].start);
if( res == 't' ) {
r = 1;
} else {
check( res == 'f', final_cleanup);
r = 0;
}
if( success ) *success = 0;
return r;
final_cleanup:
if( success ) *success = -1;
return 0;
}