diff options
Diffstat (limited to 'src/json/json.c')
-rw-r--r-- | src/json/json.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/json/json.c b/src/json/json.c new file mode 100644 index 000000000..aa74bfd48 --- /dev/null +++ b/src/json/json.c | |||
@@ -0,0 +1,92 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2014, 2015, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify it under the | ||
6 | terms of the GNU General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | /** | ||
17 | * @file json/json.c | ||
18 | * @brief functions to parse JSON snippets | ||
19 | * @author Florian Dold | ||
20 | * @author Benedikt Mueller | ||
21 | * @author Christian Grothoff | ||
22 | */ | ||
23 | #include "platform.h" | ||
24 | #include "gnunet_json_lib.h" | ||
25 | |||
26 | |||
27 | /** | ||
28 | * Navigate and parse data in a JSON tree. Tries to parse the @a root | ||
29 | * to find all of the values given in the @a spec. If one of the | ||
30 | * entries in @a spec cannot be found or parsed, the name of the JSON | ||
31 | * field is returned in @a error_json_name, and the offset of the | ||
32 | * entry in @a spec is returned in @a error_line. | ||
33 | * | ||
34 | * @param root the JSON node to start the navigation at. | ||
35 | * @param spec parse specification array | ||
36 | * @param[out] error_json_name which JSON field was problematic | ||
37 | * @param[out] which index into @a spec did we encounter an error | ||
38 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | ||
39 | */ | ||
40 | int | ||
41 | GNUNET_JSON_parse (const json_t *root, | ||
42 | struct GNUNET_JSON_Specification *spec, | ||
43 | const char **error_json_name, | ||
44 | unsigned int *error_line) | ||
45 | { | ||
46 | unsigned int i; | ||
47 | json_t *pos; | ||
48 | |||
49 | for (i=0;NULL != spec[i].parser;i++) | ||
50 | { | ||
51 | if (NULL == spec[i].field) | ||
52 | pos = (json_t *) root; | ||
53 | else | ||
54 | pos = json_object_get (root, | ||
55 | spec[i].field); | ||
56 | if ( (NULL == pos) || | ||
57 | (GNUNET_OK != | ||
58 | spec[i].parser (spec[i].cls, | ||
59 | pos, | ||
60 | &spec[i])) ) | ||
61 | { | ||
62 | if (NULL != error_json_name) | ||
63 | *error_json_name = spec[i].field; | ||
64 | if (NULL != error_line) | ||
65 | *error_line = i; | ||
66 | GNUNET_JSON_parse_free (spec); | ||
67 | return GNUNET_SYSERR; | ||
68 | } | ||
69 | } | ||
70 | return GNUNET_OK; /* all OK! */ | ||
71 | } | ||
72 | |||
73 | |||
74 | /** | ||
75 | * Frees all elements allocated during a #GNUNET_JSON_parse() | ||
76 | * operation. | ||
77 | * | ||
78 | * @param spec specification of the parse operation | ||
79 | */ | ||
80 | void | ||
81 | GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec) | ||
82 | { | ||
83 | unsigned int i; | ||
84 | |||
85 | for (i=0;NULL != spec[i].parser;i++) | ||
86 | if (NULL != spec[i].cleaner) | ||
87 | spec[i].cleaner (spec[i].cls, | ||
88 | &spec[i]); | ||
89 | } | ||
90 | |||
91 | |||
92 | /* end of json.c */ | ||