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