Utility functions for working with JSON data using nlohmann::json.
More...
#include <nlohmann/json.hpp>
#include <string>
#include <algorithm>
Go to the source code of this file.
|
float | JSON_ParseNumber (const nlohmann::json &item) |
| Parses a number from a JSON item.
|
|
float | JSON_ParseNumber (const nlohmann::json &item, const std::string keyName) |
| Parses a number from a JSON object using a specified key.
|
|
std::string | JSON_ParseString (const nlohmann::json &item) |
| Parses a string from a JSON item.
|
|
std::string | JSON_ParseString (const nlohmann::json &item, const std::string keyName) |
| Parses a string from a JSON object using a specified key.
|
|
std::string | JSON_ParseAsString (const nlohmann::json &item) |
| Converts a JSON item into a string representation.
|
|
std::string | JSON_ParseAsString (const nlohmann::json &item, const std::string keyName) |
| Converts a specific JSON object key's value into a string representation.
|
|
bool | JSON_isNumber (const nlohmann::json &item) |
| Checks if a JSON item is a number.
|
|
bool | JSON_isNumber (const nlohmann::json &item, const std::string keyName) |
| Checks if a specific key in a JSON object is a number.
|
|
bool | JSON_FromFile (nlohmann::json &data, std::string targetFile) |
| Loads JSON data from a file.
|
|
bool | JSON_ToFile (nlohmann::json &data, std::string targetFile) |
| Saves JSON data to a file.
|
|
void | JSON_Unique (nlohmann::json &jsonArray, const std::string &key) |
| Removes duplicate entries in a JSON array based on a unique key.
|
|
size_t | JSON_Find (const std::string &keyValueDumped, const std::string &keyName, const nlohmann::json &arr) |
| Finds the index of an object in a JSON array based on a key-value pair.
|
|
void | JSON_UpdateArray (nlohmann::json &present, const nlohmann::json &fetched, const std::string &uniqueKeyName) |
| Updates an existing JSON array by merging elements based on a unique key.
|
|
bool | parseDateTime (const std::string &datetime, std::tm &tm) |
| Parses a date-time string into a tm structure.
|
|
Utility functions for working with JSON data using nlohmann::json.
◆ JSONTOOLS_H
◆ JSON_Find()
size_t JSON_Find |
( |
const std::string & | keyValueDumped, |
|
|
const std::string & | keyName, |
|
|
const nlohmann::json & | arr ) |
Finds the index of an object in a JSON array based on a key-value pair.
- Parameters
-
keyValueDumped | The JSON string value to search for. |
keyName | The key whose value is being searched. |
arr | The JSON array to search in. |
- Returns
- The index (1-based) of the matching object, or 0 if not found.
◆ JSON_FromFile()
bool JSON_FromFile |
( |
nlohmann::json & | data, |
|
|
std::string | targetFile ) |
Loads JSON data from a file.
- Parameters
-
data | The JSON object to populate. |
targetFile | The file path to read from. |
- Returns
- True if loading succeeds, false otherwise.
◆ JSON_isNumber() [1/2]
bool JSON_isNumber |
( |
const nlohmann::json & | item | ) |
|
Checks if a JSON item is a number.
- Parameters
-
item | The JSON item to check. |
- Returns
- True if the item is a number, false otherwise.
◆ JSON_isNumber() [2/2]
bool JSON_isNumber |
( |
const nlohmann::json & | item, |
|
|
const std::string | keyName ) |
Checks if a specific key in a JSON object is a number.
- Parameters
-
item | The JSON object. |
keyName | The key to check. |
- Returns
- True if the key's value is a number, false otherwise.
◆ JSON_ParseAsString() [1/2]
std::string JSON_ParseAsString |
( |
const nlohmann::json & | item | ) |
|
Converts a JSON item into a string representation.
- Parameters
-
item | The JSON item to convert. |
- Returns
- The JSON item as a string.
◆ JSON_ParseAsString() [2/2]
std::string JSON_ParseAsString |
( |
const nlohmann::json & | item, |
|
|
const std::string | keyName ) |
Converts a specific JSON object key's value into a string representation.
- Parameters
-
item | The JSON object. |
keyName | The key whose value should be converted. |
- Returns
- The JSON value as a string.
◆ JSON_ParseNumber() [1/2]
float JSON_ParseNumber |
( |
const nlohmann::json & | item | ) |
|
Parses a number from a JSON item.
- Parameters
-
item | The JSON item to parse. |
- Returns
- The parsed float value, or 0 if parsing fails.
◆ JSON_ParseNumber() [2/2]
float JSON_ParseNumber |
( |
const nlohmann::json & | item, |
|
|
const std::string | keyName ) |
Parses a number from a JSON object using a specified key.
- Parameters
-
item | The JSON object. |
keyName | The key whose value should be parsed. |
- Returns
- The parsed float value, or 0 if parsing fails.
◆ JSON_ParseString() [1/2]
std::string JSON_ParseString |
( |
const nlohmann::json & | item | ) |
|
Parses a string from a JSON item.
- Parameters
-
item | The JSON item to parse. |
- Returns
- The parsed string, or an empty string if parsing fails.
◆ JSON_ParseString() [2/2]
std::string JSON_ParseString |
( |
const nlohmann::json & | item, |
|
|
const std::string | keyName ) |
Parses a string from a JSON object using a specified key.
- Parameters
-
item | The JSON object. |
keyName | The key whose value should be parsed. |
- Returns
- The parsed string, or an empty string if parsing fails.
◆ JSON_ToFile()
bool JSON_ToFile |
( |
nlohmann::json & | data, |
|
|
std::string | targetFile ) |
Saves JSON data to a file.
- Parameters
-
data | The JSON object to save. |
targetFile | The file path to write to. |
- Returns
- True if saving succeeds, false otherwise.
◆ JSON_Unique()
void JSON_Unique |
( |
nlohmann::json & | jsonArray, |
|
|
const std::string & | key ) |
Removes duplicate entries in a JSON array based on a unique key.
- Parameters
-
jsonArray | The JSON array to process. |
key | The key used to determine uniqueness. |
◆ JSON_UpdateArray()
void JSON_UpdateArray |
( |
nlohmann::json & | present, |
|
|
const nlohmann::json & | fetched, |
|
|
const std::string & | uniqueKeyName ) |
Updates an existing JSON array by merging elements based on a unique key.
- Parameters
-
present | The existing JSON array. |
fetched | The new JSON array to merge. |
uniqueKeyName | The key used to determine uniqueness. |
◆ parseDateTime()
bool parseDateTime |
( |
const std::string & | datetime, |
|
|
std::tm & | tm ) |
Parses a date-time string into a tm structure.
- Parameters
-
datetime | The date-time string in the format "%Y-%m-%d %H:%M:%S". |
tm | The structure to store the parsed time. |
- Returns
- True if parsing succeeds, false otherwise.