Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
71d20db4
Commit
71d20db4
authored
Jul 01, 2016
by
Niels
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/improved_documentation' into develop
- improved documentation
parents
c6d45c30
05ddfaf5
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
144 additions
and
43 deletions
+144
-43
doc/examples/count.cpp
doc/examples/count.cpp
+0
-1
doc/examples/count.link
doc/examples/count.link
+1
-1
src/json.hpp
src/json.hpp
+71
-20
src/json.hpp.re2c
src/json.hpp.re2c
+72
-21
No files found.
doc/examples/count.cpp
View file @
71d20db4
...
...
@@ -12,7 +12,6 @@ int main()
auto
count_three
=
j_object
.
count
(
"three"
);
// print values
std
::
cout
<<
std
::
boolalpha
;
std
::
cout
<<
"number of elements with key
\"
two
\"
: "
<<
count_two
<<
'\n'
;
std
::
cout
<<
"number of elements with key
\"
three
\"
: "
<<
count_three
<<
'\n'
;
}
doc/examples/count.link
View file @
71d20db4
<a target="_blank" href="http://melpon.org/wandbox/permlink/XoXqF9LlWu8jlNgE"><b>online</b></a>
\ No newline at end of file
<a target="_blank" href="http://melpon.org/wandbox/permlink/yv5TMrq9qREivvHf"><b>online</b></a>
\ No newline at end of file
src/json.hpp
View file @
71d20db4
...
...
@@ -90,7 +90,13 @@ namespace
{
/*!
@brief Helper to determine whether there's a key_type for T.
Thus helper is used to tell associative containers apart from other containers
such as sequence containers. For instance, `std::map` passes the test as it
contains a `mapped_type`, whereas `std::vector` fails the test.
@sa http://stackoverflow.com/a/7728728/266378
@since version 1.0.0
*/
template
<
typename
T
>
struct
has_mapped_type
...
...
@@ -104,11 +110,18 @@ struct has_mapped_type
/*!
@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
*/
class
DecimalSeparator
:
public
std
::
numpunct
<
char
>
struct
DecimalSeparator
:
std
::
numpunct
<
char
>
{
protected:
char
do_decimal_point
()
const
{
return
'.'
;
...
...
@@ -202,13 +215,8 @@ class basic_json
{
private:
/// workaround type for MSVC
using
basic_json_t
=
basic_json
<
ObjectType
,
ArrayType
,
StringType
,
BooleanType
,
NumberIntegerType
,
NumberUnsignedType
,
NumberFloatType
,
using
basic_json_t
=
basic_json
<
ObjectType
,
ArrayType
,
StringType
,
BooleanType
,
NumberIntegerType
,
NumberUnsignedType
,
NumberFloatType
,
AllocatorType
>
;
public:
...
...
@@ -221,6 +229,8 @@ class basic_json
/////////////////////
/// @name container types
/// The canonic container types to use @ref basic_json like any other STL
/// container.
/// @{
/// the type of elements in a basic_json container
...
...
@@ -270,6 +280,8 @@ class basic_json
///////////////////////////
/// @name JSON value data types
/// The data types to store a JSON value. These types are derived from
/// the template arguments passed to class @ref basic_json.
/// @{
/*!
...
...
@@ -937,6 +949,8 @@ class basic_json
//////////////////
/// @name constructors and destructors
/// Constructors of class @ref basic_json, copy/move constructor, copy
/// assignment, static functions creating objects, and the destructor.
/// @{
/*!
...
...
@@ -2074,19 +2088,20 @@ class basic_json
///////////////////////
/// @name object inspection
/// Functions to inspect the type of a JSON value.
/// @{
/*!
@brief serialization
Serialization function for JSON values. The function tries to mimic
Python's
@p json.dumps() function, and currently supports its @p
indent
Python's
`json.dumps()` function, and currently supports its @a
indent
parameter.
@param[in] indent
i
f indent is nonnegative, then array elements and object
@param[in] indent
I
f indent is nonnegative, then array elements and object
members will be pretty-printed with that indent level. An indent level of
0 will only insert newlines. -1
(the default) selects the most compact
representation
`0` will only insert newlines. `-1`
(the default) selects the most compact
representation
.
@return string containing the serialization of the JSON value
...
...
@@ -2772,6 +2787,7 @@ class basic_json
public:
/// @name value access
/// Direct access to the stored value of a JSON value.
/// @{
/*!
...
...
@@ -2822,7 +2838,8 @@ class basic_json
Explicit pointer access to the internally stored JSON value. No copies are
made.
@warning The pointer becomes invalid if the underlying JSON object changes.
@warning The pointer becomes invalid if the underlying JSON object
changes.
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
...
...
@@ -2877,7 +2894,8 @@ class basic_json
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
@ref number_unsigned_t, or @ref number_float_t.
@ref number_unsigned_t, or @ref number_float_t. Enforced by a static
assertion.
@return pointer to the internally stored JSON value if the requested
pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
...
...
@@ -2897,6 +2915,21 @@ class basic_json
,
int
>::
type
=
0
>
PointerType
get_ptr
()
noexcept
{
// get the type of the PointerType (remove pointer and const)
using
pointee_t
=
typename
std
::
remove_const
<
typename
std
::
remove_pointer
<
typename
std
::
remove_const
<
PointerType
>::
type
>::
type
>::
type
;
// make sure the type matches the allowed types
static_assert
(
std
::
is_same
<
object_t
,
pointee_t
>::
value
or
std
::
is_same
<
array_t
,
pointee_t
>::
value
or
std
::
is_same
<
string_t
,
pointee_t
>::
value
or
std
::
is_same
<
boolean_t
,
pointee_t
>::
value
or
std
::
is_same
<
number_integer_t
,
pointee_t
>::
value
or
std
::
is_same
<
number_unsigned_t
,
pointee_t
>::
value
or
std
::
is_same
<
number_float_t
,
pointee_t
>::
value
,
"incompatible pointer type"
);
// delegate the call to get_impl_ptr<>()
return
get_impl_ptr
(
static_cast
<
PointerType
>
(
nullptr
));
}
...
...
@@ -2912,6 +2945,21 @@ class basic_json
,
int
>::
type
=
0
>
constexpr
const
PointerType
get_ptr
()
const
noexcept
{
// get the type of the PointerType (remove pointer and const)
using
pointee_t
=
typename
std
::
remove_const
<
typename
std
::
remove_pointer
<
typename
std
::
remove_const
<
PointerType
>::
type
>::
type
>::
type
;
// make sure the type matches the allowed types
static_assert
(
std
::
is_same
<
object_t
,
pointee_t
>::
value
or
std
::
is_same
<
array_t
,
pointee_t
>::
value
or
std
::
is_same
<
string_t
,
pointee_t
>::
value
or
std
::
is_same
<
boolean_t
,
pointee_t
>::
value
or
std
::
is_same
<
number_integer_t
,
pointee_t
>::
value
or
std
::
is_same
<
number_unsigned_t
,
pointee_t
>::
value
or
std
::
is_same
<
number_float_t
,
pointee_t
>::
value
,
"incompatible pointer type"
);
// delegate the call to get_impl_ptr<>() const
return
get_impl_ptr
(
static_cast
<
const
PointerType
>
(
nullptr
));
}
...
...
@@ -2927,7 +2975,7 @@ class basic_json
@tparam ReferenceType reference type; must be a reference to @ref array_t,
@ref object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, or
@ref number_float_t.
@ref number_float_t.
Enforced by static assertion.
@return reference to the internally stored JSON value if the requested
reference type @a ReferenceType fits to the JSON value; throws
...
...
@@ -3017,6 +3065,7 @@ class basic_json
////////////////////
/// @name element access
/// Access to the JSON value.
/// @{
/*!
...
...
@@ -8132,7 +8181,7 @@ basic_json_parser_63:
}
/// return string representation of last read token
string_t
get_token
()
const
string_t
get_token
_string
()
const
{
assert
(
m_start
!=
nullptr
);
return
string_t
(
reinterpret_cast
<
typename
string_t
::
const_pointer
>
(
m_start
),
...
...
@@ -8400,7 +8449,7 @@ basic_json_parser_63:
if
(
type
!=
value_t
::
number_float
)
{
// multiply last value by ten and add the new digit
auto
temp
=
value
*
10
+
*
curptr
-
0x30
;
auto
temp
=
value
*
10
+
*
curptr
-
'0'
;
// test for overflow
if
(
temp
<
value
||
temp
>
max
)
...
...
@@ -8693,7 +8742,8 @@ basic_json_parser_63:
if
(
t
!=
last_token
)
{
std
::
string
error_msg
=
"parse error - unexpected "
;
error_msg
+=
(
last_token
==
lexer
::
token_type
::
parse_error
?
(
"'"
+
m_lexer
.
get_token
()
+
"'"
)
:
error_msg
+=
(
last_token
==
lexer
::
token_type
::
parse_error
?
(
"'"
+
m_lexer
.
get_token_string
()
+
"'"
)
:
lexer
::
token_type_name
(
last_token
));
error_msg
+=
"; expected "
+
lexer
::
token_type_name
(
t
);
throw
std
::
invalid_argument
(
error_msg
);
...
...
@@ -8705,7 +8755,8 @@ basic_json_parser_63:
if
(
t
==
last_token
)
{
std
::
string
error_msg
=
"parse error - unexpected "
;
error_msg
+=
(
last_token
==
lexer
::
token_type
::
parse_error
?
(
"'"
+
m_lexer
.
get_token
()
+
"'"
)
:
error_msg
+=
(
last_token
==
lexer
::
token_type
::
parse_error
?
(
"'"
+
m_lexer
.
get_token_string
()
+
"'"
)
:
lexer
::
token_type_name
(
last_token
));
throw
std
::
invalid_argument
(
error_msg
);
}
...
...
src/json.hpp.re2c
View file @
71d20db4
...
...
@@ -90,7 +90,13 @@ namespace
{
/*!
@brief Helper to determine whether there's a key_type for T.
Thus helper is used to tell associative containers apart from other containers
such as sequence containers. For instance, `std::map` passes the test as it
contains a `mapped_type`, whereas `std::vector` fails the test.
@sa http://stackoverflow.com/a/7728728/266378
@since version 1.0.0
*/
template<typename T>
struct has_mapped_type
...
...
@@ -104,11 +110,18 @@ struct has_mapped_type
/*!
@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
*/
class DecimalSeparator : public
std::numpunct<char>
struct DecimalSeparator :
std::numpunct<char>
{
protected:
char do_decimal_point() const
{
return '.';
...
...
@@ -202,13 +215,8 @@ class basic_json
{
private:
/// workaround type for MSVC
using basic_json_t = basic_json<ObjectType,
ArrayType,
StringType,
BooleanType,
NumberIntegerType,
NumberUnsignedType,
NumberFloatType,
using basic_json_t = basic_json<ObjectType, ArrayType, StringType,
BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType,
AllocatorType>;
public:
...
...
@@ -221,6 +229,8 @@ class basic_json
/////////////////////
/// @name container types
/// The canonic container types to use @ref basic_json like any other STL
/// container.
/// @{
/// the type of elements in a basic_json container
...
...
@@ -270,6 +280,8 @@ class basic_json
///////////////////////////
/// @name JSON value data types
/// The data types to store a JSON value. These types are derived from
/// the template arguments passed to class @ref basic_json.
/// @{
/*!
...
...
@@ -937,6 +949,8 @@ class basic_json
//////////////////
/// @name constructors and destructors
/// Constructors of class @ref basic_json, copy/move constructor, copy
/// assignment, static functions creating objects, and the destructor.
/// @{
/*!
...
...
@@ -2074,19 +2088,20 @@ class basic_json
///////////////////////
/// @name object inspection
/// Functions to inspect the type of a JSON value.
/// @{
/*!
@brief serialization
Serialization function for JSON values. The function tries to mimic
Python's
@p json.dumps() function, and currently supports its @p
indent
Python's
`json.dumps()` function, and currently supports its @a
indent
parameter.
@param[in] indent
i
f indent is nonnegative, then array elements and object
@param[in] indent
I
f indent is nonnegative, then array elements and object
members will be pretty-printed with that indent level. An indent level of
0 will only insert newlines. -1
(the default) selects the most compact
representation
`0` will only insert newlines. `-1`
(the default) selects the most compact
representation
.
@return string containing the serialization of the JSON value
...
...
@@ -2772,6 +2787,7 @@ class basic_json
public:
/// @name value access
/// Direct access to the stored value of a JSON value.
/// @{
/*!
...
...
@@ -2822,7 +2838,8 @@ class basic_json
Explicit pointer access to the internally stored JSON value. No copies are
made.
@warning The pointer becomes invalid if the underlying JSON object changes.
@warning The pointer becomes invalid if the underlying JSON object
changes.
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
...
...
@@ -2877,7 +2894,8 @@ class basic_json
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
@ref number_unsigned_t, or @ref number_float_t.
@ref number_unsigned_t, or @ref number_float_t. Enforced by a static
assertion.
@return pointer to the internally stored JSON value if the requested
pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
...
...
@@ -2897,6 +2915,21 @@ class basic_json
, int>::type = 0>
PointerType get_ptr() noexcept
{
// get the type of the PointerType (remove pointer and const)
using pointee_t = typename std::remove_const<typename
std::remove_pointer<typename
std::remove_const<PointerType>::type>::type>::type;
// make sure the type matches the allowed types
static_assert(
std::is_same<object_t, pointee_t>::value
or std::is_same<array_t, pointee_t>::value
or std::is_same<string_t, pointee_t>::value
or std::is_same<boolean_t, pointee_t>::value
or std::is_same<number_integer_t, pointee_t>::value
or std::is_same<number_unsigned_t, pointee_t>::value
or std::is_same<number_float_t, pointee_t>::value
, "incompatible pointer type");
// delegate the call to get_impl_ptr<>()
return get_impl_ptr(static_cast<PointerType>(nullptr));
}
...
...
@@ -2912,6 +2945,21 @@ class basic_json
, int>::type = 0>
constexpr const PointerType get_ptr() const noexcept
{
// get the type of the PointerType (remove pointer and const)
using pointee_t = typename std::remove_const<typename
std::remove_pointer<typename
std::remove_const<PointerType>::type>::type>::type;
// make sure the type matches the allowed types
static_assert(
std::is_same<object_t, pointee_t>::value
or std::is_same<array_t, pointee_t>::value
or std::is_same<string_t, pointee_t>::value
or std::is_same<boolean_t, pointee_t>::value
or std::is_same<number_integer_t, pointee_t>::value
or std::is_same<number_unsigned_t, pointee_t>::value
or std::is_same<number_float_t, pointee_t>::value
, "incompatible pointer type");
// delegate the call to get_impl_ptr<>() const
return get_impl_ptr(static_cast<const PointerType>(nullptr));
}
...
...
@@ -2927,7 +2975,7 @@ class basic_json
@tparam ReferenceType reference type; must be a reference to @ref array_t,
@ref object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, or
@ref number_float_t.
@ref number_float_t.
Enforced by static assertion.
@return reference to the internally stored JSON value if the requested
reference type @a ReferenceType fits to the JSON value; throws
...
...
@@ -3017,6 +3065,7 @@ class basic_json
////////////////////
/// @name element access
/// Access to the JSON value.
/// @{
/*!
...
...
@@ -7412,7 +7461,7 @@ class basic_json
// anything else is an error
. { return token_type::parse_error; }
*/
*/
}
/// append data from the stream to the internal buffer
...
...
@@ -7442,7 +7491,7 @@ class basic_json
}
/// return string representation of last read token
string_t get_token() const
string_t get_token
_string
() const
{
assert(m_start != nullptr);
return string_t(reinterpret_cast<typename string_t::const_pointer>(m_start),
...
...
@@ -7710,7 +7759,7 @@ class basic_json
if (type != value_t::number_float)
{
// multiply last value by ten and add the new digit
auto temp = value * 10 + *curptr -
0x30
;
auto temp = value * 10 + *curptr -
'0'
;
// test for overflow
if (temp < value || temp > max)
...
...
@@ -8003,7 +8052,8 @@ class basic_json
if (t != last_token)
{
std::string error_msg = "parse error - unexpected ";
error_msg += (last_token == lexer::token_type::parse_error ? ("'" + m_lexer.get_token() + "'") :
error_msg += (last_token == lexer::token_type::parse_error ? ("'" + m_lexer.get_token_string() +
"'") :
lexer::token_type_name(last_token));
error_msg += "; expected " + lexer::token_type_name(t);
throw std::invalid_argument(error_msg);
...
...
@@ -8015,7 +8065,8 @@ class basic_json
if (t == last_token)
{
std::string error_msg = "parse error - unexpected ";
error_msg += (last_token == lexer::token_type::parse_error ? ("'" + m_lexer.get_token() + "'") :
error_msg += (last_token == lexer::token_type::parse_error ? ("'" + m_lexer.get_token_string() +
"'") :
lexer::token_type_name(last_token));
throw std::invalid_argument(error_msg);
}
...
...
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