cctools
Functions
jx_parse.h File Reference

Parse JSON strings and files into JX expressions. More...

#include "jx.h"
#include "link.h"
#include <stdbool.h>
#include <stdio.h>

Go to the source code of this file.

Functions

struct jxjx_parse_string (const char *str)
 Parse a JSON string to a JX expression. More...
 
struct jxjx_parse_stream (FILE *file)
 Parse a standard IO stream to a JX expression. More...
 
struct jxjx_parse_file (const char *name)
 Parse a file to a JX expression. More...
 
struct jxjx_parse_link (struct link *l, time_t stoptime)
 Parse a network link to a JX expression. More...
 
struct jxjx_parse_cmd_args (struct jx *jx_args, char *args_file)
 Parse a jx argument file from a commandline option. More...
 
int jx_parse_cmd_define (struct jx *jx_args, char *define_stmt)
 Parse a jx define statement from a commandline option. More...
 
struct jx_parser * jx_parser_create (bool strict_mode)
 Create a JX parser object. More...
 
void jx_parser_read_stream (struct jx_parser *p, FILE *file)
 Attach parser to a file. More...
 
void jx_parser_read_string (struct jx_parser *p, const char *str)
 Attach parser to a string. More...
 
void jx_parser_read_link (struct jx_parser *p, struct link *l, time_t stoptime)
 Attach parser to a link. More...
 
struct jxjx_parser_yield (struct jx_parser *p)
 Parse and return a single value. More...
 
struct jxjx_parse (struct jx_parser *p)
 Parse a JX expression. More...
 
int jx_parser_errors (struct jx_parser *p)
 Return number of parse errors. More...
 
const char * jx_parser_error_string (struct jx_parser *p)
 Return text of first parse error encountered. More...
 
void jx_parser_delete (struct jx_parser *p)
 Delete a parser. More...
 

Detailed Description

Parse JSON strings and files into JX expressions.

This module parses arbirary JSON expressions according to the definition at json.org, with the following exceptions:

  1. Atomic values are limited to 4KB in size.
  2. Bare identifiers are permitted, to enable expression evaluation.

Function Documentation

struct jx* jx_parse_string ( const char *  str)

Parse a JSON string to a JX expression.

Parameters
strA C string containing JSON data.
Returns
A JX expression which must be deleted with jx_delete. If the parse fails or no JSON value is present, null is returned.
struct jx* jx_parse_stream ( FILE *  file)

Parse a standard IO stream to a JX expression.

Parameters
fileA stream containing JSON data.
Returns
A JX expression which must be deleted with jx_delete. If the parse fails or no JSON value is present, null is returned.
struct jx* jx_parse_file ( const char *  name)

Parse a file to a JX expression.

Parameters
nameThe name of a file containing JSON data.
Returns
A JX expression which must be deleted with jx_delete. If the parse fails or no JSON value is present, null is returned.
struct jx* jx_parse_link ( struct link *  l,
time_t  stoptime 
)

Parse a network link to a JX expression.

Parameters
lA link pointer (opaque struct).
stoptimeThe absolute time at which to stop.
Returns
A JX expression which must be deleted with jx_delete. If the parse fails or no JSON value is present, null is returned.
struct jx* jx_parse_cmd_args ( struct jx jx_args,
char *  args_file 
)

Parse a jx argument file from a commandline option.

The passed-in object is consumed.

Parameters
jx_argsA JX object to add args to.
args_fileName of the jx_args file that will be read in.
Returns
An augmented JX object.
NULL on failure.
int jx_parse_cmd_define ( struct jx jx_args,
char *  define_stmt 
)

Parse a jx define statement from a commandline option.

Parameters
jx_argsA JX object to add args to.
define_stmtCommand line value of from VAR=EXPR.
Returns
1 on success.
0 on failure.
struct jx_parser* jx_parser_create ( bool  strict_mode)

Create a JX parser object.

Returns
A parser object.
void jx_parser_read_stream ( struct jx_parser *  p,
FILE *  file 
)

Attach parser to a file.

Parameters
pA parser object.
fileA standard IO stream.
void jx_parser_read_string ( struct jx_parser *  p,
const char *  str 
)

Attach parser to a string.

Parameters
pA parser object.
strA JSON string to parse.
void jx_parser_read_link ( struct jx_parser *  p,
struct link *  l,
time_t  stoptime 
)

Attach parser to a link.

Parameters
pA parser object.
lA link object.
stoptimeThe absolute time at which to stop.
struct jx* jx_parser_yield ( struct jx_parser *  p)

Parse and return a single value.

This function is useful for streaming multiple independent values from a single source.

Parameters
pA parser object
Returns
A JX expression which must be deleted with jx_delete. If the parse fails or no JSON value is present, null is returned.
struct jx* jx_parse ( struct jx_parser *  p)

Parse a JX expression.

Note that in the event of a parse error, this function can return a partial result, reflecting the text that was parseable. You must call jx_parser_errors to determine if the parse was successul.

Parameters
pA parser created by jx_parser_create.
Returns
A JX expression, or null if nothing was parsed.
int jx_parser_errors ( struct jx_parser *  p)

Return number of parse errors.

Parameters
pA parser object.
Returns
Number of parse errors encountered.
const char* jx_parser_error_string ( struct jx_parser *  p)

Return text of first parse error encountered.

Parameters
pA parser object.
Returns
Error string, if available, null otherwise.
void jx_parser_delete ( struct jx_parser *  p)

Delete a parser.

Parameters
pThe parser to delete.