MCPcopy Create free account
hub / github.com/apache/cloudberry / parse_scalar

Function parse_scalar

src/common/jsonapi.c:272–314  ·  view source on GitHub ↗

* Recursive Descent parse routines. There is one for each structural * element in a json document: * - scalar (string, number, true, false, null) * - array ( [ ] ) * - array element * - object ( { } ) * - object field */

Source from the content-addressed store, hash-verified

270 * - object field
271 */
272static inline JsonParseErrorType
273parse_scalar(JsonLexContext *lex, JsonSemAction *sem)
274{
275 char *val = NULL;
276 json_scalar_action sfunc = sem->scalar;
277 JsonTokenType tok = lex_peek(lex);
278 JsonParseErrorType result;
279
280 /* a scalar must be a string, a number, true, false, or null */
281 if (tok != JSON_TOKEN_STRING && tok != JSON_TOKEN_NUMBER &&
282 tok != JSON_TOKEN_TRUE && tok != JSON_TOKEN_FALSE &&
283 tok != JSON_TOKEN_NULL)
284 return report_parse_error(JSON_PARSE_VALUE, lex);
285
286 /* if no semantic function, just consume the token */
287 if (sfunc == NULL)
288 return json_lex(lex);
289
290 /* extract the de-escaped string value, or the raw lexeme */
291 if (lex_peek(lex) == JSON_TOKEN_STRING)
292 {
293 if (lex->strval != NULL)
294 val = pstrdup(lex->strval->data);
295 }
296 else
297 {
298 int len = (lex->token_terminator - lex->token_start);
299
300 val = palloc(len + 1);
301 memcpy(val, lex->token_start, len);
302 val[len] = '\0';
303 }
304
305 /* consume the token */
306 result = json_lex(lex);
307 if (result != JSON_SUCCESS)
308 return result;
309
310 /* invoke the callback */
311 (*sfunc) (sem->semstate, val, tok);
312
313 return JSON_SUCCESS;
314}
315
316static JsonParseErrorType
317parse_object_field(JsonLexContext *lex, JsonSemAction *sem)

Callers 3

pg_parse_jsonFunction · 0.85
parse_object_fieldFunction · 0.85
parse_array_elementFunction · 0.85

Calls 5

lex_peekFunction · 0.85
report_parse_errorFunction · 0.85
json_lexFunction · 0.85
pstrdupFunction · 0.70
pallocFunction · 0.70

Tested by

no test coverage detected