GNUnet 0.22.2
json.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2014-2017, 2021, 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
27#include "platform.h"
28#include "gnunet_json_lib.h"
29
30
32GNUNET_JSON_parse (const json_t *root,
33 struct GNUNET_JSON_Specification *spec,
34 const char **error_json_name,
35 unsigned int *error_line)
36{
37 if (NULL == root)
38 {
39 if (NULL != error_json_name)
40 *error_json_name = "root is NULL";
41 if (NULL != error_line)
42 *error_line = 0;
43 return GNUNET_SYSERR;
44 }
45 for (unsigned int i = 0; NULL != spec[i].parser; i++)
46 {
47 json_t *pos;
48
49 if (NULL == spec[i].field)
50 pos = (json_t *) root;
51 else
52 pos = json_object_get (root,
53 spec[i].field);
54 if ( ( (NULL == pos) ||
55 (json_is_null (pos) ) ) &&
56 (spec[i].is_optional) )
57 {
58 if (NULL != spec[i].missing)
59 *spec[i].missing = true;
60 continue;
61 }
62 if ( (NULL == pos) ||
63 (GNUNET_OK !=
64 spec[i].parser (spec[i].cls,
65 pos,
66 &spec[i])) )
67 {
68 if (NULL != error_json_name)
69 *error_json_name = spec[i].field;
70 else
72 "Parsing failed for field `%s:%u`\n",
73 spec[i].field,
74 i);
75 if (NULL != error_line)
76 *error_line = i;
78 return GNUNET_SYSERR;
79 }
80 if (NULL != spec[i].missing)
81 *spec[i].missing = false;
82 }
83 return GNUNET_OK; /* all OK! */
84}
85
86
89 bool *missing)
90{
91 struct GNUNET_JSON_Specification ret = spec;
92
93 ret.is_optional = true;
94 ret.missing = missing;
95 return ret;
96}
97
98
99void
101{
102 for (unsigned int i = 0; NULL != spec[i].parser; i++)
103 if (NULL != spec[i].cleaner)
104 spec[i].cleaner (spec[i].cls,
105 &spec[i]);
106}
107
108
123 void *scls,
124 const char *option,
125 const char *value)
126{
127 json_t **json = scls;
128 json_error_t error;
129
130 *json = json_loads (value,
131 JSON_REJECT_DUPLICATES,
132 &error);
133 if (NULL == *json)
134 {
135 fprintf (stderr,
136 _ ("Failed to parse JSON in option `%s': %s (%s)\n"),
137 option,
138 error.text,
139 error.source);
140 return GNUNET_SYSERR;
141 }
142 return GNUNET_OK;
143}
144
145
148 const char *name,
149 const char *argumentHelp,
150 const char *description,
151 json_t **json)
152{
155 .name = name,
156 .argumentHelp = argumentHelp,
157 .description = description,
158 .require_argument = 1,
159 .processor = &set_json,
160 .scls = (void *) json
161 };
162
163 return clo;
164}
165
166
167/* end of json.c */
static int ret
Final status code.
Definition: gnunet-arm.c:93
static struct GNUNET_FS_Handle * ctx
static char * name
Name (label) of the records to list.
static char * value
Value of the record to add/remove.
functions to parse JSON objects into GNUnet objects
#define GNUNET_log(kind,...)
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_SYSERR
@ GNUNET_ERROR_TYPE_WARNING
static enum GNUNET_GenericReturnValue set_json(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, void *scls, const char *option, const char *value)
Set an option with a JSON value from the command line.
Definition: json.c:122
void GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec)
Frees all elements allocated during a GNUNET_JSON_parse() operation.
Definition: json.c:100
struct GNUNET_JSON_Specification GNUNET_JSON_spec_mark_optional(struct GNUNET_JSON_Specification spec, bool *missing)
Set the "optional" flag for a parser specification entry.
Definition: json.c:88
struct GNUNET_GETOPT_CommandLineOption GNUNET_JSON_getopt(char shortName, const char *name, const char *argumentHelp, const char *description, json_t **json)
Allow user to specify a JSON input value.
Definition: json.c:147
enum GNUNET_GenericReturnValue GNUNET_JSON_parse(const json_t *root, struct GNUNET_JSON_Specification *spec, const char **error_json_name, unsigned int *error_line)
Navigate and parse data in a JSON tree.
Definition: json.c:32
#define _(String)
GNU gettext support macro.
Definition: platform.h:179
Definition of a command line option.
const char * description
Help text for the option (description)
const char * argumentHelp
Name of the argument for the user in help text.
const char shortName
Short name of the option.
General context for command line processors.
Entry in parser specification for GNUNET_JSON_parse().
GNUNET_JSON_Cleaner cleaner
Function for how to clean up this type of entry.
bool * missing
Pointer to set to true if this argument is indeed missing.
const char * field
Name of the field to parse, use NULL to get the JSON of the main object instead of the JSON of an ind...
void * cls
Closure for parser and cleaner.
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.