Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes16kdownloads
jsonapi.h178 linesDownload Raw Back to common
1/*-------------------------------------------------------------------------2 *3 * jsonapi.h4 *	  Declarations for JSON API support.5 *6 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group7 * Portions Copyright (c) 1994, Regents of the University of California8 *9 * src/include/common/jsonapi.h10 *11 *-------------------------------------------------------------------------12 */13 14#ifndef JSONAPI_H15#define JSONAPI_H16 17#include "lib/stringinfo.h"18 19typedef enum JsonTokenType20{21	JSON_TOKEN_INVALID,22	JSON_TOKEN_STRING,23	JSON_TOKEN_NUMBER,24	JSON_TOKEN_OBJECT_START,25	JSON_TOKEN_OBJECT_END,26	JSON_TOKEN_ARRAY_START,27	JSON_TOKEN_ARRAY_END,28	JSON_TOKEN_COMMA,29	JSON_TOKEN_COLON,30	JSON_TOKEN_TRUE,31	JSON_TOKEN_FALSE,32	JSON_TOKEN_NULL,33	JSON_TOKEN_END34} JsonTokenType;35 36typedef enum JsonParseErrorType37{38	JSON_SUCCESS,39	JSON_ESCAPING_INVALID,40	JSON_ESCAPING_REQUIRED,41	JSON_EXPECTED_ARRAY_FIRST,42	JSON_EXPECTED_ARRAY_NEXT,43	JSON_EXPECTED_COLON,44	JSON_EXPECTED_END,45	JSON_EXPECTED_JSON,46	JSON_EXPECTED_MORE,47	JSON_EXPECTED_OBJECT_FIRST,48	JSON_EXPECTED_OBJECT_NEXT,49	JSON_EXPECTED_STRING,50	JSON_INVALID_TOKEN,51	JSON_UNICODE_CODE_POINT_ZERO,52	JSON_UNICODE_ESCAPE_FORMAT,53	JSON_UNICODE_HIGH_ESCAPE,54	JSON_UNICODE_UNTRANSLATABLE,55	JSON_UNICODE_HIGH_SURROGATE,56	JSON_UNICODE_LOW_SURROGATE,57	JSON_SEM_ACTION_FAILED		/* error should already be reported */58} JsonParseErrorType;59 60 61/*62 * All the fields in this structure should be treated as read-only.63 *64 * If strval is not null, then it should contain the de-escaped value65 * of the lexeme if it's a string. Otherwise most of these field names66 * should be self-explanatory.67 *68 * line_number and line_start are principally for use by the parser's69 * error reporting routines.70 * token_terminator and prev_token_terminator point to the character71 * AFTER the end of the token, i.e. where there would be a nul byte72 * if we were using nul-terminated strings.73 */74typedef struct JsonLexContext75{76	char	   *input;77	int			input_length;78	int			input_encoding;79	char	   *token_start;80	char	   *token_terminator;81	char	   *prev_token_terminator;82	JsonTokenType token_type;83	int			lex_level;84	int			line_number;	/* line number, starting from 1 */85	char	   *line_start;		/* where that line starts within input */86	StringInfo	strval;87} JsonLexContext;88 89typedef JsonParseErrorType (*json_struct_action) (void *state);90typedef JsonParseErrorType (*json_ofield_action) (void *state, char *fname, bool isnull);91typedef JsonParseErrorType (*json_aelem_action) (void *state, bool isnull);92typedef JsonParseErrorType (*json_scalar_action) (void *state, char *token, JsonTokenType tokentype);93 94 95/*96 * Semantic Action structure for use in parsing json.97 *98 * Any of these actions can be NULL, in which case nothing is done at that99 * point, Likewise, semstate can be NULL. Using an all-NULL structure amounts100 * to doing a pure parse with no side-effects, and is therefore exactly101 * what the json input routines do.102 *103 * The 'fname' and 'token' strings passed to these actions are palloc'd.104 * They are not free'd or used further by the parser, so the action function105 * is free to do what it wishes with them.106 *107 * All action functions return JsonParseErrorType.  If the result isn't108 * JSON_SUCCESS, the parse is abandoned and that error code is returned.109 * If it is JSON_SEM_ACTION_FAILED, the action function is responsible110 * for having reported the error in some appropriate way.111 */112typedef struct JsonSemAction113{114	void	   *semstate;115	json_struct_action object_start;116	json_struct_action object_end;117	json_struct_action array_start;118	json_struct_action array_end;119	json_ofield_action object_field_start;120	json_ofield_action object_field_end;121	json_aelem_action array_element_start;122	json_aelem_action array_element_end;123	json_scalar_action scalar;124} JsonSemAction;125 126/*127 * pg_parse_json will parse the string in the lex calling the128 * action functions in sem at the appropriate points. It is129 * up to them to keep what state they need in semstate. If they130 * need access to the state of the lexer, then its pointer131 * should be passed to them as a member of whatever semstate132 * points to. If the action pointers are NULL the parser133 * does nothing and just continues.134 */135extern JsonParseErrorType pg_parse_json(JsonLexContext *lex,136										JsonSemAction *sem);137 138/* the null action object used for pure validation */139extern PGDLLIMPORT JsonSemAction nullSemAction;140 141/*142 * json_count_array_elements performs a fast secondary parse to determine the143 * number of elements in passed array lex context. It should be called from an144 * array_start action.145 *146 * The return value indicates whether any error occurred, while the number147 * of elements is stored into *elements (but only if the return value is148 * JSON_SUCCESS).149 */150extern JsonParseErrorType json_count_array_elements(JsonLexContext *lex,151													int *elements);152 153/*154 * constructor for JsonLexContext, with or without strval element.155 * If supplied, the strval element will contain a de-escaped version of156 * the lexeme. However, doing this imposes a performance penalty, so157 * it should be avoided if the de-escaped lexeme is not required.158 */159extern JsonLexContext *makeJsonLexContextCstringLen(char *json,160													int len,161													int encoding,162													bool need_escapes);163 164/* lex one token */165extern JsonParseErrorType json_lex(JsonLexContext *lex);166 167/* construct an error detail string for a json error */168extern char *json_errdetail(JsonParseErrorType error, JsonLexContext *lex);169 170/*171 * Utility function to check if a string is a valid JSON number.172 *173 * str argument does not need to be nul-terminated.174 */175extern bool IsValidJsonNumber(const char *str, int len);176 177#endif							/* JSONAPI_H */178 
codekingpro/portable-devtools · Team Ai