JSON Structure is a data structure definition language that enforces strict typing, modularity, and determinism.
A portable, C99-compatible JSON Structure schema validator with modern C++11 bindings. Designed for embedded systems and cross-platform development.
vcpkg install json-structure
# With regex support
vcpkg install json-structure[regex]
Then in your CMakeLists.txt:
find_package(json_structure CONFIG REQUIRED)
target_link_libraries(your_target PRIVATE json_structure::json_structure)
# Create build directory
mkdir build && cd build
# Configure (downloads cJSON automatically)
cmake .. -DCMAKE_BUILD_TYPE=Release
# Build
cmake --build .
# Run tests
ctest
| Option | Default | Description |
|---|---|---|
JS_BUILD_TESTS |
ON | Build test suite |
JS_BUILD_EXAMPLES |
ON | Build example programs |
JS_ENABLE_REGEX |
OFF | Enable regex validation (requires PCRE2) |
JS_FETCH_DEPENDENCIES |
ON | Auto-fetch cJSON via FetchContent |
#include <json_structure/json_structure.h>
int main(void) {
const char* schema = "{\"type\": \"string\", \"minLength\": 1}";
const char* instance = "\"hello\"";
js_result_t result;
// Validate schema
if (js_validate_schema(schema, &result)) {
printf("Schema is valid\n");
}
js_result_cleanup(&result);
// Validate instance
if (js_validate_instance(instance, schema, &result)) {
printf("Instance is valid\n");
} else {
for (size_t i = 0; i < result.error_count; i++) {
printf("Error: %s\n", result.errors[i].message);
}
}
js_result_cleanup(&result);
return 0;
}
#include <json_structure/json_structure.hpp>
int main() {
using namespace json_structure;
std::string schema = R"({"type": "string", "minLength": 1})";
std::string instance = R"("hello")";
// Validate schema
auto schemaResult = validate_schema(schema);
if (schemaResult) {
std::cout << "Schema is valid" << std::endl;
}
// Validate instance
auto instanceResult = validate_instance(instance, schema);
if (instanceResult) {
std::cout << "Instance is valid" << std::endl;
} else {
std::cout << "Errors: " << instanceResult.error_summary() << std::endl;
}
// Or use exceptions
try {
InstanceValidator validator;
validator.validate_or_throw(instance, schema);
} catch (const InstanceValidationException& e) {
std::cerr << "Validation failed: " << e.what() << std::endl;
}
return 0;
}
#include <json_structure/json_structure.h>
// Your custom allocator functions
static void* my_malloc(size_t size) { /* ... */ }
static void* my_realloc(void* ptr, size_t size) { /* ... */ }
static void my_free(void* ptr) { /* ... */ }
int main(void) {
// Set custom allocator before any other calls
js_allocator_t alloc = {
.malloc_fn = my_malloc,
.realloc_fn = my_realloc,
.free_fn = my_free,
.user_data = NULL
};
js_set_allocator(alloc);
// Now all allocations use your functions
// ...
return 0;
}
The JSON Structure C SDK is designed to be thread-safe for concurrent validation operations when used correctly:
js_validate_schema(), js_validate_instance(), and related validation functions simultaneously.js_malloc(), js_realloc(), js_free()) are protected by internal synchronization primitives.For thread-safe operation, follow these guidelines:
int main(void) {
// Call js_init() once at program startup, before creating threads
js_init();
// Now safe to create threads that perform validation
// ...
return 0;
}
// Set custom allocator BEFORE any validation operations
js_init_with_allocator(my_allocator);
// Do NOT call js_set_allocator() while validation is in progress
// Ensure all validation threads have finished
// join_all_threads();
// Then call cleanup once
js_cleanup();
js_set_allocator() or js_init_with_allocator() while validation is in progressjs_cleanup() while validation is in progressTo verify thread safety in your application, compile with ThreadSanitizer:
cmake .. -DCMAKE_C_FLAGS="-fsanitize=thread -g" -DCMAKE_CXX_FLAGS="-fsanitize=thread -g"
cmake --build .
ctest
js_type_t - Enumeration of all JSON Structure typesjs_error_t - Single validation errorjs_result_t - Collection of validation resultsjs_allocator_t - Custom allocator interfacejs_schema_validator_init() - Initialize validator with defaultsjs_schema_validate_string() - Validate schema from JSON stringjs_schema_validate() - Validate pre-parsed cJSON schemajs_instance_validator_init() - Initialize validator with defaultsjs_instance_validate_strings() - Validate instance and schema from stringsjs_instance_validate() - Validate pre-parsed cJSON objectsjs_validate_schema() - Quick schema validationjs_validate_instance() - Quick instance validationc/
├── CMakeLists.txt # Main build configuration
├── include/
│ └── json_structure/
│ ├── json_structure.h # Main umbrella header (C)
│ ├── json_structure.hpp # C++ bindings
│ ├── types.h # Core type definitions
│ ├── error_codes.h # Error enumerations
│ ├── schema_validator.h # Schema validator API
│ └── instance_validator.h # Instance validator API
├── src/
│ ├── types.c # Type implementations
│ ├── error_codes.c # Error string functions
│ ├── schema_validator.c # Schema validation logic
│ ├── instance_validator.c # Instance validation logic
│ └── json_source_locator.c # Source location tracking
├── tests/
│ ├── main.c # Test runner
│ ├── test_types.c
│ ├── test_schema_validator.c
│ └── test_instance_validator.c
└── examples/
├── validate_schema.c # C example
└── validate_schema.cpp # C++ example
MIT License. See LICENSE for details.
See CONTRIBUTING.md for contribution guidelines.