AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
Public Member Functions | List of all members
alexaClientSDK::avsCommon::utils::json::JsonGenerator Class Reference

#include <JSONGenerator.h>

Public Member Functions

 JsonGenerator ()
 
 ~JsonGenerator ()=default
 
bool isFinalized ()
 
bool startObject (const std::string &key)
 
bool finishObject ()
 
bool startArray (const std::string &key)
 
bool startArrayElement ()
 
bool finishArrayElement ()
 
bool finishArray ()
 
template<typename CollectionT , typename ValueT = typename CollectionT::value_type>
bool addStringArray (const std::string &key, const CollectionT &collection)
 
template<typename CollectionT , typename ValueT = typename CollectionT::value_type>
bool addMembersArray (const std::string &key, const CollectionT &collection)
 
bool addRawJsonMember (const std::string &key, const std::string &json, bool validate=true)
 
template<typename CollectionArrayT , typename CollectionValueT = typename CollectionArrayT::value_type, typename ValueT = typename CollectionValueT::value_type>
bool addCollectionOfStringArray (const std::string &key, const CollectionArrayT &collection)
 
std::string toString (bool finalize=true)
 
bool addMember (const std::string &key, const char *value)
 
bool addMember (const std::string &key, const std::string &value)
 
bool addMember (const std::string &key, int64_t value)
 
bool addMember (const std::string &key, uint64_t value)
 
bool addMember (const std::string &key, int value)
 
bool addMember (const std::string &key, unsigned int value)
 
bool addMember (const std::string &key, bool value)
 
bool addMember (const std::string &key, double value)
 

Detailed Description

Utility class that can be used to build a json string.

E.g.: To build the following json: {"param1":"value","param2":{"param2.1":100}}. Use:

JsonGenerator generator; generator.addMember("param1", "value"); generator.startObject("param2"); generator.addMember("param2.1", 100); generator.toString();

For debugging purpose, you may be able to get the partial string by calling:

generator.toString(false)

Note
This class is NOT thread safe.

Constructor & Destructor Documentation

◆ JsonGenerator()

alexaClientSDK::avsCommon::utils::json::JsonGenerator::JsonGenerator ( )

Constructor.

◆ ~JsonGenerator()

alexaClientSDK::avsCommon::utils::json::JsonGenerator::~JsonGenerator ( )
default

Default destructor.

Member Function Documentation

◆ addCollectionOfStringArray()

template<typename CollectionArrayT , typename CollectionValueT , typename ValueT >
bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addCollectionOfStringArray ( const std::string &  key,
const CollectionArrayT &  collection 
)

Add a new array of arrays of strings with the given key name. The arrays is built from the given collection.

Template Parameters
CollectionArrayTType of the array of collection.
CollectionValueTType of the collection.
ValueTType of the collection member.
Parameters
keyThe name of the member.
collectionThe collection of string arrays. The strings are going to generate string elements.
Returns
true if it succeeded to add the new member and false otherwise.

◆ addMember() [1/8]

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addMember ( const std::string &  key,
const char *  value 
)

Add a new member with the key and value.

Parameters
keyThe name of the member.
valueThe value of the member.
Returns
true if it succeeded to add the new member and false otherwise.

◆ addMember() [2/8]

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addMember ( const std::string &  key,
const std::string &  value 
)

Add a new member with the key and value.

Parameters
keyThe name of the member.
valueThe value of the member.
Returns
true if it succeeded to add the new member and false otherwise.

◆ addMember() [3/8]

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addMember ( const std::string &  key,
int64_t  value 
)

Add a new member with the key and value.

Parameters
keyThe name of the member.
valueThe value of the member.
Returns
true if it succeeded to add the new member and false otherwise.

◆ addMember() [4/8]

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addMember ( const std::string &  key,
uint64_t  value 
)

Add a new member with the key and value.

Parameters
keyThe name of the member.
valueThe value of the member.
Returns
true if it succeeded to add the new member and false otherwise.

◆ addMember() [5/8]

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addMember ( const std::string &  key,
int  value 
)

Add a new member with the key and value.

Parameters
keyThe name of the member.
valueThe value of the member.
Returns
true if it succeeded to add the new member and false otherwise.

◆ addMember() [6/8]

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addMember ( const std::string &  key,
unsigned int  value 
)

Add a new member with the key and value.

Parameters
keyThe name of the member.
valueThe value of the member.
Returns
true if it succeeded to add the new member and false otherwise.

◆ addMember() [7/8]

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addMember ( const std::string &  key,
bool  value 
)

Add a new member with the key and value.

Parameters
keyThe name of the member.
valueThe value of the member.
Returns
true if it succeeded to add the new member and false otherwise.

◆ addMember() [8/8]

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addMember ( const std::string &  key,
double  value 
)

Add a new member with the key and value.

Parameters
keyThe name of the member.
valueThe value of the member.
Returns
true if it succeeded to add the new member and false otherwise.

◆ addMembersArray()

template<typename CollectionT , typename ValueT >
bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addMembersArray ( const std::string &  key,
const CollectionT &  collection 
)

Add a new array with the given key name. The array is built from the given collection.

Template Parameters
CollectionTType of the collection.
ValueTType of the collection member.
Parameters
keyThe name of the member.
collectionThe collection used to generate the array. Each item in the collection should be a string in the JSON format. Examples of valid items are: "\"val"", "123", and "true" representing a JSON string, a JSON integer, and a JSON boolean respectively.
Returns
true if it succeeded to add the new member and false otherwise.

◆ addRawJsonMember()

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addRawJsonMember ( const std::string &  key,
const std::string &  json,
bool  validate = true 
)

Adds a raw json as a value to the given key.

Parameters
keyThe object key to the raw json provided.
jsonA string representation of a valid json.
validateEnable json validation for the raw json parameter.
Returns
true if it succeeded to add the raw json and false otherwise.

◆ addStringArray()

template<typename CollectionT , typename ValueT >
bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::addStringArray ( const std::string &  key,
const CollectionT &  collection 
)

Add a new array of strings with the given key name. The array is built from the given collection.

Template Parameters
CollectionTType of the collection.
ValueTType of the collection member.
Parameters
keyThe name of the member.
collectionThe collection used to generate the array. The strings are going to generate string elements. For vector of json elements, use addMembersArray.
Returns
true if it succeeded to add the new member and false otherwise.

◆ finishArray()

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::finishArray ( )

Finish the last array that has been opened.

Returns
true if the last object was closed false if it fails.

◆ finishArrayElement()

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::finishArrayElement ( )

Finish the last array element that has been opened.

Returns
true if the last object was closed false if it fails.

◆ finishObject()

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::finishObject ( )

Close the last object that has been opened.

Returns
true if the last object was closed false if it fails.

◆ isFinalized()

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::isFinalized ( )

Checks whether the generator has been finalized (i.e., no changes can be made to the current json).

Returns
true if it has been finalized, false otherwise

◆ startArray()

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::startArray ( const std::string &  key)

Starts a new array with the given key.

Parameters
keyThe new array name.
Returns
true if it succeeds to create a new object and false if it fails.

◆ startArrayElement()

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::startArrayElement ( )

Starts a new array object element.

Returns
true if it succeeds to create a new object and false if it fails.

◆ startObject()

bool alexaClientSDK::avsCommon::utils::json::JsonGenerator::startObject ( const std::string &  key)

Starts a new json object with the given key.

Parameters
keyThe new object name.
Returns
true if it succeeds to create a new object and false if it fails.

◆ toString()

std::string alexaClientSDK::avsCommon::utils::json::JsonGenerator::toString ( bool  finalize = true)

Return the string representation of the object.

Parameters
finalizeIf set to true the object will be finalized and the string returned will be a complete json document. If false, the returned string will represent the current state of the json generation which could be partial.
Note
Once the object has been finalized, no changes can be made to the generator.
Returns
The string representation of the json object.

The documentation for this class was generated from the following file:

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