Header And Logo

PostgreSQL
| The world's most advanced open source database.

jsonapi.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * jsonapi.h
00004  *    Declarations for JSON API support.
00005  *
00006  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00007  * Portions Copyright (c) 1994, Regents of the University of California
00008  *
00009  * src/include/utils/jsonapi.h
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 
00014 #ifndef JSONAPI_H
00015 #define JSONAPI_H
00016 
00017 #include "lib/stringinfo.h"
00018 
00019 typedef enum
00020 {
00021     JSON_TOKEN_INVALID,
00022     JSON_TOKEN_STRING,
00023     JSON_TOKEN_NUMBER,
00024     JSON_TOKEN_OBJECT_START,
00025     JSON_TOKEN_OBJECT_END,
00026     JSON_TOKEN_ARRAY_START,
00027     JSON_TOKEN_ARRAY_END,
00028     JSON_TOKEN_COMMA,
00029     JSON_TOKEN_COLON,
00030     JSON_TOKEN_TRUE,
00031     JSON_TOKEN_FALSE,
00032     JSON_TOKEN_NULL,
00033     JSON_TOKEN_END,
00034 }   JsonTokenType;
00035 
00036 
00037 /*
00038  * All the fields in this structure should be treated as read-only.
00039  *
00040  * If strval is not null, then it should contain the de-escaped value
00041  * of the lexeme if it's a string. Otherwise most of these field names
00042  * should be self-explanatory.
00043  *
00044  * line_number and line_start are principally for use by the parser's
00045  * error reporting routines.
00046  * token_terminator and prev_token_terminator point to the character
00047  * AFTER the end of the token, i.e. where there would be a nul byte
00048  * if we were using nul-terminated strings.
00049  */
00050 typedef struct JsonLexContext
00051 {
00052     char       *input;
00053     int         input_length;
00054     char       *token_start;
00055     char       *token_terminator;
00056     char       *prev_token_terminator;
00057     JsonTokenType token_type;
00058     int         lex_level;
00059     int         line_number;
00060     char       *line_start;
00061     StringInfo  strval;
00062 } JsonLexContext;
00063 
00064 typedef void (*json_struct_action) (void *state);
00065 typedef void (*json_ofield_action) (void *state, char *fname, bool isnull);
00066 typedef void (*json_aelem_action) (void *state, bool isnull);
00067 typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType tokentype);
00068 
00069 
00070 /*
00071  * Semantic Action structure for use in parsing json.
00072  * Any of these actions can be NULL, in which case nothing is done at that
00073  * point, Likewise, semstate can be NULL. Using an all-NULL structure amounts
00074  * to doing a pure parse with no side-effects, and is therefore exactly
00075  * what the json input routines do.
00076  */
00077 typedef struct jsonSemAction
00078 {
00079     void       *semstate;
00080     json_struct_action object_start;
00081     json_struct_action object_end;
00082     json_struct_action array_start;
00083     json_struct_action array_end;
00084     json_ofield_action object_field_start;
00085     json_ofield_action object_field_end;
00086     json_aelem_action array_element_start;
00087     json_aelem_action array_element_end;
00088     json_scalar_action scalar;
00089 }   jsonSemAction, *JsonSemAction;
00090 
00091 /*
00092  * parse_json will parse the string in the lex calling the
00093  * action functions in sem at the appropriate points. It is
00094  * up to them to keep what state they need  in semstate. If they
00095  * need access to the state of the lexer, then its pointer
00096  * should be passed to them as a member of whatever semstate
00097  * points to. If the action pointers are NULL the parser
00098  * does nothing and just continues.
00099  */
00100 extern void pg_parse_json(JsonLexContext *lex, JsonSemAction sem);
00101 
00102 /*
00103  * constructor for JsonLexContext, with or without strval element.
00104  * If supplied, the strval element will contain a de-escaped version of
00105  * the lexeme. However, doing this imposes a performance penalty, so
00106  * it should be avoided if the de-escaped lexeme is not required.
00107  */
00108 extern JsonLexContext *makeJsonLexContext(text *json, bool need_escapes);
00109 
00110 #endif   /* JSONAPI_H */