Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
畅游知识海洋
json
提交
aad55219
J
json
项目概览
畅游知识海洋
/
json
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
json
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
aad55219
编写于
7月 30, 2017
作者:
N
Niels Lohmann
提交者:
GitHub
7月 30, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'develop' into move-construction
上级
93bb7123
96dd4ffa
变更
10
展开全部
隐藏空白更改
内联
并排
Showing
10 changed file
with
970 addition
and
995 deletion
+970
-995
CMakeLists.txt
CMakeLists.txt
+1
-2
doc/images/scanner.png
doc/images/scanner.png
+0
-0
src/json.hpp
src/json.hpp
+705
-948
test/src/unit-cbor.cpp
test/src/unit-cbor.cpp
+72
-9
test/src/unit-class_const_iterator.cpp
test/src/unit-class_const_iterator.cpp
+7
-7
test/src/unit-class_iterator.cpp
test/src/unit-class_iterator.cpp
+7
-7
test/src/unit-class_parser.cpp
test/src/unit-class_parser.cpp
+22
-3
test/src/unit-convenience.cpp
test/src/unit-convenience.cpp
+10
-10
test/src/unit-deserialization.cpp
test/src/unit-deserialization.cpp
+74
-1
test/src/unit-msgpack.cpp
test/src/unit-msgpack.cpp
+72
-8
未找到文件。
CMakeLists.txt
浏览文件 @
aad55219
...
...
@@ -3,8 +3,6 @@ cmake_minimum_required(VERSION 3.0)
# define the project
project
(
nlohmann_json VERSION 2.1.1 LANGUAGES CXX
)
enable_testing
()
option
(
JSON_BuildTests
"Build the unit tests"
ON
)
# define project variables
...
...
@@ -26,6 +24,7 @@ target_include_directories(${JSON_TARGET_NAME} INTERFACE
# create and configure the unit test target
if
(
JSON_BuildTests
)
enable_testing
()
add_subdirectory
(
test
)
endif
()
...
...
doc/images/scanner.png
已删除
100644 → 0
浏览文件 @
93bb7123
644.7 KB
src/json.hpp
浏览文件 @
aad55219
此差异已折叠。
点击以展开。
test/src/unit-cbor.cpp
浏览文件 @
aad55219
...
...
@@ -1284,6 +1284,25 @@ TEST_CASE("single CBOR roundtrip")
// compare parsed JSON values
CHECK
(
j1
==
j2
);
SECTION
(
"roundtrips"
)
{
SECTION
(
"std::ostringstream"
)
{
std
::
ostringstream
ss
;
json
::
to_cbor
(
j1
,
ss
);
json
j3
=
json
::
from_cbor
(
ss
.
str
());
CHECK
(
j1
==
j3
);
}
SECTION
(
"std::string"
)
{
std
::
string
s
;
json
::
to_cbor
(
j1
,
s
);
json
j3
=
json
::
from_cbor
(
s
);
CHECK
(
j1
==
j3
);
}
}
// check with different start index
packed
.
insert
(
packed
.
begin
(),
5
,
0xff
);
CHECK
(
j1
==
json
::
from_cbor
(
packed
,
5
));
...
...
@@ -1519,16 +1538,60 @@ TEST_CASE("CBOR roundtrips", "[hide]")
std
::
ifstream
f_json
(
filename
);
json
j1
=
json
::
parse
(
f_json
);
// parse CBOR file
std
::
ifstream
f_cbor
(
filename
+
".cbor"
,
std
::
ios
::
binary
);
std
::
vector
<
uint8_t
>
packed
(
(
std
::
istreambuf_iterator
<
char
>
(
f_cbor
)),
std
::
istreambuf_iterator
<
char
>
());
json
j2
;
CHECK_NOTHROW
(
j2
=
json
::
from_cbor
(
packed
));
SECTION
(
"std::vector<uint8_t>"
)
{
// parse CBOR file
std
::
ifstream
f_cbor
(
filename
+
".cbor"
,
std
::
ios
::
binary
);
std
::
vector
<
uint8_t
>
packed
(
(
std
::
istreambuf_iterator
<
char
>
(
f_cbor
)),
std
::
istreambuf_iterator
<
char
>
());
json
j2
;
CHECK_NOTHROW
(
j2
=
json
::
from_cbor
(
packed
));
// compare parsed JSON values
CHECK
(
j1
==
j2
);
}
SECTION
(
"std::ifstream"
)
{
// parse CBOR file
std
::
ifstream
f_cbor
(
filename
+
".cbor"
,
std
::
ios
::
binary
);
json
j2
;
CHECK_NOTHROW
(
j2
=
json
::
from_cbor
(
f_cbor
));
// compare parsed JSON values
CHECK
(
j1
==
j2
);
}
SECTION
(
"uint8_t* and size"
)
{
// parse CBOR file
std
::
ifstream
f_cbor
(
filename
+
".cbor"
,
std
::
ios
::
binary
);
std
::
vector
<
uint8_t
>
packed
(
(
std
::
istreambuf_iterator
<
char
>
(
f_cbor
)),
std
::
istreambuf_iterator
<
char
>
());
json
j2
;
CHECK_NOTHROW
(
j2
=
json
::
from_cbor
({
packed
.
data
(),
packed
.
size
()}));
// compare parsed JSON values
CHECK
(
j1
==
j2
);
}
// compare parsed JSON values
CHECK
(
j1
==
j2
);
SECTION
(
"output to output adapters"
)
{
// parse CBOR file
std
::
ifstream
f_cbor
(
filename
+
".cbor"
,
std
::
ios
::
binary
);
std
::
vector
<
uint8_t
>
packed
(
(
std
::
istreambuf_iterator
<
char
>
(
f_cbor
)),
std
::
istreambuf_iterator
<
char
>
());
SECTION
(
"std::vector<uint8_t>"
)
{
std
::
vector
<
uint8_t
>
vec
;
json
::
to_cbor
(
j1
,
vec
);
CHECK
(
vec
==
packed
);
}
}
}
}
}
...
...
test/src/unit-class_const_iterator.cpp
浏览文件 @
aad55219
...
...
@@ -182,32 +182,32 @@ TEST_CASE("const_iterator class")
{
json
j
(
json
::
value_t
::
null
);
json
::
const_iterator
it
=
j
.
cbegin
();
CHECK_THROWS_AS
(
it
->
type_name
(
),
json
::
invalid_iterator
&
);
CHECK_THROWS_WITH
(
it
->
type_name
(
),
"[json.exception.invalid_iterator.214] cannot get value"
);
CHECK_THROWS_AS
(
std
::
string
(
it
->
type_name
()
),
json
::
invalid_iterator
&
);
CHECK_THROWS_WITH
(
std
::
string
(
it
->
type_name
()
),
"[json.exception.invalid_iterator.214] cannot get value"
);
}
SECTION
(
"number"
)
{
json
j
(
17
);
json
::
const_iterator
it
=
j
.
cbegin
();
CHECK
(
it
->
type_name
(
)
==
"number"
);
CHECK
(
std
::
string
(
it
->
type_name
()
)
==
"number"
);
it
=
j
.
cend
();
CHECK_THROWS_AS
(
it
->
type_name
(
),
json
::
invalid_iterator
&
);
CHECK_THROWS_WITH
(
it
->
type_name
(
),
"[json.exception.invalid_iterator.214] cannot get value"
);
CHECK_THROWS_AS
(
std
::
string
(
it
->
type_name
()
),
json
::
invalid_iterator
&
);
CHECK_THROWS_WITH
(
std
::
string
(
it
->
type_name
()
),
"[json.exception.invalid_iterator.214] cannot get value"
);
}
SECTION
(
"object"
)
{
json
j
({{
"foo"
,
"bar"
}});
json
::
const_iterator
it
=
j
.
cbegin
();
CHECK
(
it
->
type_name
(
)
==
"string"
);
CHECK
(
std
::
string
(
it
->
type_name
()
)
==
"string"
);
}
SECTION
(
"array"
)
{
json
j
({
1
,
2
,
3
,
4
});
json
::
const_iterator
it
=
j
.
cbegin
();
CHECK
(
it
->
type_name
(
)
==
"number"
);
CHECK
(
std
::
string
(
it
->
type_name
()
)
==
"number"
);
}
}
}
...
...
test/src/unit-class_iterator.cpp
浏览文件 @
aad55219
...
...
@@ -166,32 +166,32 @@ TEST_CASE("iterator class")
{
json
j
(
json
::
value_t
::
null
);
json
::
iterator
it
=
j
.
begin
();
CHECK_THROWS_AS
(
it
->
type_name
(
),
json
::
invalid_iterator
&
);
CHECK_THROWS_WITH
(
it
->
type_name
(
),
"[json.exception.invalid_iterator.214] cannot get value"
);
CHECK_THROWS_AS
(
std
::
string
(
it
->
type_name
()
),
json
::
invalid_iterator
&
);
CHECK_THROWS_WITH
(
std
::
string
(
it
->
type_name
()
),
"[json.exception.invalid_iterator.214] cannot get value"
);
}
SECTION
(
"number"
)
{
json
j
(
17
);
json
::
iterator
it
=
j
.
begin
();
CHECK
(
it
->
type_name
(
)
==
"number"
);
CHECK
(
std
::
string
(
it
->
type_name
()
)
==
"number"
);
it
=
j
.
end
();
CHECK_THROWS_AS
(
it
->
type_name
(
),
json
::
invalid_iterator
&
);
CHECK_THROWS_WITH
(
it
->
type_name
(
),
"[json.exception.invalid_iterator.214] cannot get value"
);
CHECK_THROWS_AS
(
std
::
string
(
it
->
type_name
()
),
json
::
invalid_iterator
&
);
CHECK_THROWS_WITH
(
std
::
string
(
it
->
type_name
()
),
"[json.exception.invalid_iterator.214] cannot get value"
);
}
SECTION
(
"object"
)
{
json
j
({{
"foo"
,
"bar"
}});
json
::
iterator
it
=
j
.
begin
();
CHECK
(
it
->
type_name
(
)
==
"string"
);
CHECK
(
std
::
string
(
it
->
type_name
()
)
==
"string"
);
}
SECTION
(
"array"
)
{
json
j
({
1
,
2
,
3
,
4
});
json
::
iterator
it
=
j
.
begin
();
CHECK
(
it
->
type_name
(
)
==
"number"
);
CHECK
(
std
::
string
(
it
->
type_name
()
)
==
"number"
);
}
}
}
...
...
test/src/unit-class_parser.cpp
浏览文件 @
aad55219
...
...
@@ -41,12 +41,31 @@ json parser_helper(const std::string& s)
{
json
j
;
json
::
parser
(
nlohmann
::
detail
::
input_adapter
(
s
)).
parse
(
true
,
j
);
// if this line was reached, no exception ocurred
// -> check if result is the same without exceptions
json
j_nothrow
;
CHECK_NOTHROW
(
json
::
parser
(
nlohmann
::
detail
::
input_adapter
(
s
),
nullptr
,
false
).
parse
(
true
,
j_nothrow
));
CHECK
(
j_nothrow
==
j
);
return
j
;
}
bool
accept_helper
(
const
std
::
string
&
s
)
{
return
json
::
parser
(
nlohmann
::
detail
::
input_adapter
(
s
)).
accept
(
true
);
// 1. parse s without exceptions
json
j
;
CHECK_NOTHROW
(
json
::
parser
(
nlohmann
::
detail
::
input_adapter
(
s
),
nullptr
,
false
).
parse
(
true
,
j
));
const
bool
ok_noexcept
=
not
j
.
is_discarded
();
// 2. accept s
const
bool
ok_accept
=
json
::
parser
(
nlohmann
::
detail
::
input_adapter
(
s
)).
accept
(
true
);
// 3. check if both approaches come to the same result
CHECK
(
ok_noexcept
==
ok_accept
);
// 4. return result
return
ok_accept
;
}
TEST_CASE
(
"parser class"
)
...
...
@@ -590,8 +609,8 @@ TEST_CASE("parser class")
SECTION
(
"overflow"
)
{
// overflows during parsing
yield an exception, but is accepted anyway
CHECK
(
accept_helper
(
"1.18973e+4932"
));
// overflows during parsing
CHECK
(
not
accept_helper
(
"1.18973e+4932"
));
}
SECTION
(
"invalid numbers"
)
...
...
test/src/unit-convenience.cpp
浏览文件 @
aad55219
...
...
@@ -36,7 +36,7 @@ void check_escaped(const char* original, const char* escaped, const bool ensure_
void
check_escaped
(
const
char
*
original
,
const
char
*
escaped
,
const
bool
ensure_ascii
)
{
std
::
stringstream
ss
;
json
::
serializer
s
(
nlohmann
::
detail
::
output_adapter
_factory
<
char
>::
create
(
ss
),
' '
);
json
::
serializer
s
(
nlohmann
::
detail
::
output_adapter
<
char
>
(
ss
),
' '
);
s
.
dump_escaped
(
original
,
ensure_ascii
);
CHECK
(
ss
.
str
()
==
escaped
);
}
...
...
@@ -45,15 +45,15 @@ TEST_CASE("convenience functions")
{
SECTION
(
"type name as string"
)
{
CHECK
(
json
(
json
::
value_t
::
null
).
type_name
(
)
==
"null"
);
CHECK
(
json
(
json
::
value_t
::
object
).
type_name
(
)
==
"object"
);
CHECK
(
json
(
json
::
value_t
::
array
).
type_name
(
)
==
"array"
);
CHECK
(
json
(
json
::
value_t
::
number_integer
).
type_name
(
)
==
"number"
);
CHECK
(
json
(
json
::
value_t
::
number_unsigned
).
type_name
(
)
==
"number"
);
CHECK
(
json
(
json
::
value_t
::
number_float
).
type_name
(
)
==
"number"
);
CHECK
(
json
(
json
::
value_t
::
boolean
).
type_name
(
)
==
"boolean"
);
CHECK
(
json
(
json
::
value_t
::
string
).
type_name
(
)
==
"string"
);
CHECK
(
json
(
json
::
value_t
::
discarded
).
type_name
(
)
==
"discarded"
);
CHECK
(
std
::
string
(
json
(
json
::
value_t
::
null
).
type_name
()
)
==
"null"
);
CHECK
(
std
::
string
(
json
(
json
::
value_t
::
object
).
type_name
()
)
==
"object"
);
CHECK
(
std
::
string
(
json
(
json
::
value_t
::
array
).
type_name
()
)
==
"array"
);
CHECK
(
std
::
string
(
json
(
json
::
value_t
::
number_integer
).
type_name
()
)
==
"number"
);
CHECK
(
std
::
string
(
json
(
json
::
value_t
::
number_unsigned
).
type_name
()
)
==
"number"
);
CHECK
(
std
::
string
(
json
(
json
::
value_t
::
number_float
).
type_name
()
)
==
"number"
);
CHECK
(
std
::
string
(
json
(
json
::
value_t
::
boolean
).
type_name
()
)
==
"boolean"
);
CHECK
(
std
::
string
(
json
(
json
::
value_t
::
string
).
type_name
()
)
==
"string"
);
CHECK
(
std
::
string
(
json
(
json
::
value_t
::
discarded
).
type_name
()
)
==
"discarded"
);
}
SECTION
(
"string escape"
)
...
...
test/src/unit-deserialization.cpp
浏览文件 @
aad55219
...
...
@@ -91,14 +91,19 @@ TEST_CASE("deserialization")
{
SECTION
(
"stream"
)
{
std
::
stringstream
ss1
,
ss2
,
ss3
;
std
::
stringstream
ss1
,
ss2
,
ss3
,
ss4
;
ss1
<<
"[
\"
foo
\"
,1,2,3,false,{
\"
one
\"
:1}"
;
ss2
<<
"[
\"
foo
\"
,1,2,3,false,{
\"
one
\"
:1}"
;
ss3
<<
"[
\"
foo
\"
,1,2,3,false,{
\"
one
\"
:1}"
;
ss4
<<
"[
\"
foo
\"
,1,2,3,false,{
\"
one
\"
:1}"
;
CHECK_THROWS_AS
(
json
::
parse
(
ss1
),
json
::
parse_error
&
);
CHECK_THROWS_WITH
(
json
::
parse
(
ss2
),
"[json.exception.parse_error.101] parse error at 29: syntax error - unexpected end of input; expected ']'"
);
CHECK
(
not
json
::
accept
(
ss3
));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
ss1
,
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"string"
)
...
...
@@ -108,6 +113,10 @@ TEST_CASE("deserialization")
CHECK_THROWS_WITH
(
json
::
parse
(
s
),
"[json.exception.parse_error.101] parse error at 29: syntax error - unexpected end of input; expected ']'"
);
CHECK
(
not
json
::
accept
(
s
));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
s
,
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"operator<<"
)
...
...
@@ -260,6 +269,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
'a'
,
'a'
,
'a'
,
'a'
,
'a'
,
'a'
,
'\\'
,
'u'
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 2"
)
...
...
@@ -267,6 +280,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
'a'
,
'a'
,
'a'
,
'a'
,
'a'
,
'a'
,
'\\'
,
'u'
,
'1'
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 3"
)
...
...
@@ -274,6 +291,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
'a'
,
'a'
,
'a'
,
'a'
,
'a'
,
'a'
,
'\\'
,
'u'
,
'1'
,
'1'
,
'1'
,
'1'
,
'1'
,
'1'
,
'1'
,
'1'
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 4"
)
...
...
@@ -281,6 +302,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
'a'
,
'a'
,
'a'
,
'a'
,
'a'
,
'a'
,
'u'
,
'1'
,
'1'
,
'1'
,
'1'
,
'1'
,
'1'
,
'1'
,
'1'
,
'\\'
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 5"
)
...
...
@@ -288,6 +313,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
0x7F
,
0xC1
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 6"
)
...
...
@@ -295,6 +324,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
0x7F
,
0xDF
,
0x7F
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 7"
)
...
...
@@ -302,6 +335,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
0x7F
,
0xDF
,
0xC0
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 8"
)
...
...
@@ -309,6 +346,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
0x7F
,
0xE0
,
0x9F
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 9"
)
...
...
@@ -316,6 +357,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
0x7F
,
0xEF
,
0xC0
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 10"
)
...
...
@@ -323,6 +368,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
0x7F
,
0xED
,
0x7F
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 11"
)
...
...
@@ -330,6 +379,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
0x7F
,
0xF0
,
0x8F
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 12"
)
...
...
@@ -337,6 +390,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
0x7F
,
0xF0
,
0xC0
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 13"
)
...
...
@@ -344,6 +401,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
0x7F
,
0xF3
,
0x7F
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 14"
)
...
...
@@ -351,6 +412,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
0x7F
,
0xF3
,
0xC0
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 15"
)
...
...
@@ -358,6 +423,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'\"'
,
0x7F
,
0xF4
,
0x7F
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
SECTION
(
"case 16"
)
...
...
@@ -365,6 +434,10 @@ TEST_CASE("deserialization")
uint8_t
v
[]
=
{
'{'
,
'\"'
,
'\"'
,
':'
,
'1'
,
'1'
};
CHECK_THROWS_AS
(
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
)),
json
::
parse_error
&
);
CHECK
(
not
json
::
accept
(
std
::
begin
(
v
),
std
::
end
(
v
)));
json
j_error
;
CHECK_NOTHROW
(
j_error
=
json
::
parse
(
std
::
begin
(
v
),
std
::
end
(
v
),
nullptr
,
false
));
CHECK
(
j_error
.
is_discarded
());
}
}
}
...
...
test/src/unit-msgpack.cpp
浏览文件 @
aad55219
...
...
@@ -1126,6 +1126,25 @@ TEST_CASE("single MessagePack roundtrip")
// compare parsed JSON values
CHECK
(
j1
==
j2
);
SECTION
(
"roundtrips"
)
{
SECTION
(
"std::ostringstream"
)
{
std
::
ostringstream
ss
;
json
::
to_msgpack
(
j1
,
ss
);
json
j3
=
json
::
from_msgpack
(
ss
.
str
());
CHECK
(
j1
==
j3
);
}
SECTION
(
"std::string"
)
{
std
::
string
s
;
json
::
to_msgpack
(
j1
,
s
);
json
j3
=
json
::
from_msgpack
(
s
);
CHECK
(
j1
==
j3
);
}
}
// check with different start index
packed
.
insert
(
packed
.
begin
(),
5
,
0xff
);
CHECK
(
j1
==
json
::
from_msgpack
(
packed
,
5
));
...
...
@@ -1292,15 +1311,60 @@ TEST_CASE("MessagePack roundtrips", "[hide]")
std
::
ifstream
f_json
(
filename
);
json
j1
=
json
::
parse
(
f_json
);
// parse MessagePack file
std
::
ifstream
f_msgpack
(
filename
+
".msgpack"
,
std
::
ios
::
binary
);
std
::
vector
<
uint8_t
>
packed
((
std
::
istreambuf_iterator
<
char
>
(
f_msgpack
)),
std
::
istreambuf_iterator
<
char
>
());
json
j2
;
CHECK_NOTHROW
(
j2
=
json
::
from_msgpack
(
packed
));
SECTION
(
"std::vector<uint8_t>"
)
{
// parse MessagePack file
std
::
ifstream
f_msgpack
(
filename
+
".msgpack"
,
std
::
ios
::
binary
);
std
::
vector
<
uint8_t
>
packed
(
(
std
::
istreambuf_iterator
<
char
>
(
f_msgpack
)),
std
::
istreambuf_iterator
<
char
>
());
json
j2
;
CHECK_NOTHROW
(
j2
=
json
::
from_msgpack
(
packed
));
// compare parsed JSON values
CHECK
(
j1
==
j2
);
}
SECTION
(
"std::ifstream"
)
{
// parse MessagePack file
std
::
ifstream
f_msgpack
(
filename
+
".msgpack"
,
std
::
ios
::
binary
);
json
j2
;
CHECK_NOTHROW
(
j2
=
json
::
from_msgpack
(
f_msgpack
));
// compare parsed JSON values
CHECK
(
j1
==
j2
);
}
SECTION
(
"uint8_t* and size"
)
{
// parse MessagePack file
std
::
ifstream
f_msgpack
(
filename
+
".msgpack"
,
std
::
ios
::
binary
);
std
::
vector
<
uint8_t
>
packed
(
(
std
::
istreambuf_iterator
<
char
>
(
f_msgpack
)),
std
::
istreambuf_iterator
<
char
>
());
json
j2
;
CHECK_NOTHROW
(
j2
=
json
::
from_msgpack
({
packed
.
data
(),
packed
.
size
()}));
// compare parsed JSON values
CHECK
(
j1
==
j2
);
}
// compare parsed JSON values
CHECK
(
j1
==
j2
);
SECTION
(
"output to output adapters"
)
{
// parse MessagePack file
std
::
ifstream
f_msgpack
(
filename
+
".msgpack"
,
std
::
ios
::
binary
);
std
::
vector
<
uint8_t
>
packed
(
(
std
::
istreambuf_iterator
<
char
>
(
f_msgpack
)),
std
::
istreambuf_iterator
<
char
>
());
SECTION
(
"std::vector<uint8_t>"
)
{
std
::
vector
<
uint8_t
>
vec
;
json
::
to_msgpack
(
j1
,
vec
);
CHECK
(
vec
==
packed
);
}
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录