Weather App
 
Loading...
Searching...
No Matches
JSONTools.h File Reference

Utility functions for working with JSON data using nlohmann::json. More...

#include <nlohmann/json.hpp>
#include <string>
#include <algorithm>
Include dependency graph for JSONTools.h:

Go to the source code of this file.

Macros

#define JSONTOOLS_H
 

Functions

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.
 

Detailed Description

Utility functions for working with JSON data using nlohmann::json.

Macro Definition Documentation

◆ JSONTOOLS_H

#define JSONTOOLS_H

Function Documentation

◆ 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
keyValueDumpedThe JSON string value to search for.
keyNameThe key whose value is being searched.
arrThe 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
dataThe JSON object to populate.
targetFileThe 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
itemThe 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
itemThe JSON object.
keyNameThe 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
itemThe 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
itemThe JSON object.
keyNameThe 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
itemThe 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
itemThe JSON object.
keyNameThe 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
itemThe 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
itemThe JSON object.
keyNameThe 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
dataThe JSON object to save.
targetFileThe 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
jsonArrayThe JSON array to process.
keyThe 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
presentThe existing JSON array.
fetchedThe new JSON array to merge.
uniqueKeyNameThe 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
datetimeThe date-time string in the format "%Y-%m-%d %H:%M:%S".
tmThe structure to store the parsed time.
Returns
True if parsing succeeds, false otherwise.