GNUnet  0.10.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

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. 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()

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.

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 45 of file json.c.

References GNUNET_JSON_Specification::field, GNUNET_JSON_parse_free(), GNUNET_OK, GNUNET_SYSERR, and GNUNET_JSON_Specification::parser.

Referenced by add_attribute_cont(), consume_ticket_cont(), namestore_add(), revoke_ticket_cont(), and verify_cred_cont().

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, spec[i].field);
60  if ((NULL == pos) && (spec[i].is_optional))
61  continue;
62  if ((NULL == pos) ||
63  (GNUNET_OK != spec[i].parser(spec[i].cls, pos, &spec[i])))
64  {
65  if (NULL != error_json_name)
66  *error_json_name = spec[i].field;
67  if (NULL != error_line)
68  *error_line = i;
70  return GNUNET_SYSERR;
71  }
72  }
73  return GNUNET_OK; /* all OK! */
74 }
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...
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
void GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec)
Frees all elements allocated during a GNUNET_JSON_parse() operation.
Definition: json.c:100
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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 84 of file json.c.

References GNUNET_YES, GNUNET_JSON_Specification::is_optional, and ret.

85 {
86  struct GNUNET_JSON_Specification ret = spec;
87 
88  ret.is_optional = GNUNET_YES;
89  return ret;
90 }
int is_optional
Set to GNUNET_YES if this component is optional.
Entry in parser specification for GNUNET_JSON_parse().
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_YES
Definition: gnunet_common.h:77

◆ 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 100 of file json.c.

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

Referenced by add_attribute_cont(), consume_ticket_cont(), GNUNET_JSON_parse(), namestore_add(), and revoke_ticket_cont().

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, &spec[i]);
105 }
void * cls
Closure for parser and cleaner.
GNUNET_JSON_Parser parser
Function for how to parse this type of entry.
GNUNET_JSON_Cleaner cleaner
Function for how to clean up this type of entry.
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 121 of file json.c.

References _, GNUNET_OK, and GNUNET_SYSERR.

Referenced by GNUNET_JSON_getopt().

125 {
126  json_t **json = scls;
127  json_error_t error;
128 
129  *json = json_loads(value, JSON_REJECT_DUPLICATES, &error);
130  if (NULL == *json)
131  {
132  fprintf(stderr,
133  _("Failed to parse JSON in option `%s': %s (%s)\n"),
134  option,
135  error.text,
136  error.source);
137  return GNUNET_SYSERR;
138  }
139  return GNUNET_OK;
140 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static char * value
Value of the record to add/remove.
static char * option
Name of the option.
Definition: gnunet-config.c:38
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
Here is the caller graph for this function:

◆ 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 153 of file json.c.

References GNUNET_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, name, set_json(), and GNUNET_GETOPT_CommandLineOption::shortName.

158 {
160  .name = name,
161  .argumentHelp = argumentHelp,
162  .description = description,
163  .require_argument = 1,
164  .processor = &set_json,
165  .scls = (void *)json };
166 
167  return clo;
168 }
const char * argumentHelp
Name of the argument for the user in help text.
const char * description
Help text for the option (description)
Definition of a command line option.
const char shortName
Short name of the option.
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:121
const char * name
Here is the call graph for this function: