Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
畅游知识海洋
json
提交
6e4910d5
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,发现更多精彩内容 >>
未验证
提交
6e4910d5
编写于
6月 17, 2017
作者:
T
Théo DELRIEU
提交者:
Théo DELRIEU
6月 19, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add pair/tuple conversions
上级
c9836483
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
121 addition
and
0 deletion
+121
-0
src/json.hpp
src/json.hpp
+69
-0
test/src/unit-constructor1.cpp
test/src/unit-constructor1.cpp
+52
-0
未找到文件。
src/json.hpp
浏览文件 @
6e4910d5
...
...
@@ -465,6 +465,39 @@ using enable_if_t = typename std::enable_if<B, T>::type;
template
<
typename
T
>
using
uncvref_t
=
typename
std
::
remove_cv
<
typename
std
::
remove_reference
<
T
>::
type
>::
type
;
// implementation of C++14 index_sequence and affiliates
// source: https://stackoverflow.com/a/32223343
template
<
std
::
size_t
...
Ints
>
struct
index_sequence
{
using
type
=
index_sequence
;
using
value_type
=
std
::
size_t
;
static
constexpr
std
::
size_t
size
()
noexcept
{
return
sizeof
...(
Ints
);
}
};
template
<
class
Sequence1
,
class
Sequence2
>
struct
merge_and_renumber
;
template
<
std
::
size_t
...
I1
,
std
::
size_t
...
I2
>
struct
merge_and_renumber
<
index_sequence
<
I1
...
>
,
index_sequence
<
I2
...
>>
:
index_sequence
<
I1
...,
(
sizeof
...(
I1
)
+
I2
)...
>
{
};
template
<
std
::
size_t
N
>
struct
make_index_sequence
:
merge_and_renumber
<
typename
make_index_sequence
<
N
/
2
>::
type
,
typename
make_index_sequence
<
N
-
N
/
2
>::
type
>
{
};
template
<
>
struct
make_index_sequence
<
0
>
:
index_sequence
<>
{
};
template
<
>
struct
make_index_sequence
<
1
>
:
index_sequence
<
0
>
{
};
template
<
typename
...
Ts
>
using
index_sequence_for
=
make_index_sequence
<
sizeof
...(
Ts
)
>
;
/*
Implementation of two C++17 constructs: conjunction, negation. This is needed
to avoid evaluating all the traits in a condition
...
...
@@ -866,6 +899,24 @@ void to_json(BasicJsonType& j, T (&arr)[N])
external_constructor
<
value_t
::
array
>::
construct
(
j
,
arr
);
}
template
<
typename
BasicJsonType
,
typename
...
Args
>
void
to_json
(
BasicJsonType
&
j
,
const
std
::
pair
<
Args
...
>&
p
)
{
j
=
{
p
.
first
,
p
.
second
};
}
template
<
typename
BasicJsonType
,
typename
Tuple
,
std
::
size_t
...
Idx
>
void
to_json_tuple_impl
(
BasicJsonType
&
j
,
const
Tuple
&
t
,
index_sequence
<
Idx
...
>
)
{
j
=
{
std
::
get
<
Idx
>
(
t
)...};
}
template
<
typename
BasicJsonType
,
typename
...
Args
>
void
to_json
(
BasicJsonType
&
j
,
const
std
::
tuple
<
Args
...
>&
t
)
{
to_json_tuple_impl
(
j
,
t
,
index_sequence_for
<
Args
...
>
{});
}
///////////////
// from_json //
///////////////
...
...
@@ -1094,6 +1145,24 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
}
}
template
<
typename
BasicJsonType
,
typename
...
Args
>
void
from_json
(
const
BasicJsonType
&
j
,
std
::
pair
<
Args
...
>&
p
)
{
p
=
{
j
.
at
(
0
),
j
.
at
(
1
)};
}
template
<
typename
BasicJsonType
,
typename
Tuple
,
std
::
size_t
...
Idx
>
void
from_json_tuple_impl
(
const
BasicJsonType
&
j
,
Tuple
&
t
,
index_sequence
<
Idx
...
>
)
{
t
=
std
::
make_tuple
(
j
.
at
(
Idx
)...);
}
template
<
typename
BasicJsonType
,
typename
...
Args
>
void
from_json
(
const
BasicJsonType
&
j
,
std
::
tuple
<
Args
...
>&
t
)
{
from_json_tuple_impl
(
j
,
t
,
index_sequence_for
<
Args
...
>
{});
}
struct
to_json_fn
{
private:
...
...
test/src/unit-constructor1.cpp
浏览文件 @
6e4910d5
...
...
@@ -239,6 +239,58 @@ TEST_CASE("constructors")
CHECK
(
j
==
j_reference
);
}
SECTION
(
"std::pair"
)
{
std
::
pair
<
float
,
std
::
string
>
p
{
1.0
,
"string"
};
json
j
(
p
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
REQUIRE
(
j
.
size
()
==
2
);
CHECK
(
j
[
0
]
==
std
::
get
<
0
>
(
p
));
CHECK
(
j
[
1
]
==
std
::
get
<
1
>
(
p
));
}
SECTION
(
"std::pair with discarded values"
)
{
json
j
{
1
,
2.0
,
"string"
};
const
auto
p
=
j
.
get
<
std
::
pair
<
int
,
float
>>
();
CHECK
(
p
.
first
==
j
[
0
]);
CHECK
(
p
.
second
==
j
[
1
]);
}
SECTION
(
"std::tuple"
)
{
const
auto
t
=
std
::
make_tuple
(
1.0
,
"string"
,
42
,
std
::
vector
<
int
>
{
0
,
1
});
json
j
(
t
);
CHECK
(
j
.
type
()
==
json
::
value_t
::
array
);
REQUIRE
(
j
.
size
()
==
4
);
CHECK
(
j
[
0
]
==
std
::
get
<
0
>
(
t
));
CHECK
(
j
[
1
]
==
std
::
get
<
1
>
(
t
));
CHECK
(
j
[
2
]
==
std
::
get
<
2
>
(
t
));
CHECK
(
j
[
3
][
0
]
==
0
);
CHECK
(
j
[
3
][
1
]
==
1
);
}
SECTION
(
"std::tuple with discarded values"
)
{
json
j
{
1
,
2.0
,
"string"
,
42
};
const
auto
t
=
j
.
get
<
std
::
tuple
<
int
,
float
,
std
::
string
>>
();
CHECK
(
std
::
get
<
0
>
(
t
)
==
j
[
0
]);
CHECK
(
std
::
get
<
1
>
(
t
)
==
j
[
1
]);
CHECK
(
std
::
get
<
2
>
(
t
)
==
j
[
2
]);
}
SECTION
(
"std::pair/tuple failures"
)
{
json
j
{
1
};
CHECK_THROWS
((
j
.
get
<
std
::
pair
<
int
,
int
>>
()));
CHECK_THROWS
((
j
.
get
<
std
::
tuple
<
int
,
int
>>
()));
}
SECTION
(
"std::forward_list<json>"
)
{
std
::
forward_list
<
json
>
a
{
json
(
1
),
json
(
1u
),
json
(
2.2
),
json
(
false
),
json
(
"string"
),
json
()};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录