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
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
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
77 additions
and
42 deletions
+77
-42
README.md
README.md
+1
-1
json.h
header_only/json.h
+0
-0
json.cc
src/json.cc
+0
-0
json.h
src/json.h
+76
-41
json_unit.cc
test/json_unit.cc
+0
-0
No files found.
README.md
View file @
76be1ae1
...
@@ -184,7 +184,7 @@ bool foo = j.at(2);
...
@@ -184,7 +184,7 @@ bool foo = j.at(2);
// other stuff
// other stuff
j
.
size
();
// 3 entries
j
.
size
();
// 3 entries
j
.
empty
();
// false
j
.
empty
();
// false
j
.
type
();
// json::value_t
ype
::array
j
.
type
();
// json::value_t::array
j
.
clear
();
// the array is empty again
j
.
clear
();
// the array is empty again
// comparison
// 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 @@
...
@@ -2,7 +2,7 @@
@file
@file
@copyright The code is licensed under the MIT License
@copyright The code is licensed under the MIT License
<http://opensource.org/licenses/MIT>,
<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>
@author Niels Lohmann <http://nlohmann.me>
...
@@ -18,6 +18,7 @@
...
@@ -18,6 +18,7 @@
#include <vector> // std::vector
#include <vector> // std::vector
#include <iterator> // std::iterator
#include <iterator> // std::iterator
#include <limits> // std::numeric_limits
#include <limits> // std::numeric_limits
#include <functional> // std::hash
namespace
nlohmann
namespace
nlohmann
{
{
...
@@ -41,30 +42,21 @@ due to alignment.
...
@@ -41,30 +42,21 @@ due to alignment.
*/
*/
class
json
class
json
{
{
// forward declaration to friend this class
public
:
public
:
// forward declaration to friend this class
class
iterator
;
class
iterator
;
class
const_iterator
;
class
const_iterator
;
public
:
// container types
/// possible types of a JSON object
using
value_type
=
json
;
enum
class
value_type
:
uint8_t
using
reference
=
json
&
;
{
using
const_reference
=
const
json
&
;
/// ordered collection of values
using
pointer
=
json
*
;
array
=
0
,
using
const_pointer
=
const
json
*
;
/// unordered set of name/value pairs
using
iterator
=
json
::
iterator
;
object
,
using
const_iterator
=
json
::
const_iterator
;
/// null value
using
difference_type
=
std
::
ptrdiff_t
;
null
,
using
size_type
=
std
::
size_t
;
/// string value
string
,
/// Boolean value
boolean
,
/// number value (integer)
number
,
/// number value (float)
number_float
};
/// a type for an object
/// a type for an object
using
object_t
=
std
::
map
<
std
::
string
,
json
>
;
using
object_t
=
std
::
map
<
std
::
string
,
json
>
;
...
@@ -113,9 +105,28 @@ class json
...
@@ -113,9 +105,28 @@ class json
value
(
number_float_t
);
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
:
public
:
/// create an object according to given type
/// create an object according to given type
json
(
const
value_t
ype
);
json
(
const
value_t
);
/// create a null object
/// create a null object
json
()
=
default
;
json
()
=
default
;
/// create a null object
/// create a null object
...
@@ -140,15 +151,23 @@ class json
...
@@ -140,15 +151,23 @@ class json
json
(
list_init_t
);
json
(
list_init_t
);
/// create a number object (integer)
/// 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
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)
/// 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
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
/// create an array object
...
@@ -284,27 +303,29 @@ class json
...
@@ -284,27 +303,29 @@ class json
/// operator to get an element in an array
/// operator to get an element in an array
const
json
&
operator
[](
const
int
)
const
;
const
json
&
operator
[](
const
int
)
const
;
/// operator to get an element in an array
/// operator to get an element in an array
json
&
at
(
const
int
);
reference
at
(
const
int
);
/// operator to get an element in an array
/// 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
/// 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
/// operator to set an element in an object
json
&
operator
[](
const
char
*
);
reference
operator
[](
const
char
*
);
/// operator to get an element in an object
/// 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
/// 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
/// operator to set an element in an object
json
&
at
(
const
char
*
);
reference
at
(
const
char
*
);
/// operator to get an element in an object
/// 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
/// 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
/// 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
/// checks whether object is empty
bool
empty
()
const
noexcept
;
bool
empty
()
const
noexcept
;
/// removes all elements from compounds and resets values to default
/// removes all elements from compounds and resets values to default
...
@@ -314,7 +335,7 @@ class json
...
@@ -314,7 +335,7 @@ class json
void
swap
(
json
&
)
noexcept
;
void
swap
(
json
&
)
noexcept
;
/// return the type of the object
/// 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)
/// find an element in an object (returns end() iterator otherwise)
iterator
find
(
const
std
::
string
&
);
iterator
find
(
const
std
::
string
&
);
...
@@ -345,7 +366,7 @@ class json
...
@@ -345,7 +366,7 @@ class json
private
:
private
:
/// the type of this object
/// the type of this object
value_t
ype
type_
=
value_type
::
null
;
value_t
type_
=
value_t
::
null
;
/// the payload
/// the payload
value
value_
{};
value
value_
{};
...
@@ -475,15 +496,29 @@ class json
...
@@ -475,15 +496,29 @@ class json
/// user-defined literal operator to create JSON objects from strings
/// user-defined literal operator to create JSON objects from strings
nlohmann
::
json
operator
""
_json
(
const
char
*
,
std
::
size_t
);
nlohmann
::
json
operator
""
_json
(
const
char
*
,
std
::
size_t
);
// specialization of std::swap
// specialization of std::swap
, and std::hash
namespace
std
namespace
std
{
{
template
<>
template
<>
/// swaps the values of two JSON objects
/// swaps the values of two JSON objects
inline
void
swap
(
nlohmann
::
json
&
j1
,
inline
void
swap
(
nlohmann
::
json
&
j1
,
nlohmann
::
json
&
j2
)
noexcept
(
is_nothrow_move_constructible
<
nlohmann
::
json
>::
value
and
nlohmann
::
json
&
j2
)
noexcept
(
is_nothrow_move_assignable
<
nlohmann
::
json
>::
value
)
is_nothrow_move_constructible
<
nlohmann
::
json
>::
value
and
is_nothrow_move_assignable
<
nlohmann
::
json
>::
value
)
{
{
j1
.
swap
(
j2
);
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