iceberg-cpp
Loading...
Searching...
No Matches
Functions
json_util_internal.h File Reference

Internal utilities for JSON serialization and deserialization. More...

#include <optional>
#include <nlohmann/json.hpp>
#include "iceberg/result.h"
#include "iceberg/util/macros.h"

Go to the source code of this file.

Functions

template<typename T >
void iceberg::SetOptionalField (nlohmann::json &json, std::string_view key, const std::optional< T > &value)
 
template<typename T >
requires requires(const T& t) { t.empty(); }
void iceberg::SetContainerField (nlohmann::json &json, std::string_view key, const T &value)
 
void iceberg::SetOptionalStringField (nlohmann::json &json, std::string_view key, const std::string &value)
 
std::string iceberg::SafeDumpJson (const nlohmann::json &json)
 
template<typename T >
Result< T > iceberg::GetTypedJsonValue (const nlohmann::json &json)
 
template<typename T >
Result< T > iceberg::GetJsonValueImpl (const nlohmann::json &json, std::string_view key)
 
template<typename T >
Result< std::optional< T > > iceberg::GetJsonValueOptional (const nlohmann::json &json, std::string_view key)
 
template<typename T >
Result< T > iceberg::GetJsonValue (const nlohmann::json &json, std::string_view key)
 
template<typename T >
Result< T > iceberg::GetJsonValueOrDefault (const nlohmann::json &json, std::string_view key, T default_value=T{})
 
template<typename T >
nlohmann::json::array_t iceberg::ToJsonList (const std::vector< T > &list)
 Convert a list of items to a json array.
 
template<typename T >
nlohmann::json::array_t iceberg::ToJsonList (const std::vector< std::shared_ptr< T > > &list)
 Overload of the above function for a list of shared pointers.
 
template<typename T >
Result< std::vector< T > > iceberg::FromJsonList (const nlohmann::json &json, std::string_view key, const std::function< Result< T >(const nlohmann::json &)> &from_json)
 Parse a list of items from a JSON object.
 
template<typename T >
Result< std::vector< std::shared_ptr< T > > > iceberg::FromJsonList (const nlohmann::json &json, std::string_view key, const std::function< Result< std::shared_ptr< T > >(const nlohmann::json &)> &from_json)
 Parse a list of items from a JSON object.
 
template<typename T >
nlohmann::json::object_t iceberg::ToJsonMap (const std::unordered_map< std::string, T > &map)
 Convert a map of type <std::string, T> to a json object.
 
template<typename T >
nlohmann::json::object_t iceberg::ToJsonMap (const std::unordered_map< std::string, std::shared_ptr< T > > &map)
 Overload of the above function for a map of type <std::string, std::shared_ptr<T>>.
 
template<typename T = std::string>
Result< std::unordered_map< std::string, T > > iceberg::FromJsonMap (const nlohmann::json &json, std::string_view key, const std::function< Result< T >(const nlohmann::json &)> &from_json=[](const nlohmann::json &json) -> Result< T > { static_assert(std::is_same_v< T, std::string >, "T must be std::string");try { return json.get< std::string >();} catch(const std::exception &ex) { return JsonParseError("Cannot parse {} to a string value: {}", SafeDumpJson(json), ex.what());} })
 Parse a map of type <std::string, T> from a JSON object.
 

Detailed Description

Internal utilities for JSON serialization and deserialization.

Function Documentation

◆ FromJsonList() [1/2]

template<typename T >
Result< std::vector< std::shared_ptr< T > > > iceberg::FromJsonList ( const nlohmann::json &  json,
std::string_view  key,
const std::function< Result< std::shared_ptr< T > >(const nlohmann::json &)> &  from_json 
)

Parse a list of items from a JSON object.

Parameters
[in]jsonThe JSON object to parse.
[in]keyThe key to parse.
[in]from_jsonThe function to parse an item from a JSON object.
Returns
The list of items.

◆ FromJsonList() [2/2]

template<typename T >
Result< std::vector< T > > iceberg::FromJsonList ( const nlohmann::json &  json,
std::string_view  key,
const std::function< Result< T >(const nlohmann::json &)> &  from_json 
)

Parse a list of items from a JSON object.

Parameters
[in]jsonThe JSON object to parse.
[in]keyThe key to parse.
[in]from_jsonThe function to parse an item from a JSON object.
Returns
The list of items.

◆ FromJsonMap()

template<typename T = std::string>
Result< std::unordered_map< std::string, T > > iceberg::FromJsonMap ( const nlohmann::json &  json,
std::string_view  key,
const std::function< Result< T >(const nlohmann::json &)> &  from_json = [](const nlohmann::json& json) -> Result<T> { static_assert(std::is_same_v<T, std::string>, "T must be std::string"); try { return json.get<std::string>(); } catch (const std::exception& ex) { return JsonParseError("Cannot parse {} to a string value: {}", SafeDumpJson(json),                              ex.what()); } } 
)

Parse a map of type <std::string, T> from a JSON object.

Parameters
[in]jsonThe JSON object to parse.
[in]keyThe key to parse.
[in]from_jsonThe function to parse an item from a JSON object.
Returns
The map of items.

◆ ToJsonList()

template<typename T >
nlohmann::json::array_t iceberg::ToJsonList ( const std::vector< T > &  list)

Convert a list of items to a json array.

Note that ToJson(const T&) is required for this function to work.

◆ ToJsonMap()

template<typename T >
nlohmann::json::object_t iceberg::ToJsonMap ( const std::unordered_map< std::string, T > &  map)

Convert a map of type <std::string, T> to a json object.

Note that ToJson(const T&) is required for this function to work.