aboutsummaryrefslogtreecommitdiff
path: root/src/json
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-01-22 17:05:38 +0100
committerFlorian Dold <florian.dold@gmail.com>2018-01-22 17:08:27 +0100
commit407e79aca5cc2caadb9c9a916fcb91c3e9f3c244 (patch)
tree5f2474b997452fb9a66ca2d4519eabed1d205dfb /src/json
parente22c9d7e579210f39008923937e15b45fb226319 (diff)
downloadgnunet-407e79aca5cc2caadb9c9a916fcb91c3e9f3c244.tar.gz
gnunet-407e79aca5cc2caadb9c9a916fcb91c3e9f3c244.zip
implement boolean json spec
Diffstat (limited to 'src/json')
-rw-r--r--src/json/json_helper.c48
-rw-r--r--src/json/test_json.c40
2 files changed, 88 insertions, 0 deletions
diff --git a/src/json/json_helper.c b/src/json/json_helper.c
index 194ec5c76..c7eca97ec 100644
--- a/src/json/json_helper.c
+++ b/src/json/json_helper.c
@@ -943,4 +943,52 @@ GNUNET_JSON_spec_rsa_signature (const char *name,
943} 943}
944 944
945 945
946/**
947 * Parse given JSON object to an int as a boolean.
948 *
949 * @param cls closure, NULL
950 * @param root the json object representing data
951 * @param[out] spec where to write the data
952 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
953 */
954static int
955parse_boolean (void *cls,
956 json_t *root,
957 struct GNUNET_JSON_Specification *spec)
958{
959 int *bp = spec->ptr;
960
961 if (! json_is_boolean (root))
962 {
963 GNUNET_break_op (0);
964 return GNUNET_SYSERR;
965 }
966 *bp = json_boolean_value (root) ? GNUNET_YES : GNUNET_NO;
967 return GNUNET_OK;
968}
969
970
971/**
972 * Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO).
973 *
974 * @param name name of the JSON field
975 * @param[out] boolean where to store the boolean found under @a name
976 */
977struct GNUNET_JSON_Specification
978GNUNET_JSON_spec_boolean (const char *name,
979 int *boolean)
980{
981 struct GNUNET_JSON_Specification ret = {
982 .parser = &parse_boolean,
983 .cleaner = NULL,
984 .cls = NULL,
985 .field = name,
986 .ptr = boolean,
987 .ptr_size = sizeof (int),
988 .size_ptr = NULL
989 };
990 return ret;
991}
992
993
946/* end of json_helper.c */ 994/* end of json_helper.c */
diff --git a/src/json/test_json.c b/src/json/test_json.c
index 09a154678..adab817b9 100644
--- a/src/json/test_json.c
+++ b/src/json/test_json.c
@@ -195,6 +195,44 @@ test_rsa ()
195} 195}
196 196
197 197
198/**
199 * Test rsa conversions from/to JSON.
200 *
201 * @return 0 on success
202 */
203static int
204test_boolean ()
205{
206 int b1;
207 int b2;
208 json_t *json;
209 struct GNUNET_JSON_Specification pspec[] = {
210 GNUNET_JSON_spec_boolean ("b1", &b1),
211 GNUNET_JSON_spec_boolean ("b2", &b2),
212 GNUNET_JSON_spec_end()
213 };
214
215 json = json_object ();
216 json_object_set_new (json, "b1", json_true ());
217 json_object_set_new (json, "b2", json_false ());
218
219 GNUNET_assert (GNUNET_OK ==
220 GNUNET_JSON_parse (json, pspec,
221 NULL, NULL));
222
223 GNUNET_assert (GNUNET_YES == b1);
224 GNUNET_assert (GNUNET_NO == b2);
225
226 json_object_set_new (json, "b1", json_integer (42));
227
228 GNUNET_assert (GNUNET_OK !=
229 GNUNET_JSON_parse (json, pspec,
230 NULL, NULL));
231
232 return 0;
233}
234
235
198int 236int
199main(int argc, 237main(int argc,
200 const char *const argv[]) 238 const char *const argv[])
@@ -210,6 +248,8 @@ main(int argc,
210 return 1; 248 return 1;
211 if (0 != test_rsa ()) 249 if (0 != test_rsa ())
212 return 1; 250 return 1;
251 if (0 != test_boolean ())
252 return 1;
213 /* FIXME: test EdDSA signature conversion... */ 253 /* FIXME: test EdDSA signature conversion... */
214 return 0; 254 return 0;
215} 255}