Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site













SourceForge.net Logo

JsonIO and Jsonize framework

 

class JsonIO

JsonIO represents a single JSON value in Jsonize framework. It is used as parameter for Jsonize global function. Jsonize global function has default templated definition that in turn calls Jsonize method of object. However, when implementation using method is not possible (e.g. for supported primitive types), global templated specialization of Jsonize can be used.

 

 

Public Method List

 

bool IsLoading() const

Returns true when retrieving data from JSON.

 


 

bool IsStoring() const

Returns true when storing data to JSON.

 


 

const Value& Get() const

Returns the value of JSON node when loading.

 


 

void Set(const Value& v)

Sets the value of JSON node when storing.

 


 

Value Get(const char *key)

Returns the value of JSON subnode key when loading.

 


 

void Set(const char *key, const Value& v)

Sets the value of JSON subnode key when storing.

 


 

void Put(Value& v)

Retrieves JSON node data into v when storing - invoked to finish jsonization.

 


 

Value GetResult() const

Returns JSON node data into v when storing - invoked to finish jsonization.

 


 

template <class TJsonIO& operator()(const char *key, T& value)

Sets the JsonIO to represent JSON object (if it does not already), creates member key and jsonizes value into it (by calling global Jsonize function, whose general form invokes Jsonize method).

 


 

template <class TJsonIO& operator()(const char *key, T& value, const T& defvalue)

Sets the JsonIO to represent JSON object (if it does not already), creates member key and jsonizes value into it (by calling global Jsonize function, whose general form invokes Jsonize method). When loading and key is missing in source data, defvalue is assigned to value.

 


 

template <class TJsonIO& List(const char *key, const char *, T& var)

Same as operator()(key, var). Provides compatibility with Xmlize for common template routines that provide both Jsonize and Xmlize code.

 


 

template <class T, class XJsonIO& Var(const char *key, T& value, X item_jsonize)

Similar to operator()(key, value), but instead of using global Jsonize function, it uses item_jsonize to define the structure.

 


 

template <class T, class XJsonIO& Array(const char *key, T& value, X item_jsonize, const char * = NULL)

Jsonizes array container (must have GetCount, Add and operator[] methods), using item_jsonize to define the structure of elements. The last parameter is ignored and is included to provide compatibility with Xmlize code for common template routines that provide both Jsonize and Xmlize code.

 


 

JsonIO(const Value& src)

Sets JsonIO for retrieving data from src which is must be Json compatible Value.

 


 

JsonIO()

Creates JsonIO for storing data.

 

 

Jsonize Store/Load functions

 

template <class TValue StoreAsJsonValue(const T& var)

Converts var into JSON compatible Value (such that can be directly used with AsJson function).

 


 

template <class Tvoid LoadFromJsonValue(T& var, const Value& x)

Retrieves variable from JSON compatible Value. Can throw JsonizeError on error.

 


 

template <class TString StoreAsJson(const T& var, bool pretty = false)

Converts var into JSON text. If pretty is true, adds whitespaces to look better, otherwise the result is as compact as possible.

 


 

template <class Tbool LoadFromJson(T& var, const char *json)

Retrieves var from json text. Does not throw JsonizeError, returns false in case of error.

 


 

template <class Tbool StoreAsJsonFile(const T& var, const char *file = NULL, bool pretty = false)

Converts var into JSON file. If pretty is true, adds whitespaces to look better, otherwise the result is as compact as possible. If file is NULL, ConfigFile(GetExeTitle() + ".json") is used as the file path.

 


 

template <class Tbool LoadFromJsonFile(T& var, const char *file = NULL)

Retrieves var from json file. Does not throw JsonizeError, returns false in case of error. If file is NULL, ConfigFile(GetExeTitle() + ".json") is used as the file path.

 

 

Standard Jsonize template specializations

 

template <> void Jsonize(JsonIO& io, int& var)

template <> void Jsonize(JsonIO& io, byte& var)

template <> void Jsonize(JsonIO& io, int16& var)

template <> void Jsonize(JsonIO& io, int64& var)

template <> void Jsonize(JsonIO& io, double& var)

template <> void Jsonize(JsonIO& io, bool& var)

template <> void Jsonize(JsonIO& io, String& var)

template <> void Jsonize(JsonIO& io, WString& var)

template <> void Jsonize(JsonIO& io, Date& var)

template <> void Jsonize(JsonIO& io, Time& var)

Provides Jsonize template specialization to support var data type.

 

 

Special Jsonize variants

 

template <class K, class Vvoid StringMap(JsonIO& io, VectorMap<K, V, H>& map)

template <class K, class Vvoid StringMap(JsonIO& io, ArrayMap<K, V, H>& map)

The default Jsonize for Maps has to encode any type as key, which leads to somewhat unnatural results when key K is String or WString. StringMap alternative variant encodes map as JSON object with keys equal to map keys.

 

 


 

template <class Tvoid JsonizeBySerialize(JsonIO& jio, T& x)

This function encodes/retrieves Json by binary serializing the object and Jsonizing the hexadecimal data string.

 

Last edit by cxl on 01/20/2020. Do you want to contribute?. T++