BOOST的JSON解析库Boost.JSON简介
⽂章⽬录
Boost很早就有了能解析JSON的库(Boost.PropertyTree)还能解析XML,INI和INFO等。但其笨重复杂。在1.75后引⼊了新的JSON 解析库Boost.JSON。
JSON库基础
JSON中主要包括四种数据类型:object、value、array和string;要使⽤JSON功能,需要:
#include <boost/json.hpp>
using namespace boost::json;
从JSON字符串中获取键值(通过if_contains获取key对应value的指针,然后通过as_XXX获取实际值):
#include <boost/json.hpp>
auto obj = boost::json::parse(recvContent).as_object();
auto opType = obj.at("TYPE").as_string();
auto pValue = obj.if_contains("OP_STR");
if (pValue)
{
if (pValue->as_string().empty())
{
// ...
}
else
{
auto pStr = std::make_shared<std::string>(pValue->as_string().c_str());
/json值的类型有哪些
/ ...
}
}
object
object是JSON键值对的容器,定义在<boost/json/object.hpp>,其主要的成员函数:
Name Description
at(key)获取指定Key对应的元素的引⽤(不存在时会抛出out_of_range异常)
begin/end获取iterator
capacity容量
cbegin/cend获取const iterator
clear Erase all elements.
contains(key)判断Key是否存在
count(key)返回Key的数量
Construct an element in-place.
empty是否为空
erase(it/key)根据key或iterator移除元素
find(key)返回指定key的iterator或end()。
find(key)返回指定key的iterator或end()。
Name Description
if_contains(key)返回key对应value的指针,或null(不存在时)。
插⼊元素
插⼊或赋值(若key已存在)
operator=Copy assignment.Move assignment.Assignment.
operator[]存在返回对应引⽤,若不存在则插⼊null value,并返回
reserve增加容量(若指定值⼩于现有容量,则什么也不做)
size⼤⼩
swap Swap two objects.
max_size静态成员,返回object能保存元素的最⼤数量。
value
表⽰JSON值的类型,主要的成员函数:
Name Description
as_array若为数组,则返回对应的引⽤(array),否则抛出异常
as_bool
as_double
as_int64
类型匹配时,则返回对应的引⽤,否则抛出异常
as_object
as_string
as_uint64
at(key)
数组类型根据索引(从0开始),获取值引⽤;其他根据值返回引⽤;不存在时抛出异常at(pos)
emplace_array
emplace_bool
emplace_double
emplace_int64
返回对应的引⽤,并修改value为对应类型(且赋为对应的默认值)
emplace_object
emplace_string
emplace_uint64
Change the kind to null, discarding the previous contents.
get_array
get_bool
get_double
获取对应类型的引⽤(不做任何检查,速度快)
get_int64
get_object
get_string
get_uint64
if_array
if_bool
if_double
类型匹配,返回对应的指针;否则返回nullptr
if_int64
if_object
if_string
if_uint64
is_array
is_bool
is_double
类型匹配,返回true
is_int64
is_object
is_object
Name Description
is_string
is_uint64
Returns true if this is a null.
Returns true if this is not an array or object.
Returns true if this is an array or object.
返回值对应的底层类型
Swap the given values.
Return the stored number cast to an arithmetic type.
array
JSON值为数组的类型,主要成员函数:
Name Description
at(pos)获取指定索引(从0开始)处值的引⽤,出错抛出out_of_range异常back获取最后⼀个元素
begin/end
获取iterator
rbegin/rend
capacity获取容量
clear清空
data获取底层数组的指针
emplace(pos, Arg&&)在指定位置插⼊元素(构造)
emplace_back在尾部插⼊元素
empty Check if the array has no elements.
earse删除元素
front返回第⼀个元素
Return a pointer to an element, or nullptr if the index is invalid.
Insert elements before the specified location.
Copy assignment.Move assignment.Assignment.
operator[]Access an element.
pop_back删除最后⼀个元素
push_back在尾部添加元素
reserve增加容量(若指定值⼩于现有容量,则什么也不做)
resize修改元素数量(若⽐原来⼤则填充null value,否则删除多余元素)shrink_to_fit Request the removal of unused capacity.
size Return the number of elements in the array.
string
字符串值类型,主要成员函数:
Name Description
append追加字符(串)
assign赋值
at返回指定位置字符引⽤
back返回最后⼀个字符引⽤
begin/end返回iterator
c_str Return the underlying character array directly.
clear Clear the contents.
compare Compare a string with the string.
Copy a substring to another string.
Return the underlying character array directly.
Check if the string has no characters.
Return whether the string end with a string.Return whether the string ends with a character.
Erase characters from the string.Erase a character from the string.Erase a range from the string.
Find the first occurrence of a string within the string.Find the first occurrence of a character within the string.
Find the first occurrence of any of the characters not within the string.Find the first occurrence of a character not
equal to ch.
Find the first occurrence of any of the characters within the string.
Find the last occurrence of a character not within the string.Find the last occurrence of a character not equal to
ch.
Find the last occurrence of any of the characters within the string.
Return the first character.
Increase size without changing capacity.
插⼊
operator
Convert to a string_view referring to the string.
string_view
operator+=Append characters from a string.Append a character.
operator=Copy assignment.Move assignment.Assign a value to the string.
operator[]Return a character without bounds checking.
Remove the last character.
Append a character.
替换
Increase the capacity to at least a certain amount.
Change the size of the string.
Find the last occurrence of a string within the string.Find the last occurrence of a character within the string.
Request the removal of unused capacity.
Return the number of characters in the string.
Return whether the string begins with a string.Return whether the string begins with a character.
Name Description
Return a substring.
JSON使⽤
构造的JSON⽰例格式如下:
{
"a_string":"test_string",
"a_number":123,
"a_null":null,
"a_array":[
1,
"2",
{
"123":"123"
}
],
"a_object":{
"a_name":"a_data"
},
"a_bool":true
}
构造JSON
构造⼀个JSON很简单:定义⼀个object,然后设定各个value即可:boost::json::object val;
val["a_string"] = "test_string";
val["a_number"] = 123;
val["a_null"] = nullptr;
val["a_array"] = {
1, "2", boost::json::object({{"123", "123"}})
};
val["a_object"].emplace_object()["a_name"] = "a_data";
val["a_bool"] = true;
Boost.JSON⽀持使⽤std::initializer_list来构造,所以也可以这样使⽤:boost::json::value val = {
{"a_string", "test_string"},
{"a_number", 123},
{"a_null", nullptr},
{"a_array", {1, "2", {{"123", "123"}}}},
{"a_object", {{"a_name", "a_data"}}},
{"a_bool", true}
};
使⽤initializer_list构造时,有时很难区分是数组还是对象,这是可以明确指定:// 构造[["data", "value"]]
boost::json::value jsonAry = {boost::json::array({"data", "value"})};
// 构造{"data": "value"}
boost::json::value jsonObj = boost::json::object({{"data", "value"}});
序列化
JSON对象可以使⽤serialize序列化:
std::cout << boost::json::serialize(val) << std::endl;
serializer还⽀持部分流输出(在数据量较⼤时,可以有效降低内存占⽤):
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论