GNUnet 0.22.2
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 {
39 if (NULL != error_json_name)
40 *error_json_name = "root is NULL";
41 if (NULL != error_line)
42 *error_line = 0;
43 return GNUNET_SYSERR;
44 }
45 for (unsigned int i = 0; NULL != spec[i].parser; i++)
46 {
47 json_t *pos;
48
49 if (NULL == spec[i].field)
50 pos = (json_t *) root;
51 else
52 pos = json_object_get (root,
53 spec[i].field);
54 if ( ( (NULL == pos) ||
55 (json_is_null (pos) ) ) &&
56 (spec[i].is_optional) )
57 {
58 if (NULL != spec[i].missing)
59 *spec[i].missing = true;
60 continue;
61 }
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 else
72 "Parsing failed for field `%s:%u`\n",
73 spec[i].field,
74 i);
75 if (NULL != error_line)
76 *error_line = i;
78 return GNUNET_SYSERR;
79 }
80 if (NULL != spec[i].missing)
81 *spec[i].missing = false;
82 }
83 return GNUNET_OK; /* all OK! */
84}
#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:100
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 88 of file json.c.

90{
91 struct GNUNET_JSON_Specification ret = spec;
92
93 ret.is_optional = true;
94 ret.missing = missing;
95 return ret;
96}
static int ret
Final status code.
Definition: gnunet-arm.c:93
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 100 of file json.c.

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,
105 &spec[i]);
106}
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 122 of file json.c.

126{
127 json_t **json = scls;
128 json_error_t error;
129
130 *json = json_loads (value,
131 JSON_REJECT_DUPLICATES,
132 &error);
133 if (NULL == *json)
134 {
135 fprintf (stderr,
136 _ ("Failed to parse JSON in option `%s': %s (%s)\n"),
137 option,
138 error.text,
139 error.source);
140 return GNUNET_SYSERR;
141 }
142 return GNUNET_OK;
143}
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 147 of file json.c.

152{
155 .name = name,
156 .argumentHelp = argumentHelp,
157 .description = description,
158 .require_argument = 1,
159 .processor = &set_json,
160 .scls = (void *) json
161 };
162
163 return clo;
164}
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:122
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: