Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
畅游知识海洋
json
提交
a70a7a80
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,发现更多精彩内容 >>
提交
a70a7a80
编写于
12月 13, 2015
作者:
N
Niels
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixed #135: operator[] now only works on nonconst JSON objects
上级
c767f464
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
3 addition
and
145 deletion
+3
-145
README.md
README.md
+3
-5
src/json.hpp
src/json.hpp
+0
-57
src/json.hpp.re2c
src/json.hpp.re2c
+0
-57
test/unit.cpp
test/unit.cpp
+0
-26
未找到文件。
README.md
浏览文件 @
a70a7a80
...
@@ -42,11 +42,9 @@ to the files you want to use JSON objects. That's it. Do not forget to set the n
...
@@ -42,11 +42,9 @@ to the files you want to use JSON objects. That's it. Do not forget to set the n
Though it's 2015 already, the support for C++11 is still a bit sparse. Currently, the following compilers are known to work:
Though it's 2015 already, the support for C++11 is still a bit sparse. Currently, the following compilers are known to work:
-
GCC 4.9 - 5.2
-
GCC 4.9 - 5.2 (and possible later)
-
Clang 3.4 - 3.7
-
Clang 3.4 - 3.7 (and possible later)
-
Microsoft Visual C++ 14.0 RC
-
Microsoft Visual C++ 14.0 RC (and possible later)
Note using GCC 4.8, the unit tests cannot be compiled due to a
[
bug in the preprocessor
](
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55971
)
.
I would be happy to learn about other compilers/versions.
I would be happy to learn about other compilers/versions.
...
...
src/json.hpp
浏览文件 @
a70a7a80
...
@@ -2691,33 +2691,6 @@ class basic_json
...
@@ -2691,33 +2691,6 @@ class basic_json
Returns a reference to the element at with specified key @a key.
Returns a reference to the element at with specified key @a key.
@param[in] key key of the element to access
@return reference to the element at key @a key
@throw std::domain_error if JSON is not an object or null
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read using
the [] operator.,operatorarray__key_type_const}
*/
const_reference
operator
[](
const
typename
object_t
::
key_type
&
key
)
const
{
// at only works for objects
if
(
m_type
!=
value_t
::
object
)
{
throw
std
::
domain_error
(
"cannot use operator[] with "
+
type_name
());
}
return
m_value
.
object
->
operator
[](
key
);
}
/*!
@brief access specified object element
Returns a reference to the element at with specified key @a key.
@note If @a key is not found in the object, then it is silently added to
@note If @a key is not found in the object, then it is silently added to
the object and filled with a `null` value to make `key` a valid reference.
the object and filled with a `null` value to make `key` a valid reference.
In case the value was `null` before, it is converted to an object.
In case the value was `null` before, it is converted to an object.
...
@@ -2754,36 +2727,6 @@ class basic_json
...
@@ -2754,36 +2727,6 @@ class basic_json
return
m_value
.
object
->
operator
[](
key
);
return
m_value
.
object
->
operator
[](
key
);
}
}
/*!
@brief access specified object element
Returns a reference to the element at with specified key @a key.
@note This function is required for compatibility reasons with Clang.
@param[in] key key of the element to access
@return reference to the element at key @a key
@throw std::domain_error if JSON is not an object or null
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read using
the [] operator.,operatorarray__key_type_const}
*/
template
<
typename
T
,
std
::
size_t
n
>
const_reference
operator
[](
const
T
(
&
key
)[
n
])
const
{
// at only works for objects
if
(
m_type
!=
value_t
::
object
)
{
throw
std
::
domain_error
(
"cannot use operator[] with "
+
type_name
());
}
return
m_value
.
object
->
operator
[](
key
);
}
/*!
/*!
@brief access the first element
@brief access the first element
...
...
src/json.hpp.re2c
浏览文件 @
a70a7a80
...
@@ -2691,33 +2691,6 @@ class basic_json
...
@@ -2691,33 +2691,6 @@ class basic_json
Returns a reference to the element at with specified key @a key.
Returns a reference to the element at with specified key @a key.
@param[in] key key of the element to access
@return reference to the element at key @a key
@throw std::domain_error if JSON is not an object or null
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read using
the [] operator.,operatorarray__key_type_const}
*/
const_reference operator[](const typename object_t::key_type& key) const
{
// at only works for objects
if (m_type != value_t::object)
{
throw std::domain_error("cannot use operator[] with " + type_name());
}
return m_value.object->operator[](key);
}
/*!
@brief access specified object element
Returns a reference to the element at with specified key @a key.
@note If @a key is not found in the object, then it is silently added to
@note If @a key is not found in the object, then it is silently added to
the object and filled with a `null` value to make `key` a valid reference.
the object and filled with a `null` value to make `key` a valid reference.
In case the value was `null` before, it is converted to an object.
In case the value was `null` before, it is converted to an object.
...
@@ -2754,36 +2727,6 @@ class basic_json
...
@@ -2754,36 +2727,6 @@ class basic_json
return m_value.object->operator[](key);
return m_value.object->operator[](key);
}
}
/*!
@brief access specified object element
Returns a reference to the element at with specified key @a key.
@note This function is required for compatibility reasons with Clang.
@param[in] key key of the element to access
@return reference to the element at key @a key
@throw std::domain_error if JSON is not an object or null
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read using
the [] operator.,operatorarray__key_type_const}
*/
template<typename T, std::size_t n>
const_reference operator[](const T (&key)[n]) const
{
// at only works for objects
if (m_type != value_t::object)
{
throw std::domain_error("cannot use operator[] with " + type_name());
}
return m_value.object->operator[](key);
}
/*!
/*!
@brief access the first element
@brief access the first element
...
...
test/unit.cpp
浏览文件 @
a70a7a80
...
@@ -3098,38 +3098,24 @@ TEST_CASE("element access")
...
@@ -3098,38 +3098,24 @@ TEST_CASE("element access")
{
{
CHECK
(
j
[
"integer"
]
==
json
(
1
));
CHECK
(
j
[
"integer"
]
==
json
(
1
));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"integer"
)]
==
j
[
"integer"
]);
CHECK
(
j
[
json
::
object_t
::
key_type
(
"integer"
)]
==
j
[
"integer"
]);
CHECK
(
j_const
[
"integer"
]
==
json
(
1
));
CHECK
(
j_const
[
json
::
object_t
::
key_type
(
"integer"
)]
==
j
[
"integer"
]);
CHECK
(
j
[
"boolean"
]
==
json
(
true
));
CHECK
(
j
[
"boolean"
]
==
json
(
true
));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"boolean"
)]
==
j
[
"boolean"
]);
CHECK
(
j
[
json
::
object_t
::
key_type
(
"boolean"
)]
==
j
[
"boolean"
]);
CHECK
(
j_const
[
"boolean"
]
==
json
(
true
));
CHECK
(
j_const
[
json
::
object_t
::
key_type
(
"boolean"
)]
==
j
[
"boolean"
]);
CHECK
(
j
[
"null"
]
==
json
(
nullptr
));
CHECK
(
j
[
"null"
]
==
json
(
nullptr
));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"null"
)]
==
j
[
"null"
]);
CHECK
(
j
[
json
::
object_t
::
key_type
(
"null"
)]
==
j
[
"null"
]);
CHECK
(
j_const
[
"null"
]
==
json
(
nullptr
));
CHECK
(
j_const
[
json
::
object_t
::
key_type
(
"null"
)]
==
j
[
"null"
]);
CHECK
(
j
[
"string"
]
==
json
(
"hello world"
));
CHECK
(
j
[
"string"
]
==
json
(
"hello world"
));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"string"
)]
==
j
[
"string"
]);
CHECK
(
j
[
json
::
object_t
::
key_type
(
"string"
)]
==
j
[
"string"
]);
CHECK
(
j_const
[
"string"
]
==
json
(
"hello world"
));
CHECK
(
j_const
[
json
::
object_t
::
key_type
(
"string"
)]
==
j
[
"string"
]);
CHECK
(
j
[
"floating"
]
==
json
(
42.23
));
CHECK
(
j
[
"floating"
]
==
json
(
42.23
));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"floating"
)]
==
j
[
"floating"
]);
CHECK
(
j
[
json
::
object_t
::
key_type
(
"floating"
)]
==
j
[
"floating"
]);
CHECK
(
j_const
[
"floating"
]
==
json
(
42.23
));
CHECK
(
j_const
[
json
::
object_t
::
key_type
(
"floating"
)]
==
j
[
"floating"
]);
CHECK
(
j
[
"object"
]
==
json
(
json
::
object
()));
CHECK
(
j
[
"object"
]
==
json
(
json
::
object
()));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"object"
)]
==
j
[
"object"
]);
CHECK
(
j
[
json
::
object_t
::
key_type
(
"object"
)]
==
j
[
"object"
]);
CHECK
(
j_const
[
"object"
]
==
json
(
json
::
object
()));
CHECK
(
j_const
[
json
::
object_t
::
key_type
(
"object"
)]
==
j
[
"object"
]);
CHECK
(
j
[
"array"
]
==
json
({
1
,
2
,
3
}));
CHECK
(
j
[
"array"
]
==
json
({
1
,
2
,
3
}));
CHECK
(
j
[
json
::
object_t
::
key_type
(
"array"
)]
==
j
[
"array"
]);
CHECK
(
j
[
json
::
object_t
::
key_type
(
"array"
)]
==
j
[
"array"
]);
CHECK
(
j_const
[
"array"
]
==
json
({
1
,
2
,
3
}));
CHECK
(
j_const
[
json
::
object_t
::
key_type
(
"array"
)]
==
j
[
"array"
]);
}
}
SECTION
(
"access on non-object type"
)
SECTION
(
"access on non-object type"
)
...
@@ -3141,8 +3127,6 @@ TEST_CASE("element access")
...
@@ -3141,8 +3127,6 @@ TEST_CASE("element access")
const
json
j_const_nonobject
(
j_nonobject
);
const
json
j_const_nonobject
(
j_nonobject
);
CHECK_NOTHROW
(
j_nonobject
[
"foo"
]);
CHECK_NOTHROW
(
j_nonobject
[
"foo"
]);
CHECK_NOTHROW
(
j_nonobject2
[
json
::
object_t
::
key_type
(
"foo"
)]);
CHECK_NOTHROW
(
j_nonobject2
[
json
::
object_t
::
key_type
(
"foo"
)]);
CHECK_THROWS_AS
(
j_const_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
}
}
SECTION
(
"boolean"
)
SECTION
(
"boolean"
)
...
@@ -3151,8 +3135,6 @@ TEST_CASE("element access")
...
@@ -3151,8 +3135,6 @@ TEST_CASE("element access")
const
json
j_const_nonobject
(
j_nonobject
);
const
json
j_const_nonobject
(
j_nonobject
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
}
}
SECTION
(
"string"
)
SECTION
(
"string"
)
...
@@ -3161,8 +3143,6 @@ TEST_CASE("element access")
...
@@ -3161,8 +3143,6 @@ TEST_CASE("element access")
const
json
j_const_nonobject
(
j_nonobject
);
const
json
j_const_nonobject
(
j_nonobject
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
}
}
SECTION
(
"array"
)
SECTION
(
"array"
)
...
@@ -3171,8 +3151,6 @@ TEST_CASE("element access")
...
@@ -3171,8 +3151,6 @@ TEST_CASE("element access")
const
json
j_const_nonobject
(
j_nonobject
);
const
json
j_const_nonobject
(
j_nonobject
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
}
}
SECTION
(
"number (integer)"
)
SECTION
(
"number (integer)"
)
...
@@ -3181,8 +3159,6 @@ TEST_CASE("element access")
...
@@ -3181,8 +3159,6 @@ TEST_CASE("element access")
const
json
j_const_nonobject
(
j_nonobject
);
const
json
j_const_nonobject
(
j_nonobject
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
}
}
SECTION
(
"number (floating-point)"
)
SECTION
(
"number (floating-point)"
)
...
@@ -3191,8 +3167,6 @@ TEST_CASE("element access")
...
@@ -3191,8 +3167,6 @@ TEST_CASE("element access")
const
json
j_const_nonobject
(
j_nonobject
);
const
json
j_const_nonobject
(
j_nonobject
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
"foo"
],
std
::
domain_error
);
CHECK_THROWS_AS
(
j_const_nonobject
[
json
::
object_t
::
key_type
(
"foo"
)],
std
::
domain_error
);
}
}
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录