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
cb2da141
Commit
cb2da141
authored
Jul 05, 2013
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- further adjustments
parent
6abf140b
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
292 additions
and
190 deletions
+292
-190
README.md
README.md
+0
-1
src/JSON.cc
src/JSON.cc
+229
-166
src/JSON.h
src/JSON.h
+38
-18
test/JSON_test.cc
test/JSON_test.cc
+25
-5
No files found.
README.md
View file @
cb2da141
...
...
@@ -55,4 +55,3 @@ j["list"] = { 1, 0, 2 };
## Input / Output
## STL-like access
src/JSON.cc
View file @
cb2da141
This diff is collapsed.
Click to expand it.
src/JSON.h
View file @
cb2da141
...
...
@@ -22,7 +22,7 @@
#endif
class
JSON
{
// forward declaration to friend this class
// forward declaration to friend this class
public:
class
iterator
;
class
const_iterator
;
...
...
@@ -36,7 +36,7 @@ class JSON {
public:
/// possible types of a JSON object
typedef
enum
{
array
,
object
,
null
,
string
,
boolean
,
number
_int
,
number_float
array
,
object
,
null
,
string
,
boolean
,
number
,
number_float
}
json_t
;
private:
...
...
@@ -47,16 +47,21 @@ class JSON {
void
*
_payload
;
public:
/// a type for an object
typedef
std
::
map
<
std
::
string
,
JSON
>
object_t
;
/// a type for an array
typedef
std
::
vector
<
JSON
>
array_t
;
#ifdef __cplusplus11
/// a type for objects
typedef
std
::
tuple
<
std
::
string
,
JSON
>
object_t
;
/// a type for arrays
typedef
std
::
initializer_list
<
JSON
>
array_t
;
/// a type for array initialization
typedef
std
::
initializer_list
<
JSON
>
array_init_t
;
#endif
public:
/// create an empty (null) object
JSON
();
/// create an empty object according to given type
JSON
(
json_t
);
/// create a string object from C++ string
JSON
(
const
std
::
string
&
);
/// create a string object from C string
...
...
@@ -69,11 +74,13 @@ class JSON {
JSON
(
const
int
);
/// create a number object
JSON
(
const
double
);
#ifdef __cplusplus11
/// create from an initializer list (to an array)
/// create an array
JSON
(
array_t
);
/// create
from a mapping (to an object)
/// create
an object
JSON
(
object_t
);
#ifdef __cplusplus11
/// create from an initializer list (to an array)
JSON
(
array_init_t
);
#endif
/// copy constructor
...
...
@@ -143,6 +150,19 @@ class JSON {
/// add a number to an array
JSON
&
operator
+=
(
double
);
/// add an object/array to an array
void
push_back
(
const
JSON
&
);
/// add a string to an array
void
push_back
(
const
std
::
string
&
);
/// add a string to an array
void
push_back
(
const
char
*
);
/// add a Boolean to an array
void
push_back
(
bool
);
/// add a number to an array
void
push_back
(
int
);
/// add a number to an array
void
push_back
(
double
);
/// operator to set an element in an array
JSON
&
operator
[](
int
);
/// operator to get an element in an array
...
...
@@ -166,8 +186,8 @@ class JSON {
/// find an element in an object (returns end() iterator otherwise)
iterator
find
(
const
std
::
string
&
);
const_iterator
find
(
const
std
::
string
&
)
const
;
iterator
find
(
const
char
*
);
const_iterator
find
(
const
char
*
)
const
;
iterator
find
(
const
char
*
);
const_iterator
find
(
const
char
*
)
const
;
/// direct access to the underlying payload
void
*
data
();
...
...
@@ -184,8 +204,8 @@ class JSON {
public:
/// an iterator
class
iterator
{
friend
class
JSON
;
friend
class
JSON
::
const_iterator
;
friend
class
JSON
;
friend
class
JSON
::
const_iterator
;
public:
iterator
();
iterator
(
JSON
*
);
...
...
@@ -208,14 +228,14 @@ class JSON {
/// a JSON value
JSON
*
_object
;
/// an iterator for JSON arrays
std
::
vector
<
JSON
>
::
iterator
*
_vi
;
array_t
::
iterator
*
_vi
;
/// an iterator for JSON objects
std
::
map
<
std
::
string
,
JSON
>
::
iterator
*
_oi
;
object_t
::
iterator
*
_oi
;
};
/// a const iterator
class
const_iterator
{
friend
class
JSON
;
friend
class
JSON
;
public:
const_iterator
();
const_iterator
(
const
JSON
*
);
...
...
@@ -239,9 +259,9 @@ class JSON {
/// a JSON value
const
JSON
*
_object
;
/// an iterator for JSON arrays
std
::
vector
<
JSON
>
::
const_iterator
*
_vi
;
array_t
::
const_iterator
*
_vi
;
/// an iterator for JSON objects
std
::
map
<
std
::
string
,
JSON
>
::
const_iterator
*
_oi
;
object_t
::
const_iterator
*
_oi
;
};
public:
...
...
test/JSON_test.cc
View file @
cb2da141
...
...
@@ -74,7 +74,6 @@ void test_null() {
}
}
void
test_bool
()
{
JSON
True
=
true
;
JSON
False
=
false
;
...
...
@@ -212,14 +211,12 @@ void test_array() {
}
{
/*
size_t
count
=
0
;
for
(
JSON
::
const_iterator
i
=
a
.
begin
();
i
!=
a
.
end
();
++
i
)
{
std
::
cerr
<<
*
i
<<
'\n'
;
count
++
;
}
assert
(
count
==
a
.
size
());
*/
}
{
...
...
@@ -253,7 +250,6 @@ void test_array() {
}
{
/*
JSON
::
const_iterator
i
;
size_t
count
=
0
;
for
(
i
=
a
.
begin
();
i
!=
a
.
end
();
++
i
)
{
...
...
@@ -261,7 +257,6 @@ void test_array() {
count
++
;
}
assert
(
count
==
a
.
size
());
*/
}
{
...
...
@@ -282,6 +277,30 @@ void test_array() {
}
}
void
test_object
()
{
// check find()
{
JSON
o
;
o
[
"foo"
]
=
"bar"
;
JSON
::
iterator
i1
=
o
.
find
(
"foo"
);
assert
(
i1
!=
o
.
end
());
assert
(
i1
.
value
()
==
"bar"
);
assert
(
i1
.
key
()
==
"foo"
);
assert
(
*
i1
==
"bar"
);
JSON
::
iterator
i2
=
o
.
find
(
"baz"
);
assert
(
i2
==
o
.
end
());
JSON
a
;
a
+=
"foo"
;
a
+=
"bar"
;
JSON
::
iterator
i
;
i
=
a
.
find
(
"foo"
);
assert
(
i
==
a
.
end
());
}
}
void
test_streaming
()
{
// stream text representation into stream
std
::
stringstream
i
;
...
...
@@ -335,6 +354,7 @@ int main() {
test_bool
();
test_string
();
test_array
();
test_object
();
test_streaming
();
return
0
;
...
...
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