AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
Functions
alexaClientSDK::avsCommon::utils::json::jsonUtils Namespace Reference

Functions

static std::string getTag ()
 
bool findNode (const rapidjson::Value &jsonNode, const std::string &key, rapidjson::Value::ConstMemberIterator *iteratorPtr)
 
bool parseJSON (const std::string &jsonContent, rapidjson::Document *document)
 
bool convertToValue (const rapidjson::Value &documentNode, std::string *value)
 
bool convertToValue (const rapidjson::Value &documentNode, int64_t *value)
 
bool convertToValue (const rapidjson::Value &documentNode, uint64_t *value)
 
bool convertToValue (const rapidjson::Value &documentNode, bool *value)
 
bool convertToValue (const rapidjson::Value &documentNode, double *value)
 
template<typename T >
bool retrieveValue (const rapidjson::Value &jsonNode, const std::string &key, T *value)
 
template<typename T >
bool retrieveValue (const std::string jsonString, const std::string &key, T *value)
 
bool jsonArrayExists (const rapidjson::Value &parsedDocument, const std::string &key)
 
template<class CollectionT >
CollectionT retrieveStringArray (const std::string &jsonString, const std::string &key)
 
template<class CollectionT >
CollectionT retrieveStringArray (const std::string &jsonString)
 
template<class CollectionT >
CollectionT retrieveStringArray (const rapidjson::Value &value)
 
template<class CollectionT >
std::string convertToJsonString (const CollectionT &elements)
 
std::map< std::string, std::string > retrieveStringMap (const rapidjson::Value &value, const std::string &key)
 
void retrieveStringMapFromArray (const rapidjson::Value &value, const std::string &key, std::map< std::string, std::string > &elements)
 
bool retrieveArrayOfStringMapFromArray (const rapidjson::Value &value, const std::string &key, std::vector< std::map< std::string, std::string >> &elements)
 
template<>
std::vector< std::string > retrieveStringArray< std::vector< std::string > > (const std::string &jsonString, const std::string &key)
 
template<>
std::vector< std::string > retrieveStringArray< std::vector< std::string > > (const std::string &jsonString)
 
template<>
std::vector< std::string > retrieveStringArray< std::vector< std::string > > (const rapidjson::Value &value)
 
template<>
std::string convertToJsonString< std::vector< std::string > > (const std::vector< std::string > &elements)
 

Function Documentation

◆ convertToJsonString()

template<class CollectionT >
std::string alexaClientSDK::avsCommon::utils::json::jsonUtils::convertToJsonString ( const CollectionT &  elements)

Convert a string collection into a JSON string array representation.

Parameters
elementsA collection that will be converted to the JSON array representation.
Returns
The JSON string array representation if it succeeds; otherwise, return an empty string.

◆ convertToJsonString< std::vector< std::string > >()

template<>
std::string alexaClientSDK::avsCommon::utils::json::jsonUtils::convertToJsonString< std::vector< std::string > > ( const std::vector< std::string > &  elements)

◆ convertToValue() [1/5]

bool alexaClientSDK::avsCommon::utils::json::jsonUtils::convertToValue ( const rapidjson::Value &  documentNode,
std::string *  value 
)

Converts a given rapidjson document node to a string. The node must be either of Object or String type.

Parameters
documentNodeA logical node within a parsed JSON document which rapidjson understands.
[out]valueThe output parameter which will be assigned the string value.
Returns
true If the node was converted to a string ok, false otherwise.

◆ convertToValue() [2/5]

bool alexaClientSDK::avsCommon::utils::json::jsonUtils::convertToValue ( const rapidjson::Value &  documentNode,
int64_t *  value 
)

Converts a given rapidjson value node to a 64-bit signed integer. The node must be Int64 type.

Parameters
documentNodeA logical node within a parsed JSON document which rapidjson understands.
[out]valueThe output parameter which will be assigned the int64_t value.
Returns
true If the node was successfully converted, false otherwise.

◆ convertToValue() [3/5]

bool alexaClientSDK::avsCommon::utils::json::jsonUtils::convertToValue ( const rapidjson::Value &  documentNode,
uint64_t *  value 
)

Converts a given rapidjson value node to a 64-bit unsigned integer. The node must be unsigned int type.

Parameters
documentNodeA logical node within a parsed JSON document which rapidjson understands.
[out]valueThe output parameter which will be assigned the unsigned int value.
Returns
true If the node was successfully converted, false otherwise.

◆ convertToValue() [4/5]

bool alexaClientSDK::avsCommon::utils::json::jsonUtils::convertToValue ( const rapidjson::Value &  documentNode,
bool *  value 
)

Converts a given rapidjson value node to a bool. The node must be Bool type.

Parameters
documentNodeA logical node within a parsed JSON document which rapidjson understands.
[out]valueThe output parameter which will be assigned the bool value.
Returns
true If the node was successfully converted, false otherwise.

◆ convertToValue() [5/5]

bool alexaClientSDK::avsCommon::utils::json::jsonUtils::convertToValue ( const rapidjson::Value &  documentNode,
double *  value 
)

Converts a given rapidjson value node to a double. The node must be double type.

Parameters
documentNodeA logical node within a parsed JSON document which rapidjson understands.
[out]valueThe output parameter which will be assigned the double value.
Returns
true If the node was successfully converted, false otherwise.

◆ findNode()

bool alexaClientSDK::avsCommon::utils::json::jsonUtils::findNode ( const rapidjson::Value &  jsonNode,
const std::string &  key,
rapidjson::Value::ConstMemberIterator *  iteratorPtr 
)

Give a rapidjson::Value object, this function will find a direct child node that matches the key. Copy constructors are hidden for rapidjson::Value objects, so iterators are used to retrieve the value of the node.

Parameters
jsonNodeThe JSON node object.
keyThe key of the underlying JSON content we wish to acquire the value of.
[out]iteratorPtrA pointer to a ConstMemberIterator, which will contain the Value.
Returns
true if the lookup is successful, false otherwise.

◆ getTag()

static std::string alexaClientSDK::avsCommon::utils::json::jsonUtils::getTag ( )
inlinestatic

Returns the tag for logging purposes.

◆ jsonArrayExists()

bool alexaClientSDK::avsCommon::utils::json::jsonUtils::jsonArrayExists ( const rapidjson::Value &  parsedDocument,
const std::string &  key 
)

Queries whether an array object exists as a child of a pre-parsed rapidjson::Value element.

Parameters
parsedDocumentThe Value within which the array should be looked for.
keyThe name of the array being looked for.
Returns
Whether a child element of array type was found.

◆ parseJSON()

bool alexaClientSDK::avsCommon::utils::json::jsonUtils::parseJSON ( const std::string &  jsonContent,
rapidjson::Document *  document 
)

Invoke a rapidjson parse on a JSON string.

Parameters
jsonContentThe JSON content to be parsed.
[out]documentThe output parameter rapidjson document.
Returns
true If the JSON content was valid, false otherwise.

◆ retrieveArrayOfStringMapFromArray()

bool alexaClientSDK::avsCommon::utils::json::jsonUtils::retrieveArrayOfStringMapFromArray ( const rapidjson::Value &  value,
const std::string &  key,
std::vector< std::map< std::string, std::string >> &  elements 
)

Retrieve array of string map from array of a rapidjson value with the given key.

Parameters
valueA Value that represents a array of string map.
keyThe name of the array being looked for.
elementsThe output collection which will contain all extracted elements.
Returns
A bool indicating success or failure retrieving elements

◆ retrieveStringArray() [1/3]

template<class CollectionT >
CollectionT alexaClientSDK::avsCommon::utils::json::jsonUtils::retrieveStringArray ( const std::string &  jsonString,
const std::string &  key 
)

Find and retrieve a string collection from the provided stringified JSON.

Example:

auto elements = retrieveStringArray(R"({"key":["element1", "element2"]})", "key");
Template Parameters
CollectionTThe collection type.
Parameters
jsonStringThe input JSON string.
keyThe name of the array being looked for.
Returns
The output collection which will contain all extracted elements. An empty collection if jsonString is malformed, contains non-string elements, or if array elements with key is not found.
Note
This function will only look at the first level to find the array with the key.

◆ retrieveStringArray() [2/3]

template<class CollectionT >
CollectionT alexaClientSDK::avsCommon::utils::json::jsonUtils::retrieveStringArray ( const std::string &  jsonString)

Convert a JSON array of strings into a string collection.

Parameters
jsonStringThe input JSON. This should represent the array string not the entire document. E.g.:
auto elements = retrieveStringArray(R"(["element1", "element2"])");
Returns
The output string collection which will contain all extracted elements. An empty collection if jsonString is malformed, or contains non-string elements.

◆ retrieveStringArray() [3/3]

template<class CollectionT >
CollectionT alexaClientSDK::avsCommon::utils::json::jsonUtils::retrieveStringArray ( const rapidjson::Value &  value)

Retrieve string elements from a rapidjson value.

Parameters
valueA Value that represents an array of strings.
Returns
The output string collection which will contain all extracted elements. An empty collection if value contains non-string elements.

◆ retrieveStringArray< std::vector< std::string > >() [1/3]

template<>
std::vector<std::string> alexaClientSDK::avsCommon::utils::json::jsonUtils::retrieveStringArray< std::vector< std::string > > ( const std::string &  jsonString,
const std::string &  key 
)

◆ retrieveStringArray< std::vector< std::string > >() [2/3]

template<>
std::vector<std::string> alexaClientSDK::avsCommon::utils::json::jsonUtils::retrieveStringArray< std::vector< std::string > > ( const rapidjson::Value &  value)

◆ retrieveStringArray< std::vector< std::string > >() [3/3]

template<>
std::vector<std::string> alexaClientSDK::avsCommon::utils::json::jsonUtils::retrieveStringArray< std::vector< std::string > > ( const std::string &  jsonString)

◆ retrieveStringMap()

std::map<std::string, std::string> alexaClientSDK::avsCommon::utils::json::jsonUtils::retrieveStringMap ( const rapidjson::Value &  value,
const std::string &  key 
)

Retrieve a string map from a rapidjson value with the given key.

Parameters
valueA Value that represents a string map.
keyThe name of the array being looked for.
Returns
The output collection which will contain all extracted elements. An empty collection if value contains non-string elements or if the key was not found.

◆ retrieveStringMapFromArray()

void alexaClientSDK::avsCommon::utils::json::jsonUtils::retrieveStringMapFromArray ( const rapidjson::Value &  value,
const std::string &  key,
std::map< std::string, std::string > &  elements 
)

Retrieve string map from array of a rapidjson value with the given key.

Parameters
valueA Value that represents a array of string map.
keyThe name of the array being looked for.
elementsThe output collection which will contain all extracted elements. An empty collection if value contains non-string elements or if the key was not found.

◆ retrieveValue() [1/2]

template<typename T >
bool alexaClientSDK::avsCommon::utils::json::jsonUtils::retrieveValue ( const rapidjson::Value &  jsonNode,
const std::string &  key,
T *  value 
)

A template function to find and retrieve a value of type T from a direct child of the provided rapidjson::Value object. The type T must have an overload of the function convertToValue.

Parameters
jsonNodeA logical node within a parsed JSON document which rapidjson understands.
keyThe key in which to look for the value.
[out]valueThe output parameter which will be assigned the value of type T if the function succeeds. No modification is done in case of failure.
Returns
true If the value was successfully retrieved, false otherwise.

◆ retrieveValue() [2/2]

template<typename T >
bool alexaClientSDK::avsCommon::utils::json::jsonUtils::retrieveValue ( const std::string  jsonString,
const std::string &  key,
T *  value 
)

A template function to find and retrieve a value of type T from the provided JSON string. The provided string will first be parsed into a JSON document, after which the associated value T will be retrieved. The type T must have an overload of the function convertToValue.

Parameters
jsonStringA JSON string.
keyThe key in which to look for the value.
[out]valueThe output parameter which will be assigned the value of type T if the function succeeds. No modification is done in case of failure.
Returns
true If the value was successfully retrieved, false otherwise.

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0