Skip to content

Add more codes in it - #188

Open
MedChronicle wants to merge 2 commits into
FreeRTOS:mainfrom
MedChronicle:main
Open

MedChronicle wants to merge 2 commits into
FreeRTOS:mainfrom
MedChronicle:main

Conversation

@MedChronicle

Copy link
Copy Markdown

Add extension helpers: typed getters, string decoding, counting/iteration, depth, minify and prettify

Description

Adds a new optional extension module (core_json_ext.c / core_json_ext.h) on top of the public coreJSON API. core_json.c and core_json.h are not modified. The module uses only JSON_Validate, JSON_SearchConst and JSON_Iterate, and never allocates memory (the caller supplies all output buffers).

New functions:

  • JSON_GetInt, JSON_GetDouble, JSON_GetBool: typed getters with type and overflow checks (LLONG_MIN handled correctly).
  • JSON_GetString: decodes escapes (\n, \", \uXXXX including surrogate pairs) to UTF-8 and NUL-terminates. Rejects \u0000.
  • JSON_Exists, JSON_GetType: check whether a key exists and what type it is.
  • JSON_Count, JSON_ForEach: count elements or iterate an object/array with a callback (early stop supported).
  • JSON_GetDepth: maximum nesting depth of a document.
  • JSON_Minify, JSON_Prettify: compact or pretty-print into a caller buffer. *outLength always reports the required size, so callers can resize on JSONBadParameter.
  • JSON_StatusToString, JSON_TypeToString: readable names for status and type codes.

Build change: core_json_ext.c is added to JSON_SOURCES in jsonFilePaths.cmake. Linking requires -lm (strtod, isinf).

Also adds examples/demo.c (usage example) and examples/test_ext.c (ad-hoc test program).

Test Steps

  1. Build and run the test program with strict warnings and sanitizers:
    gcc -std=c99 -Wall -Wextra -Wpedantic -g -fsanitize=address,undefined -Isource/include source/core_json.c source/core_json_ext.c examples/test_ext.c -o test_ext -lm && ./test_ext
  2. Expected output ends with ALL TESTS PASSED.
  3. Build and run the demo:
    gcc -std=c99 -Wall -Wextra -Isource/include source/core_json.c source/core_json_ext.c examples/demo.c -o demo -lm && ./demo
  4. Expected output includes name = Sarthak, age = 20, tags = 3 items, missing -> JSONNotFound, followed by the pretty-printed JSON.

Checklist:

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

None.

By submitting this pull

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant