GNUnet  0.11.x
json.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2014-2017, 2021 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 
32 GNUNET_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  return GNUNET_SYSERR;
39  for (unsigned int i = 0; NULL != spec[i].parser; i++)
40  {
41  json_t *pos;
42 
43  if (NULL == spec[i].field)
44  pos = (json_t *) root;
45  else
46  pos = json_object_get (root,
47  spec[i].field);
48  if ( ( (NULL == pos) ||
49  (json_is_null (pos) ) ) &&
50  (spec[i].is_optional) )
51  continue;
52  if ( (NULL == pos) ||
53  (GNUNET_OK !=
54  spec[i].parser (spec[i].cls,
55  pos,
56  &spec[i])) )
57  {
58  if (NULL != error_json_name)
59  *error_json_name = spec[i].field;
60  else
62  "Parsing failed for field `%s:%u`\n",
63  spec[i].field,
64  i);
65  if (NULL != error_line)
66  *error_line = i;
68  return GNUNET_SYSERR;
69  }
70  }
71  return GNUNET_OK; /* all OK! */
72 }
73 
74 
77 {
78  struct GNUNET_JSON_Specification ret = spec;
79 
80  ret.is_optional = GNUNET_YES;
81  return ret;
82 }
83 
84 
85 void
87 {
88  for (unsigned int i = 0; NULL != spec[i].parser; i++)
89  if (NULL != spec[i].cleaner)
90  spec[i].cleaner (spec[i].cls, &spec[i]);
91 }
92 
93 
106 static int
108  void *scls,
109  const char *option,
110  const char *value)
111 {
112  json_t **json = scls;
113  json_error_t error;
114 
115  *json = json_loads (value, JSON_REJECT_DUPLICATES, &error);
116  if (NULL == *json)
117  {
118  fprintf (stderr,
119  _ ("Failed to parse JSON in option `%s': %s (%s)\n"),
120  option,
121  error.text,
122  error.source);
123  return GNUNET_SYSERR;
124  }
125  return GNUNET_OK;
126 }
127 
128 
131  const char *name,
132  const char *argumentHelp,
133  const char *description,
134  json_t **json)
135 {
137  .name = name,
138  .argumentHelp = argumentHelp,
139  .description = description,
140  .require_argument = 1,
141  .processor = &set_json,
142  .scls = (void *) json };
143 
144  return clo;
145 }
146 
147 
148 /* end of json.c */
void * cls
Closure for parser and cleaner.
General context for command line processors.
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:130
const char * argumentHelp
Name of the argument for the user in help text.
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...
functions to parse JSON objects into GNUnet objects
const char * description
Help text for the option (description)
int is_optional
Set to GNUNET_YES if this component is optional.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
GNUNET_GenericReturnValue
Named constants for return values.
Definition: gnunet_common.h:83
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
Entry in parser specification for GNUNET_JSON_parse().
Definition of a command line option.
const char shortName
Short name of the option.
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
void GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec)
Frees all elements allocated during a GNUNET_JSON_parse() operation.
Definition: json.c:86
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
static char * value
Value of the record to add/remove.
struct GNUNET_JSON_Specification GNUNET_JSON_spec_mark_optional(struct GNUNET_JSON_Specification spec)
Set the "optional" flag for a parser specification entry.
Definition: json.c:76
static char * option
Name of the option.
Definition: gnunet-config.c:38
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.
static int 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:107
GNUNET_JSON_Cleaner cleaner
Function for how to clean up this type of entry.
const char * name
#define GNUNET_log(kind,...)