Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
畅游知识海洋
json
提交
91aa4c9d
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,发现更多精彩内容 >>
提交
91aa4c9d
编写于
5月 11, 2016
作者:
N
Niels
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'develop' into feature/issue239
上级
6d8e00ad
9ecf83f6
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
210 addition
and
2 deletion
+210
-2
doc/examples/push_back__initializer_list.cpp
doc/examples/push_back__initializer_list.cpp
+26
-0
doc/examples/push_back__initializer_list.link
doc/examples/push_back__initializer_list.link
+1
-0
doc/examples/push_back__initializer_list.output
doc/examples/push_back__initializer_list.output
+4
-0
src/json.hpp
src/json.hpp
+49
-1
src/json.hpp.re2c
src/json.hpp.re2c
+49
-1
test/unit.cpp
test/unit.cpp
+81
-0
未找到文件。
doc/examples/push_back__initializer_list.cpp
0 → 100644
浏览文件 @
91aa4c9d
#include <json.hpp>
using
json
=
nlohmann
::
json
;
int
main
()
{
// create JSON values
json
object
=
{{
"one"
,
1
},
{
"two"
,
2
}};
json
null
;
// print values
std
::
cout
<<
object
<<
'\n'
;
std
::
cout
<<
null
<<
'\n'
;
// add values:
object
.
push_back
({
"three"
,
3
});
// object is extended
object
+=
{
"four"
,
4
};
// object is extended
null
.
push_back
({
"five"
,
5
});
// null is converted to array
// print values
std
::
cout
<<
object
<<
'\n'
;
std
::
cout
<<
null
<<
'\n'
;
// would throw:
//object.push_back({1, 2, 3});
}
doc/examples/push_back__initializer_list.link
0 → 100644
浏览文件 @
91aa4c9d
<a target="_blank" href="http://melpon.org/wandbox/permlink/wZF4dRHjfCyjb3rx"><b>online</b></a>
\ No newline at end of file
doc/examples/push_back__initializer_list.output
0 → 100644
浏览文件 @
91aa4c9d
{"one":1,"two":2}
null
{"four":4,"one":1,"three":3,"two":2}
[["five",5]]
src/json.hpp
浏览文件 @
91aa4c9d
...
...
@@ -4878,7 +4878,55 @@ class basic_json
reference
operator
+=
(
const
typename
object_t
::
value_type
&
val
)
{
push_back
(
val
);
return
operator
[](
val
.
first
);
return
*
this
;
}
/*!
@brief add an object to an object
This function allows to use `push_back` with an initializer list. In case
1. the current value is an object,
2. the initializer list @a init contains only two elements, and
3. the first element of @a init is a string,
@a init is converted into an object element and added using
@ref push_back(const typename object_t::value_type&). Otherwise, @a init
is converted to a JSON value and added using @ref push_back(basic_json&&).
@param init an initializer list
@complexity Linear in the size of the initializer list @a init.
@note This function is required to resolve an ambiguous overload error,
because pairs like `{"key", "value"}` can be both interpreted as
`object_t::value_type` or `std::initializer_list<basic_json>`, see
https://github.com/nlohmann/json/issues/235 for more information.
@liveexample{The example shows how initializer lists are treated as
objects when possible.,push_back__initializer_list}
*/
void
push_back
(
std
::
initializer_list
<
basic_json
>
init
)
{
if
(
is_object
()
and
init
.
size
()
==
2
and
init
.
begin
()
->
is_string
())
{
const
string_t
key
=
*
init
.
begin
();
push_back
(
typename
object_t
::
value_type
(
key
,
*
(
init
.
begin
()
+
1
)));
}
else
{
push_back
(
basic_json
(
init
));
}
}
/*!
@brief add an object to an object
@copydoc push_back(std::initializer_list<basic_json>)
*/
reference
operator
+=
(
std
::
initializer_list
<
basic_json
>
init
)
{
push_back
(
init
);
return
*
this
;
}
/*!
...
...
src/json.hpp.re2c
浏览文件 @
91aa4c9d
...
...
@@ -4878,7 +4878,55 @@ class basic_json
reference operator+=(const typename object_t::value_type& val)
{
push_back(val);
return operator[](val.first);
return *this;
}
/*!
@brief add an object to an object
This function allows to use `push_back` with an initializer list. In case
1. the current value is an object,
2. the initializer list @a init contains only two elements, and
3. the first element of @a init is a string,
@a init is converted into an object element and added using
@ref push_back(const typename object_t::value_type&). Otherwise, @a init
is converted to a JSON value and added using @ref push_back(basic_json&&).
@param init an initializer list
@complexity Linear in the size of the initializer list @a init.
@note This function is required to resolve an ambiguous overload error,
because pairs like `{"key", "value"}` can be both interpreted as
`object_t::value_type` or `std::initializer_list<basic_json>`, see
https://github.com/nlohmann/json/issues/235 for more information.
@liveexample{The example shows how initializer lists are treated as
objects when possible.,push_back__initializer_list}
*/
void push_back(std::initializer_list<basic_json> init)
{
if (is_object() and init.size() == 2 and init.begin()->is_string())
{
const string_t key = *init.begin();
push_back(typename object_t::value_type(key, *(init.begin() + 1)));
}
else
{
push_back(basic_json(init));
}
}
/*!
@brief add an object to an object
@copydoc push_back(std::initializer_list<basic_json>)
*/
reference operator+=(std::initializer_list<basic_json> init)
{
push_back(init);
return *this;
}
/*!
...
...
test/unit.cpp
浏览文件 @
91aa4c9d
...
...
@@ -7920,6 +7920,42 @@ TEST_CASE("modifiers")
"cannot use push_back() with number"
);
}
}
SECTION
(
"with initializer_list"
)
{
SECTION
(
"null"
)
{
json
j
;
j
.
push_back
({
"foo"
,
"bar"
});
CHECK
(
j
==
json
::
array
({{
"foo"
,
"bar"
}}));
json
k
;
k
.
push_back
({
1
,
2
,
3
});
CHECK
(
k
==
json
::
array
({{
1
,
2
,
3
}}));
}
SECTION
(
"array"
)
{
json
j
=
{
1
,
2
,
3
};
j
.
push_back
({
"foo"
,
"bar"
});
CHECK
(
j
==
json
({
1
,
2
,
3
,
{
"foo"
,
"bar"
}}));
json
k
=
{
1
,
2
,
3
};
k
.
push_back
({
1
,
2
,
3
});
CHECK
(
k
==
json
({
1
,
2
,
3
,
{
1
,
2
,
3
}}));
}
SECTION
(
"object"
)
{
json
j
=
{{
"key1"
,
1
}};
j
.
push_back
({
"key2"
,
"bar"
});
CHECK
(
j
==
json
({{
"key1"
,
1
},
{
"key2"
,
"bar"
}}));
json
k
=
{{
"key1"
,
1
}};
CHECK_THROWS_AS
(
k
.
push_back
({
1
,
2
,
3
,
4
}),
std
::
domain_error
);
CHECK_THROWS_WITH
(
k
.
push_back
({
1
,
2
,
3
,
4
}),
"cannot use push_back() with object"
);
}
}
}
SECTION
(
"operator+="
)
...
...
@@ -8016,6 +8052,42 @@ TEST_CASE("modifiers")
"cannot use push_back() with number"
);
}
}
SECTION
(
"with initializer_list"
)
{
SECTION
(
"null"
)
{
json
j
;
j
+=
{
"foo"
,
"bar"
};
CHECK
(
j
==
json
::
array
({{
"foo"
,
"bar"
}}));
json
k
;
k
+=
{
1
,
2
,
3
};
CHECK
(
k
==
json
::
array
({{
1
,
2
,
3
}}));
}
SECTION
(
"array"
)
{
json
j
=
{
1
,
2
,
3
};
j
+=
{
"foo"
,
"bar"
};
CHECK
(
j
==
json
({
1
,
2
,
3
,
{
"foo"
,
"bar"
}}));
json
k
=
{
1
,
2
,
3
};
k
+=
{
1
,
2
,
3
};
CHECK
(
k
==
json
({
1
,
2
,
3
,
{
1
,
2
,
3
}}));
}
SECTION
(
"object"
)
{
json
j
=
{{
"key1"
,
1
}};
j
+=
{
"key2"
,
"bar"
};
CHECK
(
j
==
json
({{
"key1"
,
1
},
{
"key2"
,
"bar"
}}));
json
k
=
{{
"key1"
,
1
}};
CHECK_THROWS_AS
((
k
+=
{
1
,
2
,
3
,
4
}),
std
::
domain_error
);
CHECK_THROWS_WITH
((
k
+=
{
1
,
2
,
3
,
4
}),
"cannot use push_back() with object"
);
}
}
}
SECTION
(
"insert"
)
...
...
@@ -13992,6 +14064,15 @@ TEST_CASE("regression tests")
CHECK
(
dest
==
expected
);
}
SECTION
(
"issue ##235 - ambiguous overload for 'push_back' and 'operator+='"
)
{
json
data
=
{{
"key"
,
"value"
}};
data
.
push_back
({
"key2"
,
"value2"
});
data
+=
{
"key3"
,
"value3"
};
CHECK
(
data
==
json
({{
"key"
,
"value"
},
{
"key2"
,
"value2"
},
{
"key3"
,
"value3"
}}));
}
}
// special test case to check if memory is leaked if constructor throws
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录