当前位置: 首页 > news >正文

wordpress 模版 cho s宁波seo网络推广定制多少钱

wordpress 模版 cho s,宁波seo网络推广定制多少钱,网络工作室图,app公司开发Linux网络:序列化与反序列化 序列化与反序列化jsonjsoncppValue对象序列化反序列化WriterReader 序列化与反序列化 在网络通信中,最重要的就是通过接口,将数据通过网络发送给另一台主机。那么另一台主机收到数据后,就可以对数据进…

Linux网络:序列化与反序列化

    • 序列化与反序列化
    • json
    • jsoncpp
      • Value对象
      • 序列化反序列化
        • Writer
        • Reader


序列化与反序列化

在网络通信中,最重要的就是通过接口,将数据通过网络发送给另一台主机。那么另一台主机收到数据后,就可以对数据进行处理了。

但是网络通信中,数据不是简单的将所有的数据堆砌在一起,然后一起发送出去。相反的,数据应该以特定的形式组织起来,才能让接收方进行合理的解析。

比如以下数据:

你好2024.10.21cool boy

这是一个叫做cool boy的用户,在2024.10.21发送了一条"你好"的消息。对于人来说,这是很好理解的,但是对于计算机来说,这就很难处理了。比如说它也可以被解析为用户名是"你好"的用户发送了一条叫做cool boy的消息。用户名、时间、消息内容,这三个字段的分界符在哪里?如何区分这三个区域?

对于UDP来说,一个消息放在一个报文中一起发送。但是对于TCP来说,它是面向字节流的,那么就可以能出现多个消息的粘包问题:

你好2024.10.21cool boy我是一个程序员2024.10.21cool boy

比如收到这样一条TCP字节流消息,计算机又要如何区分两个报文的边界?

可以发现,简单的把数据堆在一起,这个数据就会难以解析。因此要把数据按照一定的规律合理组织在一起,并且约定好各个字段的含义,如果是TCP通信,还需要约定好一条消息的定界符,避免粘包问题。

按照一定的规则来组织数据,这种行为称为序列化。而从组织好的数据中还原出目标信息,称为反序列化

当前流行的序列化协议:

  • JSON
  • XML
  • 二进制格式

其中json全称JavaScript Object Notation,即JavaScript 对象 表示法,顾名思义它是由JavaScript提出的一种序列化规范,这是一种文本形式的协议,以字符串的形式存储与解析数据。

比如刚才的消息可以表示为:

{"message": "你好","time": "2024.10.21","name": "cool boy"
}

这样整个消息就被分为了三个部分,并且每个字段都以键值对的形式表示。这样就可以很明确的表示"你好"是一条消息,而cool boy是用户名。而整条报文的最外层,有一个{},这就可以起到TCP的定界符的作用。

除去json还有很多数据序列化格式,比如二进制格式可以使用protobuf等。接下来博客讲解json的序列化格式,以及它的C++接口。


json

json可以保存标量类型,数组,对象三种形式的数据,规则如下:

  1. 数据以键值对key: value的形式存储
  2. 多条数据使用,隔开
  3. key的类型必须是字符串
  4. value的类型可以是标量类型,数组,对象
  5. 数组用[]表示
  6. 对象用{}表示

对于整个json消息,它可以是数组或对象。

数组形式:

["hello","world",true,3.14
]

对象形式:

{"name": "cool boy","age": 12,"adult": false
}

首先,数组内部的数据,不需要以键值对的形式存储,而数组最外侧使用[]表示。在数组中,可以存储任何形式的数据,比如true是布尔类型,3.14是浮点型。

而在对象形式中,每个字段都要以key:value的形式存储,最外侧使用{},对象中也可以存储任意标量类型。

另外的,数组与对象之间,还可以进行嵌套:

[{"name": "张三","age": 12,"adult": false},{"name": "李四","age": 20,"adult": true}
]

比如这是一个数组,内部有两个对象,这就是数组内嵌套对象。

{"name": "张三","age": 12,"friend": ["李四","王五","赵六"]
}

这是一个对象,在对象中,friend存储的是一个数组,数组内又存储了多个字符串。

想要处理json数据,还是比较麻烦的,比如说要考虑括号匹配问题,数据类型的转化问题。但是不用担心,目前的主流语言,都要专门支持json格式的库,其中C++可以使用jsoncpp库完成数据的序列化与反序列化。


jsoncpp

Linux中,下载jsoncpp非常简单,以Ubuntu为例:

sudo apt install libjsoncpp-dev

执行完指令,就可以使用该库了,只需包含头文件:

#include <jsoncpp/json/json.h>

Value对象

jsoncpp的所有操作,都基于Json::Value类,该类用于保存一个json格式的对象。

基于C++的操作符重载特性,Json::Value类实现了operator[],完成对元素的增删改,函数声明如下:

Value& operator[](const char* key);
Value& operator[](const String& key);

这两个接口,是对json对象的操作,[]内放key值,而key必须是一个字符串,此处支持char*std::string两者格式的字符串。

std::map用法一致,如果[]内的值存在,那么就返回该值,如果不存在,就添加该值:

Json::Value root;
root["name"] = "张三";
root["age"] = 12;std::cout << root["age"] << std::endl;

以上代码中,创建了一个json对象root,并设置了“name”"age"属性,最后又获取并输出"gae"

如果Json::Value存储的是一个json数组,那么使用以下两个接口:

Value& operator[](ArrayIndex index);
Value& operator[](int index);

这两个接口根据下标index返回元素的引用,下标从0开始。

如果存储了json数组,那么就不能通过operator[key] = value的形式插入值了,因为数组不需要key值,此时使用以下接口:

void append(const Json::Value& value);

只需要append(value)即可在数组尾插一个元素。

Json::Value root;
root.append("张三");
root.append("李四");
root.append("王五");
root.append("赵六");std::cout << root[2] << std::endl;

以上代码,往数组中插入了四个值,并输出下标为2的元素。

  • 类型转化

如果仔细观察,你会发现上面所有接口,操作的都是Json::Value,比如append(const Json::Value& value)

其实Json::Value可以转化为绝大部分的标量类型:

Json::Value& operator=(bool value);
Json::Value& operator=(int value);
Json::Value& operator=(unsigned int value);
Json::Value& operator=(Int64 value);
Json::Value& operator=(UInt64 value);
Json::Value& operator=(double value);
Json::Value& operator=(const char* value);
Json::Value& operator=(const std::string value);

也就是说之前的所有接口,其实Json::Value这个参数可以表达所有的标量类型,因此不论是数组的appent还是对象的operator[],都可以插入任意标量类型的数据,这些标量数据最后会转化为Json::Value

  • 数组和对象操作

再补充一些数组和对象的通用接口:

size_t size();
bool empty();
void clear();
  1. size:返回数组或对象中元素数量
  2. empty:检查数组或对象是否为空
  3. clear:清空数组或对象的所有元素

序列化反序列化

了解如何往Json::Value中填写数据后,接下来就要考虑如何进行序列化与反序列化了。

Writer

jsoncpp通过Json::Writer类进行序列化操作,但是该类是一个抽象类,无法实例化出对象。它有两个派生类StyledWriterFastWriter,这两个类的对象完成具体的序列化操作。

Json::Writer类中,有一个writer函数,声明如下:

virtual String write(const Value& root) = 0;

这是最核心的序列化函数,= 0表示这个函数由子类重写,也就是StyledWriterFastWriter对这个函数进行了重写。

函数输入一个Json::Value对象,随后会把这个对象进行序列化,将序列化好的数据作为返回值。

构造并序列化以下json

["hello","world",12,false,["abc","def"],{"gender": "boy","age": 12}
]

这是一个数组,内部包含了六个元素,其中第五个元素是另一个数组,第六个元素是一个对象。

构造对象:

Json::Value root;
root.append("hello");
root.append("world");
root.append(12);
root.append(false);Json::Value arr;
arr.append("abc");
arr.append("def");
root.append(arr);Json::Value obj;
obj["gender"] = "boy";
obj["age"] = 12;
root.append(obj);

对于前四个标量数据,直接通过append添加到root结尾即可。而对于嵌套的数组和对象,要创建另一个Json::Value,先进行数据填充,在添加到root结尾。

序列化:

Json::StyledWriter swriter;
std::string style = swriter.write(root);
std::cout << "StyledWriter:" << std::endl;
std::cout << style << std::endl;Json::FastWriter fwriter;
std::string fast = fwriter.write(root);
std::cout << "FastWriter:" << std::endl;
std::cout << fast << std::endl;

此处的序列化分别实验了StyledWriterFastWriter,它们都可以完成序列化。通过write(root)接口完成序列化后,将序列化的结果输出,结果:

StyledWriter:
["hello","world",12,false,[ "abc", "def" ],{"age" : 12,"gender" : "boy"}
]FastWriter:
["hello","world",12,false,["abc","def"],{"age":12,"gender":"boy"}]

可以看出,其实两种序列化形式的数据内容是一样的,区别在于格式:

  1. StyledWriter:会给json数据添加换行格式,让数据更加可视化
  2. FastWriter:去除所有的换行格式,整个json就是一行数据

在实际中,选用哪一种都可以,两者区别其实不大。

现在将以上对象序列化到test.json文件中:

Json::StyledWriter swriter;
std::string str = swriter.write(root);
std::ofstream ofs("test.json");
ofs << str;
ofs.close();

使用std::string接收到write返回值后,直接通过文件流对象输出到文件即可。


Reader

当需要反序列化数据时,就要用到Json::Reader类,核心反序列化函数如下:

bool parse(const std::string& document, Value& root,bool collectComments = true);
bool parse(IStream& is, Value& root, bool collectComments = true);

Writer不同,Reader可以直接使用,无需使用它的派生类。不论是通过StyledWriter还是FastWriter序列化的数据,Reader都可以直接反序列化。

第一个函数接收一个std::string和一个Json::Value root对象的引用,parse直接反序列化字符串内的数据给root对象,第三个参数不用管,默认为true即可。

第二个函数则是通过文件流IStream& is读取数据,然后反序列化到root中。

当拿到一个未知的Json::Value,又要如何解析?这就要用到isas系列的接口:

bool isNull() const; // 检查值是否为为空
bool isBool() const; // 检查是否为布尔类型
bool isInt() const;  // 检查是否为整型
bool isInt64() const; // 检查是否为64位整型
bool isUInt() const; // 检查是否为无符号整型
bool isUInt64() const; // 检查是否为64位无符号整型
bool isIntegral() const; // 检查是否为整数或者可转化为整数的浮点数
bool isDouble() const; // 检查是否为浮点数
bool isNumeric() const; // 检查是否为数字,整数或浮点数
bool isString() const; // 检查是否为字符串
bool isArray() const; // 检查是否为数组
bool isObject() const; // 检查是否为对象

由于拿到Json::Value后,无法确定内部的值是对象,数组,还是一些标量。因此提供了以上is系列接口,来检测数据类型,防止出现接收数据时,类型不匹配导致的错误。

const char* asCString() const;
String asString() const;
Int asInt() const;
UInt asUInt() const;
Int64 asInt64() const;
UInt64 asUInt64() const;
float asFloat() const;
double asDouble() const;
bool asBool() const;

当确定了类型后,就可以通过以上的as系列接口,直接把Json::Value转化为对应的标量类型,进行后续操作。

而对于数组可能不知道它有多少个元素,可以通过size接口获取下标范围。而对于对象,可能不知道他有哪些key,此时可以通过以下接口:

Members getMemberNames() const;

如果Json::Value内部存储的是对象,该函数用于获取一个Json::Value的所有key,并存储在Json::Value::Members中,Json::Value::Members是一个数组,可以直接通过下标访问。

对象常用以下格式解析:

Json::Value::Members keys = root.getMemberNames();
for (int j = 0; j < keys.size(); j++)
{Json::Value item = root[keys[j]]// 解析item
}

确定root是一个对象后,先root.getMemberNames()拿到所有的key。随后通过一个循环遍历所有的keyroot[keys[j]]就是对象root的一个成员,随后在对该对象做解析操作即可。

由于不论是标量类型,还是数组或者对象,都是使用Json::Value表示的,所以常用递归的形式来解析:

void print(Json::Value root)
{if (root.isInt())std::cout << root.asInt() << std::endl;else if (root.isBool())std::cout << root.asBool() << std::endl;else if (root.isString())std::cout << root.asString() << std::endl;else if (root.isArray()){std::cout << "[" << std::endl;for (int i = 0; i < root.size(); i++)print(root[i]);std::cout << "]" << std::endl;}else if (root.isObject()){std::cout << "{" << std::endl;Json::Value::Members keys = root.getMemberNames();for (int j = 0; j < keys.size(); j++){std::cout << keys[j] << ": ";print(root[keys[j]]);}std::cout << "}" << std::endl;}
}

当该函数接收到一个Json::Value root后,先判断他是不是基本的标量类型,如果是标量类型,那么直接输出即可。

如果是数组,那么for (int i = 0; i < root.size(); i++)遍历它的所有下标,每个元素root[i]有可能是标量,嵌套数组,嵌套对象。但是没关系,这些类型都被统一为了Json::Value,直接递归print(root[i])

对象同理,先通过root.getMemberNames()拿到所有的key,所有root[keys[j]]就是对象内的元素,这个元素同样有可能是标量,嵌套数组,嵌套对象,它们被统一为了Json::Value,直接递归print(root[keys[j]])

以递归形式读取刚才的test.json

void print(Json::Value root)
{if (root.isInt())std::cout << root.asInt() << std::endl;else if (root.isBool())std::cout << root.asBool() << std::endl;else if (root.isString())std::cout << root.asString() << std::endl;else if (root.isArray()){std::cout << "[" << std::endl;for (int i = 0; i < root.size(); i++)print(root[i]);std::cout << "]" << std::endl;}else if (root.isObject()){std::cout << "{" << std::endl;Json::Value::Members keys = root.getMemberNames();for (int j = 0; j < keys.size(); j++){std::cout << keys[j] << ": ";print(root[keys[j]]);}std::cout << "}" << std::endl;}
}int main()
{std::ifstream ifs("test.json");Json::Reader re;Json::Value root;re.parse(ifs, root);print(root);return 0;
}

main函数中,打开文件流ifs("test.json"),随后re.parse(ifs, root)直接从文件流中读取数据,解析到root对象中,最后通过print函数递归输出该数据。

输出结果:

[
hello
world
12
0
[
abc
def
]
{
age: 12
gender: boy
}
]

这个结果没有控制缩进,所有格式有点丑。



文章转载自:
http://superpersonal.tsnq.cn
http://littleness.tsnq.cn
http://girt.tsnq.cn
http://wrack.tsnq.cn
http://krone.tsnq.cn
http://mhr.tsnq.cn
http://cesarian.tsnq.cn
http://semper.tsnq.cn
http://areometer.tsnq.cn
http://exsection.tsnq.cn
http://sporeling.tsnq.cn
http://prothoracic.tsnq.cn
http://geomorphic.tsnq.cn
http://quoit.tsnq.cn
http://immunoelectrophoresis.tsnq.cn
http://valuable.tsnq.cn
http://fanlike.tsnq.cn
http://bluppy.tsnq.cn
http://ejecta.tsnq.cn
http://prismoid.tsnq.cn
http://jackassery.tsnq.cn
http://mitigate.tsnq.cn
http://coordinates.tsnq.cn
http://eyepatch.tsnq.cn
http://dumbbell.tsnq.cn
http://stirring.tsnq.cn
http://freeby.tsnq.cn
http://sciolto.tsnq.cn
http://bedeman.tsnq.cn
http://asynchronism.tsnq.cn
http://piddle.tsnq.cn
http://litigable.tsnq.cn
http://investigator.tsnq.cn
http://monotocous.tsnq.cn
http://sirup.tsnq.cn
http://continence.tsnq.cn
http://ulama.tsnq.cn
http://cliffhang.tsnq.cn
http://elusive.tsnq.cn
http://coleopteron.tsnq.cn
http://thermoform.tsnq.cn
http://suberate.tsnq.cn
http://darky.tsnq.cn
http://tuberculosis.tsnq.cn
http://homeworker.tsnq.cn
http://unborn.tsnq.cn
http://ferula.tsnq.cn
http://monocase.tsnq.cn
http://patience.tsnq.cn
http://automatically.tsnq.cn
http://fora.tsnq.cn
http://cursed.tsnq.cn
http://ralline.tsnq.cn
http://deneb.tsnq.cn
http://sardinia.tsnq.cn
http://reembark.tsnq.cn
http://whortle.tsnq.cn
http://edwardine.tsnq.cn
http://affranchise.tsnq.cn
http://cumshaw.tsnq.cn
http://gone.tsnq.cn
http://markan.tsnq.cn
http://subotica.tsnq.cn
http://forwardly.tsnq.cn
http://legislatrix.tsnq.cn
http://allot.tsnq.cn
http://boodler.tsnq.cn
http://britches.tsnq.cn
http://killifish.tsnq.cn
http://shakedown.tsnq.cn
http://mondaine.tsnq.cn
http://noncaloric.tsnq.cn
http://fusuma.tsnq.cn
http://centerboard.tsnq.cn
http://dogrobber.tsnq.cn
http://nabbie.tsnq.cn
http://unabroken.tsnq.cn
http://lymphocytotic.tsnq.cn
http://somedeal.tsnq.cn
http://pachycepbalosaur.tsnq.cn
http://spiraculum.tsnq.cn
http://ruhmkorff.tsnq.cn
http://batrachotoxin.tsnq.cn
http://shypoo.tsnq.cn
http://laingian.tsnq.cn
http://khidmutgar.tsnq.cn
http://correspond.tsnq.cn
http://biocoenose.tsnq.cn
http://newgate.tsnq.cn
http://godspeed.tsnq.cn
http://tweeter.tsnq.cn
http://carafe.tsnq.cn
http://wildling.tsnq.cn
http://daunorubicin.tsnq.cn
http://residual.tsnq.cn
http://propagandism.tsnq.cn
http://recessionary.tsnq.cn
http://cryosurgery.tsnq.cn
http://teleshopping.tsnq.cn
http://debride.tsnq.cn
http://www.dt0577.cn/news/114442.html

相关文章:

  • php网站后台建设长春视频剪辑培训机构
  • 句容网站制作哪家好重庆百度seo代理
  • 网站建设主要课程软文广告文案
  • 苏州网站建设公司电话网络推广有前途吗
  • seo兼职工资一般多少网络优化推广公司哪家好
  • 微信网站开发制作公司seo外链购买
  • 中国室内设计师seo海外推广
  • 网站建设模板公司营销网站案例
  • c2c模式的诞生与发展seo快速优化文章排名
  • 做电影网站要买什么刘连康seo培训哪家强
  • 简单网页制作模板下载自学seo大概需要多久
  • 现在用什么cms做网站好今日军事新闻热点事件
  • 网站建设优化的作用aso优化推广公司
  • 做代理的网站北京朝阳区疫情最新情况
  • 网页游戏排行榜魔域长沙优化科技
  • 网页游戏开服表好吗抖音视频排名优化
  • 门户网站开发平台学校seo推广培训班
  • 四省网站建设百度点击工具
  • 传统网站建设架构最新国际新闻事件
  • 推广做网站怎么样百度搜索排名靠前
  • 网站开发小程序开发如何推广网站链接
  • 网站建设与管理淘宝网站优化seo推广服务
  • 唐山哪里建设的好关键词优化一年的收费标准
  • 龙口网站制作多少钱最新资讯热点
  • 商丘电子商务网站建设百度人工在线客服
  • 大学生网站开发南宁百度首页优化
  • 成都网站网页设计推广神器
  • 保定市做网站公司地址电话如何提高自己在百度的排名
  • 做品牌特价的网站宁波网站制作设计
  • 网络策划就业前景seo推广是什么意思呢