GNUnet  0.10.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 
44 int
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  unsigned int i;
51  json_t *pos;
52 
53  if (NULL == root)
54  return GNUNET_SYSERR;
55  for (i=0;NULL != spec[i].parser;i++)
56  {
57  if (NULL == spec[i].field)
58  pos = (json_t *) root;
59  else
60  pos = json_object_get (root,
61  spec[i].field);
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  if (NULL != error_line)
71  *error_line = i;
73  return GNUNET_SYSERR;
74  }
75  }
76  return GNUNET_OK; /* all OK! */
77 }
78 
79 
86 void
88 {
89  for (unsigned int i=0;NULL != spec[i].parser;i++)
90  if (NULL != spec[i].cleaner)
91  spec[i].cleaner (spec[i].cls,
92  &spec[i]);
93 }
94 
95 
108 static int
110  void *scls,
111  const char *option,
112  const char *value)
113 {
114  json_t **json = scls;
115  json_error_t error;
116 
117  *json = json_loads (value,
118  JSON_REJECT_DUPLICATES,
119  &error);
120  if (NULL == *json)
121  {
122  FPRINTF (stderr,
123  _("Failed to parse JSON in option `%s': %s (%s)\n"),
124  option,
125  error.text,
126  error.source);
127  return GNUNET_SYSERR;
128  }
129  return GNUNET_OK;
130 }
131 
132 
144  const char *name,
145  const char *argumentHelp,
146  const char *description,
147  json_t **json)
148 {
149  struct GNUNET_GETOPT_CommandLineOption clo = {
150  .shortName = shortName,
151  .name = name,
152  .argumentHelp = argumentHelp,
153  .description = description,
154  .require_argument = 1,
155  .processor = &set_json,
156  .scls = (void *) json
157  };
158 
159  return clo;
160 }
161 
162 
163 /* end of json.c */
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:143
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
#define FPRINTF
Definition: plibc.h:683
const char * description
Help text for the option (description)
Entry in parser specification for GNUNET_JSON_parse().
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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:87
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static char * value
Value of the record to add/remove.
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.
int 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
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
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:109
const char * name
GNUNET_JSON_Cleaner cleaner
Function for how to clean up this type of entry.