GNUnet 0.21.1
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, bool *missing)
 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 enum GNUNET_GenericReturnValue 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]error_linewhich index into spec did we encounter an error
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 32 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 {
52 if (NULL != spec[i].missing)
53 *spec[i].missing = true;
54 continue;
55 }
56 if ( (NULL == pos) ||
57 (GNUNET_OK !=
58 spec[i].parser (spec[i].cls,
59 pos,
60 &spec[i])) )
61 {
62 if (NULL != error_json_name)
63 *error_json_name = spec[i].field;
64 else
66 "Parsing failed for field `%s:%u`\n",
67 spec[i].field,
68 i);
69 if (NULL != error_line)
70 *error_line = i;
72 return GNUNET_SYSERR;
73 }
74 if (NULL != spec[i].missing)
75 *spec[i].missing = false;
76 }
77 return GNUNET_OK; /* all OK! */
78}
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_SYSERR
@ 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:94
bool * missing
Pointer to set to true if this argument is indeed missing.
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, GNUNET_JSON_Specification::missing, consensus-simulation::parser, and GNUNET_JSON_Specification::parser.

Referenced by add_attribute_cont(), add_credential_cont(), bulk_tx_start(), consume_ticket_cont(), namestore_add_or_update(), parse_blinded_message(), parse_blinded_sig(), parse_unblinded_sig(), 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,
bool *  missing 
)

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

Parameters
specspecification to modify
[out]missingset to true if the argument is missing, NULL is allowed.
Returns
spec copy of spec with optional bit set

Definition at line 82 of file json.c.

84{
85 struct GNUNET_JSON_Specification ret = spec;
86
87 ret.is_optional = true;
88 ret.missing = missing;
89 return ret;
90}
static int ret
Final status code.
Definition: gnunet-arm.c:94
Entry in parser specification for GNUNET_JSON_parse().

References GNUNET_JSON_Specification::missing, and ret.

◆ GNUNET_JSON_parse_free()

void GNUNET_JSON_parse_free ( struct GNUNET_JSON_Specification spec)

Frees all elements allocated during a GNUNET_JSON_parse() operation.

Convenience function to be called if cleaning up all heap-allocated data from a GNUNET_JSON_parse() is desired. The function does not have to be called if no data was heap-allocated (e.g. only integers, strings and fixed-sized data was used), or if the application calls the respective code to free the heap (not always GNUNET_free(), depends on the data type!) on the returned heap-allocated data itself.

Parameters
specspecification of the parse operation

Definition at line 94 of file json.c.

95{
96 for (unsigned int i = 0; NULL != spec[i].parser; i++)
97 if (NULL != spec[i].cleaner)
98 spec[i].cleaner (spec[i].cls,
99 &spec[i]);
100}
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 enum GNUNET_GenericReturnValue 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 116 of file json.c.

120{
121 json_t **json = scls;
122 json_error_t error;
123
124 *json = json_loads (value,
125 JSON_REJECT_DUPLICATES,
126 &error);
127 if (NULL == *json)
128 {
129 fprintf (stderr,
130 _ ("Failed to parse JSON in option `%s': %s (%s)\n"),
131 option,
132 error.text,
133 error.source);
134 return GNUNET_SYSERR;
135 }
136 return GNUNET_OK;
137}
static char * value
Value of the record to add/remove.
#define _(String)
GNU gettext support macro.
Definition: platform.h:178

References _, GNUNET_OK, GNUNET_SYSERR, and value.

Referenced by GNUNET_JSON_getopt().

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

146{
149 .name = name,
150 .argumentHelp = argumentHelp,
151 .description = description,
152 .require_argument = 1,
153 .processor = &set_json,
154 .scls = (void *) json
155 };
156
157 return clo;
158}
static char * name
Name (label) of the records to list.
static enum GNUNET_GenericReturnValue 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:116
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_GETOPT_CommandLineOption::argumentHelp, GNUNET_GETOPT_CommandLineOption::description, name, set_json(), and GNUNET_GETOPT_CommandLineOption::shortName.

Here is the call graph for this function: