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
a53c878c
Commit
a53c878c
authored
Jan 06, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
moved class into "nlohmann" namespace
- fixed issue #9 - also removed std::mutex member variable - also added “std::” prefix to size_t variables
parent
4f0afbbe
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
26 additions
and
36 deletions
+26
-36
README.md
README.md
+3
-0
parse.cc
benchmark/parse.cc
+1
-1
json.cc
src/json.cc
+12
-27
json.h
src/json.h
+8
-8
json_unit.cc
test/json_unit.cc
+2
-0
No files found.
README.md
View file @
a53c878c
...
...
@@ -30,6 +30,9 @@ All you need to do is add
```
cpp
#include "json.h"
// for convenience
using
json
=
nlohmann
::
json
;
```
to the files you want to use JSON objects. Furthermore, you need to compile the file
`json.cc`
and link it to your binaries. Do not forget to set the necessary switches to enable C++11 (e.g.,
`-std=c++11`
for GCC and Clang).
...
...
benchmark/parse.cc
View file @
a53c878c
...
...
@@ -2,7 +2,7 @@
int
main
()
{
json
j
;
nlohmann
::
json
j
;
j
<<
std
::
cin
;
return
0
;
}
src/json.cc
View file @
a53c878c
...
...
@@ -12,17 +12,12 @@
#include "json.h"
#include <cctype> // std::isdigit, std::isspace
#include <cstddef> // size_t
#include <cstddef> // s
td::s
ize_t
#include <stdexcept> // std::runtime_error
#include <utility> // std::swap, std::move
////////////////////
// STATIC MEMBERS //
////////////////////
std
::
mutex
json
::
token_
;
namespace
nlohmann
{
///////////////////////////////////
// CONSTRUCTORS OF UNION "value" //
...
...
@@ -639,8 +634,6 @@ void json::push_back(const json& o)
throw
std
::
runtime_error
(
"cannot add element to "
+
type_name
());
}
std
::
lock_guard
<
std
::
mutex
>
lg
(
token_
);
// transform null object into an array
if
(
type_
==
value_type
::
null
)
{
...
...
@@ -676,8 +669,6 @@ void json::push_back(json&& o)
throw
std
::
runtime_error
(
"cannot add element to "
+
type_name
());
}
std
::
lock_guard
<
std
::
mutex
>
lg
(
token_
);
// transform null object into an array
if
(
type_
==
value_type
::
null
)
{
...
...
@@ -804,10 +795,8 @@ json& json::operator[](const int index)
std
::
to_string
(
index
)
+
" to "
+
type_name
());
}
std
::
lock_guard
<
std
::
mutex
>
lg
(
token_
);
// return reference to element from array at given index
return
(
*
value_
.
array
)[
static_cast
<
size_t
>
(
index
)];
return
(
*
value_
.
array
)[
static_cast
<
s
td
::
s
ize_t
>
(
index
)];
}
/*!
...
...
@@ -836,7 +825,7 @@ const json& json::operator[](const int index) const
}
// return element from array at given index
return
(
*
value_
.
array
)[
static_cast
<
size_t
>
(
index
)];
return
(
*
value_
.
array
)[
static_cast
<
s
td
::
s
ize_t
>
(
index
)];
}
/*!
...
...
@@ -865,10 +854,8 @@ json& json::at(const int index)
std
::
to_string
(
index
)
+
" to "
+
type_name
());
}
std
::
lock_guard
<
std
::
mutex
>
lg
(
token_
);
// return reference to element from array at given index
return
value_
.
array
->
at
(
static_cast
<
size_t
>
(
index
));
return
value_
.
array
->
at
(
static_cast
<
s
td
::
s
ize_t
>
(
index
));
}
/*!
...
...
@@ -898,7 +885,7 @@ const json& json::at(const int index) const
}
// return element from array at given index
return
value_
.
array
->
at
(
static_cast
<
size_t
>
(
index
));
return
value_
.
array
->
at
(
static_cast
<
s
td
::
s
ize_t
>
(
index
));
}
/*!
...
...
@@ -924,8 +911,6 @@ key.
*/
json
&
json
::
operator
[](
const
char
*
key
)
{
std
::
lock_guard
<
std
::
mutex
>
lg
(
token_
);
// implicitly convert null to object
if
(
type_
==
value_type
::
null
)
{
...
...
@@ -1006,8 +991,6 @@ key.
*/
json
&
json
::
at
(
const
char
*
key
)
{
std
::
lock_guard
<
std
::
mutex
>
lg
(
token_
);
// this function operator only works for objects
if
(
type_
!=
value_type
::
object
)
{
...
...
@@ -1061,7 +1044,7 @@ Returns the size of the JSON object.
@invariant The size is reported as 0 if and only if empty() would return true.
*/
size_t
json
::
size
()
const
noexcept
s
td
::
s
ize_t
json
::
size
()
const
noexcept
{
switch
(
type_
)
{
...
...
@@ -2105,6 +2088,8 @@ void json::parser::expect(const char c)
}
}
}
/*!
This operator implements a user-defined string literal for JSON objects. It can
be used by adding \p "_json" to a string literal and returns a JSON object if
...
...
@@ -2113,7 +2098,7 @@ no parse error occurred.
@param s a string representation of a JSON object
@return a JSON object
*/
json
operator
""
_json
(
const
char
*
s
,
size_t
)
nlohmann
::
json
operator
""
_json
(
const
char
*
s
,
std
::
size_t
)
{
return
json
::
parse
(
s
);
return
nlohmann
::
json
::
parse
(
s
);
}
src/json.h
View file @
a53c878c
...
...
@@ -14,10 +14,12 @@
#include <initializer_list> // std::initializer_list
#include <iostream> // std::istream, std::ostream
#include <map> // std::map
#include <mutex> // std::mutex
#include <string> // std::string
#include <vector> // std::vector
namespace
nlohmann
{
/*!
@brief JSON for Modern C++
...
...
@@ -274,7 +276,7 @@ class json
const
json
&
at
(
const
char
*
)
const
;
/// return the number of stored values
size_t
size
()
const
noexcept
;
s
td
::
s
ize_t
size
()
const
noexcept
;
/// checks whether object is empty
bool
empty
()
const
noexcept
;
/// removes all elements from compounds and resets values to default
...
...
@@ -317,10 +319,6 @@ class json
/// the payload
value
value_
{};
private
:
/// mutex to guard payload
static
std
::
mutex
token_
;
public
:
/// an iterator
class
iterator
...
...
@@ -431,9 +429,11 @@ class json
/// the current character
char
current_
{};
/// the position inside the input buffer
size_t
pos_
=
0
;
s
td
::
s
ize_t
pos_
=
0
;
};
};
}
/// user-defined literal operator to create JSON objects from strings
json
operator
""
_json
(
const
char
*
,
size_t
);
nlohmann
::
json
operator
""
_json
(
const
char
*
,
std
::
size_t
);
test/json_unit.cc
View file @
a53c878c
...
...
@@ -3,6 +3,8 @@
#include "json.h"
using
json
=
nlohmann
::
json
;
TEST_CASE
(
"array"
)
{
SECTION
(
"Basics"
)
...
...
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