JSON Parser Test Application
Overview
This application demonstrates the usage of a JSON lexer and parser library, specifically showcasing how to use the ParsedJsonDataSet
and JsonRef
classes to create and access JSON information. The program is designed as a test application that parses a complex JSON structure and demonstrates various access methods and type checking capabilities.
Application Details
- Application Name: JsonParseTest
- Main File: main.cpp
- Purpose: Parse Test demonstration for JSON Lexer classes
Features
The application demonstrates the following JSON parsing capabilities:
1. JSON Data Structure
The program works with a complex nested JSON structure that includes:
- Boolean values (
SUCCESS
, b3
, aftaav
)
- Null values (
ACTION
, b4
)
- Numeric values (integers and floats)
- String values
- Nested objects (
JDATA
, bar
, b2
, s4
, wa
)
- Arrays (
av
, a
, z
)
- Mixed-type arrays containing numbers, strings, objects, and nested arrays
2. Access Methods
The application demonstrates two primary methods for accessing JSON data:
Explicit Type Enforcement
int32_t j = pjd.object("wa").name("a")[5].name("z")[1];
const char *str = pjd.object("wa").name("a")[4];
NBString nbs = pjd.object(
"JDATA").object(
"bar").object(
"b2").object(
"s4").name(
"Sub4a");
Lightweight alternative to C++ CString class.
Definition nbstring.h:118
Simplified Access (Without Type Enforcement)
j = pjd("wa")("a")[5]("z")[1];
str = pjd("wa")("a")[4];
nbs = (const char*)(pjd("JDATA")("bar")("b2")("s4")("Sub4a"));
3. Type Checking
The application includes comprehensive type checking capabilities:
IsNumber()
- Check if value is numeric
IsObject()
- Check if value is a JSON object
IsNull()
- Check if value is null
IsArray()
- Check if value is an array
IsBool()
- Check if value is boolean
IsString()
- Check if value is a string
4. Error Handling
The application demonstrates proper error handling for:
- Non-existent objects and properties
- Invalid parse attempts
- Validation using
JsonRef.Valid()
method
JSON Structure Example
The embedded JSON data structure includes:
{
"SUCCESS": true,
"ACTION": null,
"JDATA": {
"foo": 7,
"bar": {
"b1": "Sb1",
"b2": {
"s1": "Sub1",
"s2": "Sub2",
"s3": "Sub3",
"s4": {
"Sub4a": "Test4a",
"Sub4b": "Test4b"
},
"s5": "Sub5",
"s6": "Sub6"
},
"b3": true,
"b4": null
},
"bogus": "Bstring",
"nubog": 1234.57,
"av": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
"aftaav": true
},
"wa": {
"a": [1, 2, 3, 4, "bob",
{"b": "c", "z": [1, 2, 3]},
[12, 10, 9, "zoro"],
93]
}
}
Key Classes
ParsedJsonDataSet
- Main class for parsing and storing JSON data
- Supports initialization from character array data
- Provides
PrintObject()
method for debugging
- Supports both explicit and simplified access methods
JsonRef
- Reference class for accessing JSON elements
- Provides type checking methods
- Includes validation through
Valid()
method
- Supports automatic type conversion
Usage Pattern
- Initialize: Create a
ParsedJsonDataSet
object with JSON data
- Access: Use either explicit or simplified syntax to navigate the JSON structure
- Validate: Check if the parse was successful using
Valid()
- Type Check: Verify data types before processing
- Extract: Convert to appropriate C++ types (int32_t, const char*, NBString, etc.)
System Requirements
- Embedded system environment (references to DHCP, network waiting)
- NetBurner platform (references to NBString, system diagnostics)
- Real-time operating system (OSTimeDly, TICKS_PER_SECOND)
Build Dependencies
Example Output
The application provides detailed console output showing:
- Parsed integer values from nested structures
- String extraction from various JSON locations
- Error handling for non-existent properties
- Type validation results
- Success/failure status of parse operations