Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
畅游知识海洋
json
提交
6b89785f
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,发现更多精彩内容 >>
提交
6b89785f
编写于
1月 07, 2017
作者:
T
Théo DELRIEU
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
replace constructor by from/to_json: boolean_t
上级
c833b22b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
86 addition
and
56 deletion
+86
-56
src/json.hpp
src/json.hpp
+43
-28
src/json.hpp.re2c
src/json.hpp.re2c
+43
-28
未找到文件。
src/json.hpp
浏览文件 @
6b89785f
...
...
@@ -202,6 +202,21 @@ template <typename Json> std::string type_name(Json const &j)
}
}
// This is an experiment. I need this to move constructors out of basic_json.
// I'm sure there is a better way, but this might need a big basic_json refactoring
template
<
value_t
>
struct
external_constructor
;
template
<
>
struct
external_constructor
<
value_t
::
boolean
>
{
template
<
typename
Json
>
static
void
construct
(
Json
&
j
,
typename
Json
::
boolean_t
b
)
noexcept
{
j
.
m_type
=
value_t
::
boolean
;
j
.
m_value
=
b
;
j
.
assert_invariant
();
}
};
// very useful construct against boilerplate (more boilerplate needed than in
// C++17: http://en.cppreference.com/w/cpp/types/void_t)
template
<
typename
...
>
struct
make_void
...
...
@@ -340,9 +355,11 @@ struct is_compatible_integer_type_impl<true, RealIntegerType, CompatibleNumberIn
template
<
typename
RealIntegerType
,
typename
CompatibleNumberIntegerType
>
struct
is_compatible_integer_type
{
static
constexpr
auto
value
=
is_compatible_integer_type_impl
<
std
::
is_arithmetic
<
CompatibleNumberIntegerType
>::
value
,
RealIntegerType
,
CompatibleNumberIntegerType
>::
value
;
static
constexpr
auto
value
=
is_compatible_integer_type_impl
<
std
::
is_arithmetic
<
CompatibleNumberIntegerType
>::
value
and
not
std
::
is_same
<
bool
,
CompatibleNumberIntegerType
>::
value
,
RealIntegerType
,
CompatibleNumberIntegerType
>
::
value
;
};
template
<
typename
RealFloat
,
typename
CompatibleFloat
>
...
...
@@ -360,7 +377,6 @@ struct is_compatible_basic_json_type
is_unscoped_enum
<
T
>::
value
or
std
::
is_same
<
T
,
BasicJson
>::
value
or
std
::
is_constructible
<
typename
BasicJson
::
string_t
,
T
>::
value
or
std
::
is_same
<
typename
BasicJson
::
boolean_t
,
T
>::
value
or
is_compatible_array_type
<
BasicJson
,
T
>::
value
or
is_compatible_object_type
<
typename
BasicJson
::
object_t
,
T
>::
value
or
is_compatible_float_type
<
typename
BasicJson
::
number_float_t
,
T
>::
value
or
...
...
@@ -433,6 +449,22 @@ struct has_to_json
void
to_json
();
void
from_json
();
// overloads for basic_json template parameters
template
<
typename
Json
>
void
to_json
(
Json
&
j
,
typename
Json
::
boolean_t
b
)
noexcept
{
external_constructor
<
value_t
::
boolean
>::
construct
(
j
,
b
);
}
template
<
typename
Json
>
void
from_json
(
Json
const
&
j
,
typename
Json
::
boolean_t
&
b
)
{
if
(
!
j
.
is_boolean
())
throw
std
::
domain_error
(
"type must be boolean, but is "
+
type_name
(
j
));
b
=
*
const_cast
<
Json
&>
(
j
).
template
get_ptr
<
typename
Json
::
boolean_t
*
>();
}
struct
to_json_fn
{
// is it really useful to mark those as constexpr?
...
...
@@ -606,6 +638,7 @@ template <
class
basic_json
{
private:
template
<::
nlohmann
::
value_t
>
friend
struct
detail
::
external_constructor
;
template
<
typename
Json
>
friend
std
::
string
detail
::
type_name
(
Json
const
&
);
/// workaround type for MSVC
using
basic_json_t
=
basic_json
<
ObjectType
,
ArrayType
,
StringType
,
...
...
@@ -1731,26 +1764,6 @@ class basic_json
assert_invariant
();
}
/*!
@brief create a boolean (explicit)
Creates a JSON boolean type from a given value.
@param[in] val a boolean value to store
@complexity Constant.
@liveexample{The example below demonstrates boolean
values.,basic_json__boolean_t}
@since version 1.0.0
*/
basic_json
(
boolean_t
val
)
noexcept
:
m_type
(
value_t
::
boolean
),
m_value
(
val
)
{
assert_invariant
();
}
/*!
@brief create an integer number (explicit)
...
...
@@ -3330,11 +3343,13 @@ class basic_json
@since version 1.0.0
*/
template
<
typename
ValueType
,
enable_if_t
<
not
std
::
is_pointer
<
ValueType
>
::
value
,
int
>
=
0
>
enable_if_t
<
not
std
::
is_pointer
<
ValueType
>
::
value
and
detail
::
is_compatible_basic_json_type
<
uncvref_t
<
ValueType
>
,
basic_json_t
>::
value
,
int
>
=
0
>
auto
get
()
const
->
decltype
(
this
->
get_impl
(
static_cast
<
ValueType
*>
(
nullptr
)))
{
return
get_impl
(
static_cast
<
ValueType
*>
(
nullptr
));
->
decltype
(
this
->
get_impl
(
static_cast
<
ValueType
*>
(
nullptr
)))
{
return
get_impl
(
static_cast
<
ValueType
*>
(
nullptr
));
}
template
<
...
...
src/json.hpp.re2c
浏览文件 @
6b89785f
...
...
@@ -202,6 +202,21 @@ template <typename Json> std::string type_name(Json const &j)
}
}
// This is an experiment. I need this to move constructors out of basic_json.
// I'm sure there is a better way, but this might need a big basic_json refactoring
template <value_t> struct external_constructor;
template <>
struct external_constructor<value_t::boolean>
{
template <typename Json>
static void construct(Json &j, typename Json::boolean_t b) noexcept
{
j.m_type = value_t::boolean;
j.m_value = b;
j.assert_invariant();
}
};
// very useful construct against boilerplate (more boilerplate needed than in
// C++17: http://en.cppreference.com/w/cpp/types/void_t)
template <typename...> struct make_void
...
...
@@ -340,9 +355,11 @@ struct is_compatible_integer_type_impl<true, RealIntegerType, CompatibleNumberIn
template <typename RealIntegerType, typename CompatibleNumberIntegerType>
struct is_compatible_integer_type
{
static constexpr auto value = is_compatible_integer_type_impl <
std::is_arithmetic<CompatibleNumberIntegerType>::value, RealIntegerType,
CompatibleNumberIntegerType >::value;
static constexpr auto
value = is_compatible_integer_type_impl <
std::is_arithmetic<CompatibleNumberIntegerType>::value and
not std::is_same<bool, CompatibleNumberIntegerType>::value,
RealIntegerType, CompatibleNumberIntegerType > ::value;
};
template <typename RealFloat, typename CompatibleFloat>
...
...
@@ -360,7 +377,6 @@ struct is_compatible_basic_json_type
is_unscoped_enum<T>::value or
std::is_same<T, BasicJson>::value or
std::is_constructible<typename BasicJson::string_t, T>::value or
std::is_same<typename BasicJson::boolean_t, T>::value or
is_compatible_array_type<BasicJson, T>::value or
is_compatible_object_type<typename BasicJson::object_t, T>::value or
is_compatible_float_type<typename BasicJson::number_float_t, T>::value or
...
...
@@ -433,6 +449,22 @@ struct has_to_json
void to_json();
void from_json();
// overloads for basic_json template parameters
template <typename Json>
void to_json(Json &j, typename Json::boolean_t b) noexcept
{
external_constructor<value_t::boolean>::construct(j, b);
}
template <typename Json>
void from_json(Json const& j, typename Json::boolean_t& b)
{
if (!j.is_boolean())
throw std::domain_error("type must be boolean, but is " + type_name(j));
b = *const_cast<Json&>(j).template get_ptr<typename Json::boolean_t*>();
}
struct to_json_fn
{
// is it really useful to mark those as constexpr?
...
...
@@ -607,6 +639,7 @@ template <
class basic_json
{
private:
template <::nlohmann::value_t> friend struct detail::external_constructor;
template <typename Json> friend std::string detail::type_name(Json const &);
/// workaround type for MSVC
using basic_json_t = basic_json<ObjectType, ArrayType, StringType,
...
...
@@ -1732,26 +1765,6 @@ class basic_json
assert_invariant();
}
/*!
@brief create a boolean (explicit)
Creates a JSON boolean type from a given value.
@param[in] val a boolean value to store
@complexity Constant.
@liveexample{The example below demonstrates boolean
values.,basic_json__boolean_t}
@since version 1.0.0
*/
basic_json(boolean_t val) noexcept
: m_type(value_t::boolean), m_value(val)
{
assert_invariant();
}
/*!
@brief create an integer number (explicit)
...
...
@@ -3328,11 +3341,13 @@ class basic_json
@since version 1.0.0
*/
template <typename ValueType,
enable_if_t<not std::is_pointer<ValueType>::value, int> = 0>
enable_if_t<not std::is_pointer<ValueType>::value and
detail::is_compatible_basic_json_type<
uncvref_t<ValueType>, basic_json_t>::value,
int> = 0>
auto get() const
-> decltype(this->get_impl(static_cast<ValueType*>(nullptr)))
{
return get_impl(static_cast<ValueType*>(nullptr));
-> decltype(this->get_impl(static_cast<ValueType *>(nullptr))) {
return get_impl(static_cast<ValueType *>(nullptr));
}
template <
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录