Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
cd9701b1
Commit
cd9701b1
authored
Jan 26, 2017
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
💄
cleanup
parent
1305e035
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
72 deletions
+84
-72
json.hpp
src/json.hpp
+42
-36
json.hpp.re2c
src/json.hpp.re2c
+42
-36
No files found.
src/json.hpp
View file @
cd9701b1
...
@@ -633,7 +633,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
...
@@ -633,7 +633,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
template
<
typename
BasicJsonType
>
template
<
typename
BasicJsonType
>
void
from_json
(
const
BasicJsonType
&
j
,
typename
BasicJsonType
::
boolean_t
&
b
)
void
from_json
(
const
BasicJsonType
&
j
,
typename
BasicJsonType
::
boolean_t
&
b
)
{
{
if
(
!
j
.
is_boolean
())
if
(
not
j
.
is_boolean
())
{
{
JSON_THROW
(
std
::
domain_error
(
"type must be boolean, but is "
+
j
.
type_name
()));
JSON_THROW
(
std
::
domain_error
(
"type must be boolean, but is "
+
j
.
type_name
()));
}
}
...
@@ -643,7 +643,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
...
@@ -643,7 +643,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
template
<
typename
BasicJsonType
>
template
<
typename
BasicJsonType
>
void
from_json
(
const
BasicJsonType
&
j
,
typename
BasicJsonType
::
string_t
&
s
)
void
from_json
(
const
BasicJsonType
&
j
,
typename
BasicJsonType
::
string_t
&
s
)
{
{
if
(
!
j
.
is_string
())
if
(
not
j
.
is_string
())
{
{
JSON_THROW
(
std
::
domain_error
(
"type must be string, but is "
+
j
.
type_name
()));
JSON_THROW
(
std
::
domain_error
(
"type must be string, but is "
+
j
.
type_name
()));
}
}
...
@@ -680,7 +680,7 @@ void from_json(const BasicJsonType& j, UnscopedEnumType& e)
...
@@ -680,7 +680,7 @@ void from_json(const BasicJsonType& j, UnscopedEnumType& e)
template
<
typename
BasicJsonType
>
template
<
typename
BasicJsonType
>
void
from_json
(
const
BasicJsonType
&
j
,
typename
BasicJsonType
::
array_t
&
arr
)
void
from_json
(
const
BasicJsonType
&
j
,
typename
BasicJsonType
::
array_t
&
arr
)
{
{
if
(
!
j
.
is_array
())
if
(
not
j
.
is_array
())
{
{
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
}
}
...
@@ -699,7 +699,7 @@ void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
...
@@ -699,7 +699,7 @@ void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
}
}
if
(
not
std
::
is_same
<
T
,
BasicJsonType
>::
value
)
if
(
not
std
::
is_same
<
T
,
BasicJsonType
>::
value
)
{
{
if
(
!
j
.
is_array
())
if
(
not
j
.
is_array
())
{
{
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
}
}
...
@@ -757,7 +757,7 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
...
@@ -757,7 +757,7 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
// when T == BasicJsonType, do not check if value_t is correct
// when T == BasicJsonType, do not check if value_t is correct
if
(
not
std
::
is_same
<
typename
CompatibleArrayType
::
value_type
,
BasicJsonType
>::
value
)
if
(
not
std
::
is_same
<
typename
CompatibleArrayType
::
value_type
,
BasicJsonType
>::
value
)
{
{
if
(
!
j
.
is_array
())
if
(
not
j
.
is_array
())
{
{
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
JSON_THROW
(
std
::
domain_error
(
"type must be array, but is "
+
j
.
type_name
()));
}
}
...
@@ -769,7 +769,7 @@ template<typename BasicJsonType, typename CompatibleObjectType,
...
@@ -769,7 +769,7 @@ template<typename BasicJsonType, typename CompatibleObjectType,
enable_if_t
<
is_compatible_object_type
<
BasicJsonType
,
CompatibleObjectType
>::
value
,
int
>
=
0
>
enable_if_t
<
is_compatible_object_type
<
BasicJsonType
,
CompatibleObjectType
>::
value
,
int
>
=
0
>
void
from_json
(
const
BasicJsonType
&
j
,
CompatibleObjectType
&
obj
)
void
from_json
(
const
BasicJsonType
&
j
,
CompatibleObjectType
&
obj
)
{
{
if
(
!
j
.
is_object
())
if
(
not
j
.
is_object
())
{
{
JSON_THROW
(
std
::
domain_error
(
"type must be object, but is "
+
j
.
type_name
()));
JSON_THROW
(
std
::
domain_error
(
"type must be object, but is "
+
j
.
type_name
()));
}
}
...
@@ -828,6 +828,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
...
@@ -828,6 +828,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
struct
to_json_fn
struct
to_json_fn
{
{
private
:
template
<
typename
BasicJsonType
,
typename
T
>
template
<
typename
BasicJsonType
,
typename
T
>
auto
call
(
BasicJsonType
&
j
,
T
&&
val
,
priority_tag
<
1
>
)
const
noexcept
(
noexcept
(
to_json
(
j
,
std
::
forward
<
T
>
(
val
))))
auto
call
(
BasicJsonType
&
j
,
T
&&
val
,
priority_tag
<
1
>
)
const
noexcept
(
noexcept
(
to_json
(
j
,
std
::
forward
<
T
>
(
val
))))
->
decltype
(
to_json
(
j
,
std
::
forward
<
T
>
(
val
)),
void
())
->
decltype
(
to_json
(
j
,
std
::
forward
<
T
>
(
val
)),
void
())
...
@@ -887,52 +888,57 @@ struct static_const
...
@@ -887,52 +888,57 @@ struct static_const
template
<
typename
T
>
template
<
typename
T
>
constexpr
T
static_const
<
T
>::
value
;
constexpr
T
static_const
<
T
>::
value
;
/*!
@brief helper class to create locales with decimal point
This struct is used a default locale during the JSON serialization. JSON
requires the decimal point to be `.`, so this function overloads the
`do_decimal_point()` function to return `.`. This function is called by
float-to-string conversions to retrieve the decimal separator between integer
and fractional parts.
@sa https://github.com/nlohmann/json/issues/51#issuecomment-86869315
@since version 2.0.0
*/
struct
DecimalSeparator
:
std
::
numpunct
<
char
>
{
char
do_decimal_point
()
const
{
return
'.'
;
}
};
}
// namespace detail
}
// namespace detail
/// namespace to hold default `to_json` / `from_json` functions
namespace
namespace
{
{
constexpr
const
auto
&
to_json
=
detail
::
static_const
<
detail
::
to_json_fn
>::
value
;
constexpr
const
auto
&
to_json
=
detail
::
static_const
<
detail
::
to_json_fn
>::
value
;
constexpr
const
auto
&
from_json
=
detail
::
static_const
<
detail
::
from_json_fn
>::
value
;
constexpr
const
auto
&
from_json
=
detail
::
static_const
<
detail
::
from_json_fn
>::
value
;
}
}
// default JSONSerializer template argument, doesn't care about template argument
// will use ADL for serialization
/*!
@brief default JSONSerializer template argument
This serializer ignores the template arguments and uses ADL
([argument-dependent lookup](http://en.cppreference.com/w/cpp/language/adl))
for serialization.
*/
template
<
typename
=
void
,
typename
=
void
>
template
<
typename
=
void
,
typename
=
void
>
struct
adl_serializer
struct
adl_serializer
{
{
template
<
typename
BasicJsonType
,
typename
T
>
/*!
static
void
from_json
(
BasicJsonType
&&
j
,
T
&
val
)
noexcept
(
noexcept
(
::
nlohmann
::
from_json
(
std
::
forward
<
BasicJsonType
>
(
j
),
val
)))
@brief convert a JSON value to any value type
This function is usually called by the `get()` function of the
@ref basic_json class (either explicit or via conversion operators).
@param[in] j JSON value to read from
@param[in, out] val value to write to
*/
template
<
typename
BasicJsonType
,
typename
ValueType
>
static
void
from_json
(
BasicJsonType
&&
j
,
ValueType
&
val
)
noexcept
(
noexcept
(
::
nlohmann
::
from_json
(
std
::
forward
<
BasicJsonType
>
(
j
),
val
)))
{
{
::
nlohmann
::
from_json
(
std
::
forward
<
BasicJsonType
>
(
j
),
val
);
::
nlohmann
::
from_json
(
std
::
forward
<
BasicJsonType
>
(
j
),
val
);
}
}
template
<
typename
BasicJsonType
,
typename
T
>
/*!
static
void
to_json
(
BasicJsonType
&
j
,
T
&&
val
)
noexcept
(
@brief convert any value type to a JSON value
noexcept
(
::
nlohmann
::
to_json
(
j
,
std
::
forward
<
T
>
(
val
))))
This function is usually called by the constructors of the @ref basic_json
class.
@param[in, out] j JSON value to write to
@param[in] val value to read from
*/
template
<
typename
BasicJsonType
,
typename
ValueType
>
static
void
to_json
(
BasicJsonType
&
j
,
ValueType
&&
val
)
noexcept
(
noexcept
(
::
nlohmann
::
to_json
(
j
,
std
::
forward
<
ValueType
>
(
val
))))
{
{
::
nlohmann
::
to_json
(
j
,
std
::
forward
<
T
>
(
val
));
::
nlohmann
::
to_json
(
j
,
std
::
forward
<
ValueType
>
(
val
));
}
}
};
};
...
@@ -957,7 +963,7 @@ default; will be used in @ref number_float_t)
...
@@ -957,7 +963,7 @@ default; will be used in @ref number_float_t)
@tparam AllocatorType type of the allocator to use (`std::allocator` by
@tparam AllocatorType type of the allocator to use (`std::allocator` by
default)
default)
@tparam JSONSerializer the serializer to resolve internal calls to `to_json()`
@tparam JSONSerializer the serializer to resolve internal calls to `to_json()`
and `from_json()`
and `from_json()`
(@ref adl_serializer by default)
@requirement The class satisfies the following concept requirements:
@requirement The class satisfies the following concept requirements:
- Basic
- Basic
...
...
src/json.hpp.re2c
View file @
cd9701b1
...
@@ -633,7 +633,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
...
@@ -633,7 +633,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
template<typename BasicJsonType>
template<typename BasicJsonType>
void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
{
{
if (
!
j.is_boolean())
if (
not
j.is_boolean())
{
{
JSON_THROW(std::domain_error("type must be boolean, but is " + j.type_name()));
JSON_THROW(std::domain_error("type must be boolean, but is " + j.type_name()));
}
}
...
@@ -643,7 +643,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
...
@@ -643,7 +643,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
template<typename BasicJsonType>
template<typename BasicJsonType>
void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
{
{
if (
!
j.is_string())
if (
not
j.is_string())
{
{
JSON_THROW(std::domain_error("type must be string, but is " + j.type_name()));
JSON_THROW(std::domain_error("type must be string, but is " + j.type_name()));
}
}
...
@@ -680,7 +680,7 @@ void from_json(const BasicJsonType& j, UnscopedEnumType& e)
...
@@ -680,7 +680,7 @@ void from_json(const BasicJsonType& j, UnscopedEnumType& e)
template<typename BasicJsonType>
template<typename BasicJsonType>
void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
{
{
if (
!
j.is_array())
if (
not
j.is_array())
{
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
}
}
...
@@ -699,7 +699,7 @@ void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
...
@@ -699,7 +699,7 @@ void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
}
}
if (not std::is_same<T, BasicJsonType>::value)
if (not std::is_same<T, BasicJsonType>::value)
{
{
if (
!
j.is_array())
if (
not
j.is_array())
{
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
}
}
...
@@ -757,7 +757,7 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
...
@@ -757,7 +757,7 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
// when T == BasicJsonType, do not check if value_t is correct
// when T == BasicJsonType, do not check if value_t is correct
if (not std::is_same<typename CompatibleArrayType::value_type, BasicJsonType>::value)
if (not std::is_same<typename CompatibleArrayType::value_type, BasicJsonType>::value)
{
{
if (
!
j.is_array())
if (
not
j.is_array())
{
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
}
}
...
@@ -769,7 +769,7 @@ template<typename BasicJsonType, typename CompatibleObjectType,
...
@@ -769,7 +769,7 @@ template<typename BasicJsonType, typename CompatibleObjectType,
enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value, int> = 0>
enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value, int> = 0>
void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
{
{
if (
!
j.is_object())
if (
not
j.is_object())
{
{
JSON_THROW(std::domain_error("type must be object, but is " + j.type_name()));
JSON_THROW(std::domain_error("type must be object, but is " + j.type_name()));
}
}
...
@@ -828,6 +828,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
...
@@ -828,6 +828,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
struct to_json_fn
struct to_json_fn
{
{
private:
template<typename BasicJsonType, typename T>
template<typename BasicJsonType, typename T>
auto call(BasicJsonType& j, T&& val, priority_tag<1>) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
auto call(BasicJsonType& j, T&& val, priority_tag<1>) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
-> decltype(to_json(j, std::forward<T>(val)), void())
-> decltype(to_json(j, std::forward<T>(val)), void())
...
@@ -887,52 +888,57 @@ struct static_const
...
@@ -887,52 +888,57 @@ struct static_const
template<typename T>
template<typename T>
constexpr T static_const<T>::value;
constexpr T static_const<T>::value;
/*!
@brief helper class to create locales with decimal point
This struct is used a default locale during the JSON serialization. JSON
requires the decimal point to be `.`, so this function overloads the
`do_decimal_point()` function to return `.`. This function is called by
float-to-string conversions to retrieve the decimal separator between integer
and fractional parts.
@sa https://github.com/nlohmann/json/issues/51#issuecomment-86869315
@since version 2.0.0
*/
struct DecimalSeparator : std::numpunct<char>
{
char do_decimal_point() const
{
return '.';
}
};
} // namespace detail
} // namespace detail
/// namespace to hold default `to_json` / `from_json` functions
namespace
namespace
{
{
constexpr const auto& to_json = detail::static_const<detail::to_json_fn>::value;
constexpr const auto& to_json = detail::static_const<detail::to_json_fn>::value;
constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value;
constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value;
}
}
// default JSONSerializer template argument, doesn't care about template argument
// will use ADL for serialization
/*!
@brief default JSONSerializer template argument
This serializer ignores the template arguments and uses ADL
([argument-dependent lookup](http://en.cppreference.com/w/cpp/language/adl))
for serialization.
*/
template<typename = void, typename = void>
template<typename = void, typename = void>
struct adl_serializer
struct adl_serializer
{
{
template<typename BasicJsonType, typename T>
/*!
static void from_json(BasicJsonType&& j, T& val) noexcept(noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
@brief convert a JSON value to any value type
This function is usually called by the `get()` function of the
@ref basic_json class (either explicit or via conversion operators).
@param[in] j JSON value to read from
@param[in, out] val value to write to
*/
template<typename BasicJsonType, typename ValueType>
static void from_json(BasicJsonType&& j, ValueType& val) noexcept(
noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
{
{
::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
}
}
template<typename BasicJsonType, typename T>
/*!
static void to_json(BasicJsonType& j, T&& val) noexcept(
@brief convert any value type to a JSON value
noexcept(::nlohmann::to_json(j, std::forward<T>(val))))
This function is usually called by the constructors of the @ref basic_json
class.
@param[in, out] j JSON value to write to
@param[in] val value to read from
*/
template<typename BasicJsonType, typename ValueType>
static void to_json(BasicJsonType& j, ValueType&& val) noexcept(
noexcept(::nlohmann::to_json(j, std::forward<ValueType>(val))))
{
{
::nlohmann::to_json(j, std::forward<
T
>(val));
::nlohmann::to_json(j, std::forward<
ValueType
>(val));
}
}
};
};
...
@@ -957,7 +963,7 @@ default; will be used in @ref number_float_t)
...
@@ -957,7 +963,7 @@ default; will be used in @ref number_float_t)
@tparam AllocatorType type of the allocator to use (`std::allocator` by
@tparam AllocatorType type of the allocator to use (`std::allocator` by
default)
default)
@tparam JSONSerializer the serializer to resolve internal calls to `to_json()`
@tparam JSONSerializer the serializer to resolve internal calls to `to_json()`
and `from_json()`
and `from_json()`
(@ref adl_serializer by default)
@requirement The class satisfies the following concept requirements:
@requirement The class satisfies the following concept requirements:
- Basic
- Basic
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment