diff options
Diffstat (limited to 'src/json/json.c')
-rw-r--r-- | src/json/json.c | 88 |
1 files changed, 44 insertions, 44 deletions
diff --git a/src/json/json.c b/src/json/json.c index ecdea0294..f6d2406c4 100644 --- a/src/json/json.c +++ b/src/json/json.c | |||
@@ -42,34 +42,34 @@ | |||
42 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 42 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
43 | */ | 43 | */ |
44 | int | 44 | int |
45 | GNUNET_JSON_parse(const json_t *root, | 45 | GNUNET_JSON_parse (const json_t *root, |
46 | struct GNUNET_JSON_Specification *spec, | 46 | struct GNUNET_JSON_Specification *spec, |
47 | const char **error_json_name, | 47 | const char **error_json_name, |
48 | unsigned int *error_line) | 48 | unsigned int *error_line) |
49 | { | 49 | { |
50 | if (NULL == root) | 50 | if (NULL == root) |
51 | return GNUNET_SYSERR; | 51 | return GNUNET_SYSERR; |
52 | for (unsigned int i = 0; NULL != spec[i].parser; i++) | 52 | for (unsigned int i = 0; NULL != spec[i].parser; i++) |
53 | { | ||
54 | json_t *pos; | ||
55 | |||
56 | if (NULL == spec[i].field) | ||
57 | pos = (json_t *) root; | ||
58 | else | ||
59 | pos = json_object_get (root, spec[i].field); | ||
60 | if ((NULL == pos) && (spec[i].is_optional)) | ||
61 | continue; | ||
62 | if ((NULL == pos) || | ||
63 | (GNUNET_OK != spec[i].parser (spec[i].cls, pos, &spec[i]))) | ||
53 | { | 64 | { |
54 | json_t *pos; | 65 | if (NULL != error_json_name) |
55 | 66 | *error_json_name = spec[i].field; | |
56 | if (NULL == spec[i].field) | 67 | if (NULL != error_line) |
57 | pos = (json_t *)root; | 68 | *error_line = i; |
58 | else | 69 | GNUNET_JSON_parse_free (spec); |
59 | pos = json_object_get(root, spec[i].field); | 70 | return GNUNET_SYSERR; |
60 | if ((NULL == pos) && (spec[i].is_optional)) | ||
61 | continue; | ||
62 | if ((NULL == pos) || | ||
63 | (GNUNET_OK != spec[i].parser(spec[i].cls, pos, &spec[i]))) | ||
64 | { | ||
65 | if (NULL != error_json_name) | ||
66 | *error_json_name = spec[i].field; | ||
67 | if (NULL != error_line) | ||
68 | *error_line = i; | ||
69 | GNUNET_JSON_parse_free(spec); | ||
70 | return GNUNET_SYSERR; | ||
71 | } | ||
72 | } | 71 | } |
72 | } | ||
73 | return GNUNET_OK; /* all OK! */ | 73 | return GNUNET_OK; /* all OK! */ |
74 | } | 74 | } |
75 | 75 | ||
@@ -81,7 +81,7 @@ GNUNET_JSON_parse(const json_t *root, | |||
81 | * @return spec copy of @a spec with optional bit set | 81 | * @return spec copy of @a spec with optional bit set |
82 | */ | 82 | */ |
83 | struct GNUNET_JSON_Specification | 83 | struct GNUNET_JSON_Specification |
84 | GNUNET_JSON_spec_mark_optional(struct GNUNET_JSON_Specification spec) | 84 | GNUNET_JSON_spec_mark_optional (struct GNUNET_JSON_Specification spec) |
85 | { | 85 | { |
86 | struct GNUNET_JSON_Specification ret = spec; | 86 | struct GNUNET_JSON_Specification ret = spec; |
87 | 87 | ||
@@ -97,11 +97,11 @@ GNUNET_JSON_spec_mark_optional(struct GNUNET_JSON_Specification spec) | |||
97 | * @param spec specification of the parse operation | 97 | * @param spec specification of the parse operation |
98 | */ | 98 | */ |
99 | void | 99 | void |
100 | GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec) | 100 | GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec) |
101 | { | 101 | { |
102 | for (unsigned int i = 0; NULL != spec[i].parser; i++) | 102 | for (unsigned int i = 0; NULL != spec[i].parser; i++) |
103 | if (NULL != spec[i].cleaner) | 103 | if (NULL != spec[i].cleaner) |
104 | spec[i].cleaner(spec[i].cls, &spec[i]); | 104 | spec[i].cleaner (spec[i].cls, &spec[i]); |
105 | } | 105 | } |
106 | 106 | ||
107 | 107 | ||
@@ -118,24 +118,24 @@ GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec) | |||
118 | * @return #GNUNET_OK if parsing the value worked | 118 | * @return #GNUNET_OK if parsing the value worked |
119 | */ | 119 | */ |
120 | static int | 120 | static int |
121 | set_json(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | 121 | set_json (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, |
122 | void *scls, | 122 | void *scls, |
123 | const char *option, | 123 | const char *option, |
124 | const char *value) | 124 | const char *value) |
125 | { | 125 | { |
126 | json_t **json = scls; | 126 | json_t **json = scls; |
127 | json_error_t error; | 127 | json_error_t error; |
128 | 128 | ||
129 | *json = json_loads(value, JSON_REJECT_DUPLICATES, &error); | 129 | *json = json_loads (value, JSON_REJECT_DUPLICATES, &error); |
130 | if (NULL == *json) | 130 | if (NULL == *json) |
131 | { | 131 | { |
132 | fprintf(stderr, | 132 | fprintf (stderr, |
133 | _("Failed to parse JSON in option `%s': %s (%s)\n"), | 133 | _ ("Failed to parse JSON in option `%s': %s (%s)\n"), |
134 | option, | 134 | option, |
135 | error.text, | 135 | error.text, |
136 | error.source); | 136 | error.source); |
137 | return GNUNET_SYSERR; | 137 | return GNUNET_SYSERR; |
138 | } | 138 | } |
139 | return GNUNET_OK; | 139 | return GNUNET_OK; |
140 | } | 140 | } |
141 | 141 | ||
@@ -150,11 +150,11 @@ set_json(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | |||
150 | * @param[out] val set to the JSON specified at the command line | 150 | * @param[out] val set to the JSON specified at the command line |
151 | */ | 151 | */ |
152 | struct GNUNET_GETOPT_CommandLineOption | 152 | struct GNUNET_GETOPT_CommandLineOption |
153 | GNUNET_JSON_getopt(char shortName, | 153 | GNUNET_JSON_getopt (char shortName, |
154 | const char *name, | 154 | const char *name, |
155 | const char *argumentHelp, | 155 | const char *argumentHelp, |
156 | const char *description, | 156 | const char *description, |
157 | json_t **json) | 157 | json_t **json) |
158 | { | 158 | { |
159 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, | 159 | struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, |
160 | .name = name, | 160 | .name = name, |
@@ -162,7 +162,7 @@ GNUNET_JSON_getopt(char shortName, | |||
162 | .description = description, | 162 | .description = description, |
163 | .require_argument = 1, | 163 | .require_argument = 1, |
164 | .processor = &set_json, | 164 | .processor = &set_json, |
165 | .scls = (void *)json }; | 165 | .scls = (void *) json }; |
166 | 166 | ||
167 | return clo; | 167 | return clo; |
168 | } | 168 | } |