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
76be1ae1
Commit
76be1ae1
authored
Jan 21, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
+ more adjustments toward a real container
parent
3c780f0f
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
632 additions
and
490 deletions
+632
-490
README.md
README.md
+1
-1
header_only/json.h
header_only/json.h
+255
-191
src/json.cc
src/json.cc
+179
-150
src/json.h
src/json.h
+76
-41
test/json_unit.cc
test/json_unit.cc
+121
-107
No files found.
README.md
View file @
76be1ae1
...
...
@@ -184,7 +184,7 @@ bool foo = j.at(2);
// other stuff
j
.
size
();
// 3 entries
j
.
empty
();
// false
j
.
type
();
// json::value_t
ype
::array
j
.
type
();
// json::value_t::array
j
.
clear
();
// the array is empty again
// comparison
...
...
header_only/json.h
View file @
76be1ae1
This diff is collapsed.
Click to expand it.
src/json.cc
View file @
76be1ae1
This diff is collapsed.
Click to expand it.
src/json.h
View file @
76be1ae1
...
...
@@ -2,7 +2,7 @@
@file
@copyright The code is licensed under the MIT License
<http://opensource.org/licenses/MIT>,
Copyright (c) 2013-201
4
Niels Lohmann.
Copyright (c) 2013-201
5
Niels Lohmann.
@author Niels Lohmann <http://nlohmann.me>
...
...
@@ -18,6 +18,7 @@
#include <vector> // std::vector
#include <iterator> // std::iterator
#include <limits> // std::numeric_limits
#include <functional> // std::hash
namespace
nlohmann
{
...
...
@@ -41,30 +42,21 @@ due to alignment.
*/
class
json
{
// forward declaration to friend this class
public:
// forward declaration to friend this class
class
iterator
;
class
const_iterator
;
public:
/// possible types of a JSON object
enum
class
value_type
:
uint8_t
{
/// ordered collection of values
array
=
0
,
/// unordered set of name/value pairs
object
,
/// null value
null
,
/// string value
string
,
/// Boolean value
boolean
,
/// number value (integer)
number
,
/// number value (float)
number_float
};
// container types
using
value_type
=
json
;
using
reference
=
json
&
;
using
const_reference
=
const
json
&
;
using
pointer
=
json
*
;
using
const_pointer
=
const
json
*
;
using
iterator
=
json
::
iterator
;
using
const_iterator
=
json
::
const_iterator
;
using
difference_type
=
std
::
ptrdiff_t
;
using
size_type
=
std
::
size_t
;
/// a type for an object
using
object_t
=
std
::
map
<
std
::
string
,
json
>
;
...
...
@@ -113,9 +105,28 @@ class json
value
(
number_float_t
);
};
/// possible types of a JSON object
enum
class
value_t
:
uint8_t
{
/// ordered collection of values
array
=
0
,
/// unordered set of name/value pairs
object
,
/// null value
null
,
/// string value
string
,
/// Boolean value
boolean
,
/// number value (integer)
number
,
/// number value (float)
number_float
};
public:
/// create an object according to given type
json
(
const
value_t
ype
);
json
(
const
value_t
);
/// create a null object
json
()
=
default
;
/// create a null object
...
...
@@ -140,15 +151,23 @@ class json
json
(
list_init_t
);
/// create a number object (integer)
template
<
typename
T
,
typename
std
::
enable_if
<
std
::
numeric_limits
<
T
>
::
is_integer
,
T
>::
type
=
0
>
template
<
typename
T
,
typename
std
::
enable_if
<
std
::
numeric_limits
<
T
>
::
is_integer
,
T
>::
type
=
0
>
json
(
const
T
n
)
noexcept
:
type_
(
value_type
::
number
),
value_
(
static_cast
<
number_t
>
(
n
))
:
type_
(
value_t
::
number
),
value_
(
static_cast
<
number_t
>
(
n
))
{}
/// create a number object (float)
template
<
typename
T
,
typename
=
typename
std
::
enable_if
<
std
::
is_floating_point
<
T
>
::
value
>::
type
>
template
<
typename
T
,
typename
=
typename
std
::
enable_if
<
std
::
is_floating_point
<
T
>
::
value
>::
type
>
json
(
const
T
n
)
noexcept
:
type_
(
value_type
::
number_float
),
value_
(
static_cast
<
number_float_t
>
(
n
))
:
type_
(
value_t
::
number_float
),
value_
(
static_cast
<
number_float_t
>
(
n
))
{}
/// create an array object
...
...
@@ -284,27 +303,29 @@ class json
/// operator to get an element in an array
const
json
&
operator
[](
const
int
)
const
;
/// operator to get an element in an array
json
&
at
(
const
int
);
reference
at
(
const
int
);
/// operator to get an element in an array
const
json
&
at
(
const
int
)
const
;
const
_reference
at
(
const
int
)
const
;
/// operator to set an element in an object
json
&
operator
[](
const
std
::
string
&
);
reference
operator
[](
const
std
::
string
&
);
/// operator to set an element in an object
json
&
operator
[](
const
char
*
);
reference
operator
[](
const
char
*
);
/// operator to get an element in an object
const
json
&
operator
[](
const
std
::
string
&
)
const
;
const
_reference
operator
[](
const
std
::
string
&
)
const
;
/// operator to set an element in an object
json
&
at
(
const
std
::
string
&
);
reference
at
(
const
std
::
string
&
);
/// operator to set an element in an object
json
&
at
(
const
char
*
);
reference
at
(
const
char
*
);
/// operator to get an element in an object
const
json
&
at
(
const
std
::
string
&
)
const
;
const
_reference
at
(
const
std
::
string
&
)
const
;
/// operator to get an element in an object
const
json
&
at
(
const
char
*
)
const
;
const
_reference
at
(
const
char
*
)
const
;
/// return the number of stored values
std
::
size_t
size
()
const
noexcept
;
size_type
size
()
const
noexcept
;
/// return the maximal number of values that can be stored
size_type
max_size
()
const
noexcept
;
/// checks whether object is empty
bool
empty
()
const
noexcept
;
/// removes all elements from compounds and resets values to default
...
...
@@ -314,7 +335,7 @@ class json
void
swap
(
json
&
)
noexcept
;
/// return the type of the object
value_t
ype
type
()
const
noexcept
;
value_t
type
()
const
noexcept
;
/// find an element in an object (returns end() iterator otherwise)
iterator
find
(
const
std
::
string
&
);
...
...
@@ -345,7 +366,7 @@ class json
private:
/// the type of this object
value_t
ype
type_
=
value_type
::
null
;
value_t
type_
=
value_t
::
null
;
/// the payload
value
value_
{};
...
...
@@ -475,15 +496,29 @@ class json
/// user-defined literal operator to create JSON objects from strings
nlohmann
::
json
operator
""
_json
(
const
char
*
,
std
::
size_t
);
// specialization of std::swap
// specialization of std::swap
, and std::hash
namespace
std
{
template
<
>
/// swaps the values of two JSON objects
inline
void
swap
(
nlohmann
::
json
&
j1
,
nlohmann
::
json
&
j2
)
noexcept
(
is_nothrow_move_constructible
<
nlohmann
::
json
>::
value
and
is_nothrow_move_assignable
<
nlohmann
::
json
>::
value
)
nlohmann
::
json
&
j2
)
noexcept
(
is_nothrow_move_constructible
<
nlohmann
::
json
>::
value
and
is_nothrow_move_assignable
<
nlohmann
::
json
>::
value
)
{
j1
.
swap
(
j2
);
}
template
<
>
/// hash value for JSON objects
struct
hash
<
nlohmann
::
json
>
{
size_t
operator
()(
const
nlohmann
::
json
&
j
)
const
{
// a naive hashing via the string representation
return
hash
<
std
::
string
>
()(
j
.
dump
());
}
};
}
test/json_unit.cc
View file @
76be1ae1
This diff is collapsed.
Click to expand it.
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