要序列化json变量,请使用zetjsoncpp::反序列化完成。ZetJsonCpp支持以下类型的反序列化,
要反序列化json布尔值,可以通过JsonVarBoolean完成,如下所示,
zetjsoncpp::deserialize>("true");
booleans的json向量可以是:,
[true,false,true]
要反序列化booleans的json向量,可以通过JsonVarVectorBoolean完成,如下所示,
zetjsoncpp::deserialize>(
"["
"true"
",false"
",true"
"]");
booleans的json映射可能如下所示,
{
"id1":true
,"id2":false
,"id3":true
}
要反序列化booleans的json映射,可以通过JsonVarMapBoolean完成,如下所示,
zetjsoncpp::deserialize>(
"{"
"\"id1\":true"
",\"id2\":false"
",\"id3\":true"
"}");
一个json数字,可以是,
zetjsoncpp::deserialize>("1");
要反序列化一个json编号,可以通过JsonVarNumber完成,如下所示,
一个由数字组成的json向量,可以是:,
[1,3.7e+2,-3]
要反序列化数字的json向量,可以通过JsonVarVectorNumber完成,如下所示,
zetjsoncpp::deserialize>(
"["
"1"
",3.7e+2"
",-3"
"]");
一个json数字映射可以是这样的,
{
"id1":1
,"id2":3.7e+2
,"id3":-3
}
要反序列化数字的json映射,可以通过JsonVarMapNumber完成,如下所示,
zetjsoncpp::deserialize>(
"{"
"\"id1\":1"
",\"id2\":3.7e+2"
",\"id3\":-3"
"}");
要反序列化json字符串,可以通过JsonVarString完成,如下所示,
zetjsoncpp::deserialize>("\"my_string\"")
字符串的json向量可以是,
["string_1","string_2","string_3"]
要反序列化字符串向量,可以通过JsonVarVectorString完成,如下所示,
zetjsoncpp::deserialize>(
"["
"\"string_1\""
",\"string_2\""
",\"string_3\""
"]");
一个字符串的json映射可以是,
{
"id1":"string_1"
,"id2":"string_2"
,"id3":"string_3"
}
要反序列化字符串映射,可以通过JsonVarMapString完成,如下所示,
JsonVarMapString<> *m3=zetjsoncpp::deserialize>(
"{"
"\"id1\":\"string_1\""
",\"id2\":\"string_2\""
",\"id3\":\"string_3\""
"}");
到目前为止,它已经发现了一种简单易懂的序列化基本类型和结构化类型的方法。现在我们介绍反序列化json对象的方法,它需要一些设置。json对象就像一个json映射,其值上有不同的内容。
json对象可以是
{
"encoding":"UTF-8"
,"length":1000
,"use_space":false
}
List2.1
以List2.1为例,在zetjsoncpp中,它使用C中的结构定义json对象,如下所示,
typedef struct{
zetjsoncpp::JsonVarString encoding;
zetjsoncpp::JsonVarNumber length;
zetjsoncpp::JsonVarBoolean use_space;
}JsonSample;
List2.2注:
你可能注意到ZJïu CONSTïu CHAR(s)。这是一个通过可变模板逐字符传递文本字符串的技巧,因为模板不接受传递文本字符串(即constchar*)作为参数。
最后,要反序列化json对象,可以通过JsonVarObject传递要反序列化的结构类型来完成,如List2.2所示,
auto json_object=zetjsoncpp::deserialize>(
"{"
"\"encoding\":\"UTF-8\""
",\"length\":1000"
",\"use_space\":false"
"}");
如果任何变量没有被反序列化,因为它不存在于字符串/文件中,或者它与JSON属性名称JSON不匹配C++结构中定义的JSON,它必须使用ISDRIALIZED()来检查变量是否被反序列化。
例如:
if(json_object->encoding.isDeserialized()){
// value deserialized ok. do something...
}
List3.4默认情况下,任何未反序列化的变量字符串都设置为空,数字和布尔值将分别设置为0和false。
对象的json向量可以是,
[{
"encoding":"UTF-8"
,"length":1000
,"use_space":false
},{
"encoding":"ANSII"
,"length":1500
,"use_space":true
}]
要反序列化对象向量,可以通过JSONVARVERCTORObject来完成,传递结构类型以反序列化清单2.2中所示的结构类型,
zetjsoncpp::deserialize>(
"[{"
"\"encoding\":\"UTF-8\""
",\"length\":1000"
",\"use_space\":false"
"},{"
"\"encoding\":\"ANSII\""
",\"length\":1500"
",\"use_space\":true"
"}]");
一个对象的json映射它可以是,
{
"id1":{
"encoding":"UTF-8"
,"length":1000
,"use_space":false
}
,"id2":{
"encoding":"ANSII"
,"length":1500
,"use_space":true
}
}
要反序列化对象映射,可以通过JsonVarMapObject来完成,传递结构类型以反序列化清单2.2中所示的结构类型,
zetjsoncpp::deserialize>(
"{"
"\"id1\":{"
"\"encoding\":\"UTF-8\""
",\"length\":1000"
",\"use_space\":false"
"}"
",\"id2\":{"
"\"encoding\":\"ANSII\""
",\"length\":1500"
",\"use_space\":true"
"}"
"}");
要序列化json变量,请使用zetjsoncpp::serialize完成。
例如,
// parse json var number
auto json_number=zetjsoncpp::deserialize>("2");
// change it by 3.5
json_number=3.5;
std::cout << zetjsoncpp::serialize(json_var); << std::enl;
// it outputs 3.5
假设它有一个名为sample.json的文件,包含以下内容,
// Configuration options
{
// Default encoding for text
"encoding" : "UTF-8",
"number": 3.34E-5,
// Plug-ins loaded at start-up
"plug-ins" : [
"python",
"c++",
"ruby"
],
// Tab indent size
"indent" : { "length" : 3, "use_space": true },
// set of languages
"languages":[{
"code" : "en",
"general_texts": {
"general.hello_word":"Hello world!"
,"general.yes":"Yes"
,"general.no":"No"
}
},{
"code" : "es",
"general_texts": {
"general.hello_word":"Hola mundo!"
,"general.yes":"Si"
,"general.no":"No"
}
},{
"code" : "zh-CN",
"general_texts": {
"general.hello_word":"???"
,"general.yes":"?"
,"general.no":"??"
}
}]
// set of interpolators
,"interpolations":{
"id_1":{
"type":"material"
,"channels":"rgb"
,"data":[
// r g b t
//---- --- --- ----
0.0,1.0,0.0,1000
,0.0,0.0,0.0,0
]
},"id_2":{
"type":"transform"
,"channels":"xyz"
,"data":[
// x y z t
//---- --- --- ----
0.0,1.0,0.0,1000
,0.0,0.0,0.0,0
]
}
}
}
List3.1
要加载list1.1中的代码,我们必须声明以下C结构,
#include "zetjsoncpp.h"
using zetjsoncpp::JsonVarNumber;
using zetjsoncpp::JsonVarBoolean;
using zetjsoncpp::JsonVarString;
using zetjsoncpp::JsonVarMapString;
using zetjsoncpp::JsonVarVectorNumber;
using zetjsoncpp::JsonVarVectorString;
using zetjsoncpp::JsonVarObject;
using zetjsoncpp::JsonVarVectorObject;
using zetjsoncpp::JsonVarMapObject;
typedef struct{
// Number length
JsonVarNumber length;
// Boolean use_space
JsonVarBooleanuse_space;
}Ident;
typedef struct{
// String code
JsonVarString code;
// Map of strings general_texts
JsonVarMapString general_texts;
}Language;
typedef struct{
// String type
JsonVarString type;
// String channels
JsonVarString channels;
// Vector of numbers data
JsonVarVectorNumber data;
}Interpolation;
typedef struct
{
// String encoding
JsonVarString encoding;
// Number number
JsonVarNumber number;
// Vector of strings plug-ins
JsonVarVectorString plugins;
// Object indent
JsonVarObject indent;
// Object languages
JsonVarVectorObject languages;
// Map of objects interpolations
JsonVarMapObject interpolations;
}SampleJson;
List3.2
然后我们必须编写以下代码,将list1.1中所示的数据加载到list1.2中的Esstructure中,
try{
auto json_object=zetjsoncpp::deserialize_file>("sample.json");
// the values before modifications.
std::cout << "---------------------------------------------------" << std::endl;
std::cout << " Before modifications:"<< std::endl;
std::cout << zetjsoncpp::serialize(json_object);
// From here we can operate with loaded data in our program using c++ operators
// put m_use_space to false...
json_object->indent.use_space = false;
// iterate of all plugins and replace with random strings...
for(unsigned i = 0; i < json_object->plugins.size(); i++) {
json_object->plugins[i] = "my_randomstring"+zetjsoncpp::zj_strutils::int_to_str(i+1);
}
// iterate of all interpolations and replace its data values...
for(auto it_map = json_object->interpolations.begin(); it_map != json_object->interpolations.end(); it_map++) {
for(auto it = it_map->second->data.begin(); it != it_map->second->data.end(); it++) {
*it = rand();
}
}
std::cout << "--------------------------------------------------" << std::endl;
std::cout << " After modifications:"<< std::endl;
std::cout << zetjsoncpp::serialize(json_object);
// destroy json_object
delete json_object;
}catch(std::exception & ex){
std::cerr << "Error:" << ex.what() << std::endl;
}
作为例子,我们提出了一种将加载的JSON数据操作到C++代码的方法,
int main(int argc, char *argv[]){
try{
auto json_object=zetjsoncpp::deserialize_file>("sample.json");
// the values before modifications.
std::cout << "---------------------------------------------------" << std::endl;
std::cout << " Before modifications:"<< std::endl;
std::cout << zetjsoncpp::serialize(json_object);
// From here we can operate with loaded data in our program using c++ operators
// put m_use_space to false...
json_object->indent.use_space = false;
// iterate of all plugins and replace with random strings...
for(unsigned i = 0; i < json_object->plugins.size(); i++) {
json_object->plugins[i] = "my_randomstring"+zetjsoncpp::zj_strutils::int_to_str(i+1);
}
// iterate of all interpolations and replace its data values...
for(auto it_map = json_object->interpolations.begin(); it_map != json_object->interpolations.end(); it_map++) {
for(auto it = it_map->second->data.begin(); it != it_map->second->data.end(); it++) {
*it = rand();
}
}
std::cout << "--------------------------------------------------" << std::endl;
std::cout << " After modifications:"<< std::endl;
std::cout << zetjsoncpp::serialize(json_object);
// destroy json_object
delete json_object;
}catch(std::exception & ex){
std::cerr << "Error:" << ex.what() << std::endl;
}
}
执行之后,输出显示了更改前后的序列化json,
---------------------------------------------------
Before modifications:
{
"encoding":"UTF-8",
"number":0.000033,
"plug-ins":
[
"python","c++","ruby"
],
"indent":
{
"length":3.000000,
"use_space":true
},
"languages":
[{
"code":"en",
"general_texts":
{
"general.hello_word":"Hello world!"
,"general.no":"No"
,"general.yes":"Yes"
}
},{
"code":"es",
"general_texts":
{
"general.hello_word":"Hola mundo!"
,"general.no":"No"
,"general.yes":"Si"
}
},{
"code":"zh-CN",
"general_texts":
{
"general.hello_word":"你好词"
,"general.no":"没有"
,"general.yes":"是"
}
}],
"interpolations":
{
"id_1":{
"type":"material",
"channels":"rgb",
"data":
[
0.000000,1.000000,0.000000,1000.000000,0.000000,0.000000,0.000000,0.000000
]
}
,"id_2":{
"type":"transform",
"channels":"xyz",
"data":
[
0.000000,1.000000,0.000000,1000.000000,0.000000,0.000000,0.000000,0.000000
]
}
}
}--------------------------------------------------
After modifications:
{
"encoding":"UTF-8",
"number":0.000033,
"plug-ins":
[
"my_randomstring1","my_randomstring2","my_randomstring3"
],
"indent":
{
"length":3.000000,
"use_space":false
},
"languages":
[{
"code":"en",
"general_texts":
{
"general.hello_word":"Hello world!"
,"general.no":"No"
,"general.yes":"Yes"
}
},{
"code":"es",
"general_texts":
{
"general.hello_word":"Hola mundo!"
,"general.no":"No"
,"general.yes":"Si"
}
},{
"code":"zh-CN",
"general_texts":
{
"general.hello_word":"你好词"
,"general.no":"没有"
,"general.yes":"是"
}
}],
"interpolations":
{
"id_1":{
"type":"material",
"channels":"rgb",
"data":
[
41.000000,18467.000000,6334.000000,26500.000000,19169.000000,15724.000000,11478.000000,29358.000000
]
}
,"id_2":{
"type":"transform",
"channels":"xyz",
"data":
[
26962.000000,24464.000000,5705.000000,28145.000000,23281.000000,16827.000000,9961.000000,491.000000
]
}
}
}