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 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 
45 GNUNET_JSON_parse (const json_t *root,
46  struct GNUNET_JSON_Specification *spec,
47  const char **error_json_name,
48  unsigned int *error_line)
49 {
50  if (NULL == root)
51  return GNUNET_SYSERR;
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,
60  spec[i].field);
61  if ( ( (NULL == pos) ||
62  (json_is_null (pos) ) ) &&
63  (spec[i].is_optional) )
64  continue;
65  if ( (NULL == pos) ||
66  (GNUNET_OK !=
67  spec[i].parser (spec[i].cls,
68  pos,
69  &spec[i])) )
70  {
71  if (NULL != error_json_name)
72  *error_json_name = spec[i].field;
73  if (NULL != error_line)
74  *error_line = i;
76  return GNUNET_SYSERR;
77  }
78  }
79  return GNUNET_OK; /* all OK! */
80 }
81 
82 
91 {
92  struct GNUNET_JSON_Specification ret = spec;
93 
94  ret.is_optional = GNUNET_YES;
95  return ret;
96 }
97 
98 
105 void
107 {
108  for (unsigned int i = 0; NULL != spec[i].parser; i++)
109  if (NULL != spec[i].cleaner)
110  spec[i].cleaner (spec[i].cls, &spec[i]);
111 }
112 
113 
126 static int
128  void *scls,
129  const char *option,
130  const char *value)
131 {
132  json_t **json = scls;
133  json_error_t error;
134 
135  *json = json_loads (value, JSON_REJECT_DUPLICATES, &error);
136  if (NULL == *json)
137  {
138  fprintf (stderr,
139  _ ("Failed to parse JSON in option `%s': %s (%s)\n"),
140  option,
141  error.text,
142  error.source);
143  return GNUNET_SYSERR;
144  }
145  return GNUNET_OK;
146 }
147 
148 
160  const char *name,
161  const char *argumentHelp,
162  const char *description,
163  json_t **json)
164 {
166  .name = name,
167  .argumentHelp = argumentHelp,
168  .description = description,
169  .require_argument = 1,
170  .processor = &set_json,
171  .scls = (void *) json };
172 
173  return clo;
174 }
175 
176 
177 /* 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:159
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:45
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:106
#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:90
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:127
GNUNET_JSON_Cleaner cleaner
Function for how to clean up this type of entry.
const char * name