Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
JSONUtils Namespace Reference

Set of lightweight JSON parsing and formatting helper utilities. More...

Functions

std::string indent (int level)
 Generates indentation string according to spacing level.
std::string quote (const std::string &value)
 Wraps a string in double quotes.
std::string vec3ToJson (const glm::vec3 &v)
 Serializes a glm::vec3 to a JSON array.
std::string vec4ToJson (const glm::vec4 &v)
 Serializes a glm::vec4 to a JSON array.
std::string extractStringValue (const std::string &source, const std::string &key)
 Finds and extracts a string value associated with a JSON key.
bool extractFloatArray (const std::string &source, const std::string &key, float *values, size_t count)
 Finds and extracts an array of floats associated with a JSON key.
bool extractFloatValue (const std::string &source, const std::string &key, float &value)
 Finds and extracts a single float value associated with a JSON key.
std::vector< std::string > extractEntityObjects (const std::string &source)
 Extracts individual entity JSON blocks from a main scene array.
bool extractIntVector (const std::string &source, const std::string &key, std::vector< int > &values)
 Finds and extracts an array of integers associated with a JSON key.
bool extractStringVector (const std::string &source, const std::string &key, std::vector< std::string > &values)
 Finds and extracts an array of quoted strings associated with a JSON key.
std::string extractSubObject (const std::string &source, const std::string &key)
 Extracts a sub-object JSON string (enclosed in { ... }) for a given key.

Detailed Description

Set of lightweight JSON parsing and formatting helper utilities.

Helper utilities for hand-parsing and formatting simple JSON files.

Function Documentation

◆ extractEntityObjects()

std::vector< std::string > JSONUtils::extractEntityObjects ( const std::string & source)

Extracts individual entity JSON blocks from a main scene array.

Extracts individual entity JSON objects.

Parameters
sourceFull JSON array text.
Returns
Vector of individual entity JSON strings.
Parameters
sourceRaw JSON source.
Returns
Vector of entity JSON strings.

◆ extractFloatArray()

bool JSONUtils::extractFloatArray ( const std::string & source,
const std::string & key,
float * values,
size_t count )

Finds and extracts an array of floats associated with a JSON key.

Extract float array values corresponding to target key.

Parameters
sourceJSON text source.
keySearch key.
valuesDestination buffer.
countCount of floats.
Returns
True if successful, false otherwise.
Parameters
sourceRaw JSON source.
keyTarget JSON property key.
valuesDestination buffer.
countMaximum array values to read.
Returns
True if successful, false otherwise.

◆ extractFloatValue()

bool JSONUtils::extractFloatValue ( const std::string & source,
const std::string & key,
float & value )

Finds and extracts a single float value associated with a JSON key.

Extract single float value corresponding to target key.

Parameters
sourceJSON text source.
keySearch key.
valueDestination float reference.
Returns
True if successful, false otherwise.
Parameters
sourceRaw JSON source.
keyTarget JSON property key.
valueDestination variable reference.
Returns
True if successful, false otherwise.

◆ extractIntVector()

bool JSONUtils::extractIntVector ( const std::string & source,
const std::string & key,
std::vector< int > & values )

Finds and extracts an array of integers associated with a JSON key.

Parameters
sourceJSON text source.
keySearch key.
valuesDestination vector.
Returns
True if successful, false otherwise.

◆ extractStringValue()

std::string JSONUtils::extractStringValue ( const std::string & source,
const std::string & key )

Finds and extracts a string value associated with a JSON key.

Extract string values corresponding to target key.

Parameters
sourceJSON text source.
keySearch key.
Returns
Extracted string value.
Parameters
sourceRaw JSON source.
keyTarget JSON property key.
Returns
Extracted value.

◆ extractStringVector()

bool JSONUtils::extractStringVector ( const std::string & source,
const std::string & key,
std::vector< std::string > & values )

Finds and extracts an array of quoted strings associated with a JSON key.

Finds and extracts an array of quoted string values associated with a JSON key. Handles both compact (["a","b"]) and pretty-printed arrays.

Parameters
sourceJSON text source.
keySearch key.
valuesDestination vector of strings.
Returns
True if the key was found and at least one string was parsed.

◆ extractSubObject()

std::string JSONUtils::extractSubObject ( const std::string & source,
const std::string & key )

Extracts a sub-object JSON string (enclosed in { ... }) for a given key.

Parameters
sourceRaw JSON text.
keyProperty key.
Returns
Extracted JSON object string including surrounding braces, or empty string if not found.

◆ indent()

std::string JSONUtils::indent ( int level)

Generates indentation string according to spacing level.

Returns a string of spaces representing indentation level.

Parameters
levelIndent level.
Returns
Indent spacing.
Parameters
levelIndentation multiplier level.
Returns
Space character string.

◆ quote()

std::string JSONUtils::quote ( const std::string & value)

Wraps a string in double quotes.

Quotes a string value.

Parameters
valueRaw string.
Returns
Quoted string.

◆ vec3ToJson()

std::string JSONUtils::vec3ToJson ( const glm::vec3 & v)

Serializes a glm::vec3 to a JSON array.

Converts glm::vec3 value to a JSON array representation.

Parameters
v3D vector.
Returns
JSON formatted array string.
Parameters
vInput vector.
Returns
JSON array representation.

◆ vec4ToJson()

std::string JSONUtils::vec4ToJson ( const glm::vec4 & v)

Serializes a glm::vec4 to a JSON array.

Converts glm::vec4 value to a JSON array representation.

Parameters
v4D vector.
Returns
JSON formatted array string.
Parameters
vInput vector.
Returns
JSON array representation.