GNUnet  0.11.x
Functions
json.c File Reference

functions to parse JSON snippets More...

#include "platform.h"
#include "gnunet_json_lib.h"
Include dependency graph for json.c:

Go to the source code of this file.

Functions

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. More...
 
struct GNUNET_JSON_Specification GNUNET_JSON_spec_mark_optional (struct GNUNET_JSON_Specification spec)
 Set the "optional" flag for a parser specification entry. More...
 
void GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec)
 Frees all elements allocated during a GNUNET_JSON_parse() operation. More...
 
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. More...
 
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. More...
 

Detailed Description

functions to parse JSON snippets

Author
Florian Dold
Benedikt Mueller
Christian Grothoff

Definition in file json.c.

Function Documentation

◆ GNUNET_JSON_parse()

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.

Tries to parse the root to find all of the values given in the spec. If one of the entries in spec cannot be found or parsed, the name of the JSON field is returned in error_json_name, and the offset of the entry in spec is returned in error_line.

Parameters
rootthe JSON node to start the navigation at.
specparse specification array
[out]error_json_namewhich JSON field was problematic
[out]whichindex into spec did we encounter an error
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 1 of file json.c.

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 }
#define GNUNET_log(kind,...)
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_SYSERR
Definition: gnunet_common.h:93
@ GNUNET_ERROR_TYPE_WARNING
void GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec)
Frees all elements allocated during a GNUNET_JSON_parse() operation.
Definition: json.c:86
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...
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.

References GNUNET_JSON_Specification::field, GNUNET_ERROR_TYPE_WARNING, GNUNET_JSON_parse_free(), GNUNET_log, GNUNET_OK, GNUNET_SYSERR, consensus-simulation::parser, and GNUNET_JSON_Specification::parser.

Referenced by add_attribute_cont(), add_credential_cont(), consume_ticket_cont(), namestore_add_or_update(), and revoke_ticket_cont().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_JSON_spec_mark_optional()

struct GNUNET_JSON_Specification GNUNET_JSON_spec_mark_optional ( struct GNUNET_JSON_Specification  spec)

Set the "optional" flag for a parser specification entry.

Parameters
specspecification to modify
Returns
spec copy of spec with optional bit set

Definition at line 1 of file json.c.

77 {
78  struct GNUNET_JSON_Specification ret = spec;
79 
80  ret.is_optional = GNUNET_YES;
81  return ret;
82 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
@ GNUNET_YES
Definition: gnunet_common.h:97
Entry in parser specification for GNUNET_JSON_parse().

◆ GNUNET_JSON_parse_free()

void GNUNET_JSON_parse_free ( struct GNUNET_JSON_Specification spec)

Frees all elements allocated during a GNUNET_JSON_parse() operation.

Parameters
specspecification of the parse operation

Definition at line 86 of file json.c.

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,
91  &spec[i]);
92 }
GNUNET_JSON_Cleaner cleaner
Function for how to clean up this type of entry.

References GNUNET_JSON_Specification::cleaner, GNUNET_JSON_Specification::cls, and GNUNET_JSON_Specification::parser.

Referenced by add_attribute_cont(), add_credential_cont(), consume_ticket_cont(), GNUNET_JSON_parse(), namestore_add_or_update(), and revoke_ticket_cont().

Here is the caller graph for this function:

◆ set_json()

static int set_json ( struct GNUNET_GETOPT_CommandLineProcessorContext ctx,
void *  scls,
const char *  option,
const char *  value 
)
static

Set an option with a JSON value from the command line.

A pointer to this function should be passed as part of the 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options of this type.

Parameters
ctxcommand line processing context
sclsadditional closure (will point to the 'json_t *')
optionname of the option
valueactual value of the option as a string.
Returns
GNUNET_OK if parsing the value worked

Definition at line 108 of file json.c.

112 {
113  json_t **json = scls;
114  json_error_t error;
115 
116  *json = json_loads (value, JSON_REJECT_DUPLICATES, &error);
117  if (NULL == *json)
118  {
119  fprintf (stderr,
120  _ ("Failed to parse JSON in option `%s': %s (%s)\n"),
121  option,
122  error.text,
123  error.source);
124  return GNUNET_SYSERR;
125  }
126  return GNUNET_OK;
127 }
static char * value
Value of the record to add/remove.
#define _(String)
GNU gettext support macro.
Definition: platform.h:177

◆ GNUNET_JSON_getopt()

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.

Parameters
shortNameshort name of the option
namelong name of the option
argumentHelphelp text for the option argument
descriptionlong help text for the option
[out]valset to the JSON specified at the command line

Definition at line 108 of file json.c.

136 {
138  .name = name,
139  .argumentHelp = argumentHelp,
140  .description = description,
141  .require_argument = 1,
142  .processor = &set_json,
143  .scls = (void *) json };
144 
145  return clo;
146 }
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:108
const char * name
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.

References _, GNUNET_OK, GNUNET_SYSERR, and value.