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
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
225 additions
and
123 deletions
+225
-123
README.md
README.md
+0
-1
JSON.cc
src/JSON.cc
+166
-103
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
...
@@ -35,45 +35,83 @@ std::mutex JSON::_token;
...
@@ -35,45 +35,83 @@ std::mutex JSON::_token;
*******************************/
*******************************/
JSON
::
JSON
()
:
_type
(
null
),
_payload
(
nullptr
)
{}
JSON
::
JSON
()
:
_type
(
null
),
_payload
(
nullptr
)
{}
JSON
::
JSON
(
json_t
type
)
:
_type
(
type
)
{
switch
(
_type
)
{
case
(
array
):
{
_payload
=
new
array_t
();
break
;
}
case
(
object
):
{
_payload
=
new
object_t
();
break
;
}
case
(
string
):
{
_payload
=
new
std
::
string
();
break
;
}
case
(
boolean
):
{
_payload
=
new
bool
();
break
;
}
case
(
number
):
{
_payload
=
new
int
(
0
);
break
;
}
case
(
number_float
):
{
_payload
=
new
double
(
0.0
);
break
;
}
case
(
null
):
{
break
;
}
}
}
JSON
::
JSON
(
const
std
::
string
&
s
)
:
_type
(
string
),
_payload
(
new
std
::
string
(
s
))
{}
JSON
::
JSON
(
const
std
::
string
&
s
)
:
_type
(
string
),
_payload
(
new
std
::
string
(
s
))
{}
JSON
::
JSON
(
const
char
*
s
)
:
_type
(
string
),
_payload
(
new
std
::
string
(
s
))
{}
JSON
::
JSON
(
const
char
*
s
)
:
_type
(
string
),
_payload
(
new
std
::
string
(
s
))
{}
JSON
::
JSON
(
char
*
s
)
:
_type
(
string
),
_payload
(
new
std
::
string
(
s
))
{}
JSON
::
JSON
(
char
*
s
)
:
_type
(
string
),
_payload
(
new
std
::
string
(
s
))
{}
JSON
::
JSON
(
const
bool
b
)
:
_type
(
boolean
),
_payload
(
new
bool
(
b
))
{}
JSON
::
JSON
(
const
bool
b
)
:
_type
(
boolean
),
_payload
(
new
bool
(
b
))
{}
JSON
::
JSON
(
const
int
i
)
:
_type
(
number
_int
),
_payload
(
new
int
(
i
))
{}
JSON
::
JSON
(
const
int
i
)
:
_type
(
number
),
_payload
(
new
int
(
i
))
{}
JSON
::
JSON
(
const
double
f
)
:
_type
(
number_float
),
_payload
(
new
double
(
f
))
{}
JSON
::
JSON
(
const
double
f
)
:
_type
(
number_float
),
_payload
(
new
double
(
f
))
{}
JSON
::
JSON
(
array_t
a
)
:
_type
(
array
),
_payload
(
new
array_t
(
a
))
{}
JSON
::
JSON
(
object_t
o
)
:
_type
(
object
),
_payload
(
new
object_t
(
o
))
{}
#ifdef __cplusplus11
#ifdef __cplusplus11
JSON
::
JSON
(
array_t
a
)
:
_type
(
array
),
_payload
(
new
std
::
vector
<
JSON
>
(
a
))
{}
JSON
::
JSON
(
array_init_t
a
)
:
_type
(
array
),
_payload
(
new
array_t
(
a
))
{}
JSON
::
JSON
(
object_t
o
)
:
_type
(
object
),
_payload
(
new
std
::
map
<
std
::
string
,
JSON
>
)
{
(
*
this
)[
std
::
get
<
0
>
(
o
)]
=
std
::
get
<
1
>
(
o
);
}
#endif
#endif
/// copy constructor
/// copy constructor
JSON
::
JSON
(
const
JSON
&
o
)
:
_type
(
o
.
_type
)
{
JSON
::
JSON
(
const
JSON
&
o
)
:
_type
(
o
.
_type
)
{
switch
(
_type
)
{
switch
(
_type
)
{
case
(
array
):
case
(
array
):
{
_payload
=
new
std
::
vector
<
JSON
>
(
*
static_cast
<
std
::
vector
<
JSON
>
*>
(
o
.
_payload
));
_payload
=
new
array_t
(
*
static_cast
<
array_t
*>
(
o
.
_payload
));
break
;
break
;
case
(
object
):
}
_payload
=
new
std
::
map
<
std
::
string
,
JSON
>
(
*
static_cast
<
std
::
map
<
std
::
string
,
JSON
>*>
(
o
.
_payload
));
case
(
object
):
{
_payload
=
new
object_t
(
*
static_cast
<
object_t
*>
(
o
.
_payload
));
break
;
break
;
case
(
string
):
}
case
(
string
):
{
_payload
=
new
std
::
string
(
*
static_cast
<
std
::
string
*>
(
o
.
_payload
));
_payload
=
new
std
::
string
(
*
static_cast
<
std
::
string
*>
(
o
.
_payload
));
break
;
break
;
case
(
boolean
):
}
case
(
boolean
):
{
_payload
=
new
bool
(
*
static_cast
<
bool
*>
(
o
.
_payload
));
_payload
=
new
bool
(
*
static_cast
<
bool
*>
(
o
.
_payload
));
break
;
break
;
case
(
number_int
):
}
case
(
number
):
{
_payload
=
new
int
(
*
static_cast
<
int
*>
(
o
.
_payload
));
_payload
=
new
int
(
*
static_cast
<
int
*>
(
o
.
_payload
));
break
;
break
;
case
(
number_float
):
}
case
(
number_float
):
{
_payload
=
new
double
(
*
static_cast
<
double
*>
(
o
.
_payload
));
_payload
=
new
double
(
*
static_cast
<
double
*>
(
o
.
_payload
));
break
;
break
;
case
(
null
):
}
case
(
null
):
{
break
;
break
;
}
}
}
}
}
#ifdef __cplusplus11
#ifdef __cplusplus11
...
@@ -97,11 +135,11 @@ JSON& JSON::operator=(const JSON& o) {
...
@@ -97,11 +135,11 @@ JSON& JSON::operator=(const JSON& o) {
switch
(
_type
)
{
switch
(
_type
)
{
case
(
array
):
{
case
(
array
):
{
delete
static_cast
<
std
::
vector
<
JSON
>
*>
(
_payload
);
delete
static_cast
<
array_t
*>
(
_payload
);
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
delete
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_payload
);
delete
static_cast
<
object_t
*>
(
_payload
);
break
;
break
;
}
}
case
(
string
):
{
case
(
string
):
{
...
@@ -112,7 +150,7 @@ JSON& JSON::operator=(const JSON& o) {
...
@@ -112,7 +150,7 @@ JSON& JSON::operator=(const JSON& o) {
delete
static_cast
<
bool
*>
(
_payload
);
delete
static_cast
<
bool
*>
(
_payload
);
break
;
break
;
}
}
case
(
number
_int
):
{
case
(
number
):
{
delete
static_cast
<
int
*>
(
_payload
);
delete
static_cast
<
int
*>
(
_payload
);
break
;
break
;
}
}
...
@@ -128,11 +166,11 @@ JSON& JSON::operator=(const JSON& o) {
...
@@ -128,11 +166,11 @@ JSON& JSON::operator=(const JSON& o) {
_type
=
o
.
_type
;
_type
=
o
.
_type
;
switch
(
_type
)
{
switch
(
_type
)
{
case
(
array
):
{
case
(
array
):
{
_payload
=
new
std
::
vector
<
JSON
>
(
*
static_cast
<
std
::
vector
<
JSON
>
*>
(
o
.
_payload
));
_payload
=
new
array_t
(
*
static_cast
<
array_t
*>
(
o
.
_payload
));
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
_payload
=
new
std
::
map
<
std
::
string
,
JSON
>
(
*
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
o
.
_payload
));
_payload
=
new
object_t
(
*
static_cast
<
object_t
*>
(
o
.
_payload
));
break
;
break
;
}
}
case
(
string
):
{
case
(
string
):
{
...
@@ -143,7 +181,7 @@ JSON& JSON::operator=(const JSON& o) {
...
@@ -143,7 +181,7 @@ JSON& JSON::operator=(const JSON& o) {
_payload
=
new
bool
(
*
static_cast
<
bool
*>
(
o
.
_payload
));
_payload
=
new
bool
(
*
static_cast
<
bool
*>
(
o
.
_payload
));
break
;
break
;
}
}
case
(
number
_int
):
{
case
(
number
):
{
_payload
=
new
int
(
*
static_cast
<
int
*>
(
o
.
_payload
));
_payload
=
new
int
(
*
static_cast
<
int
*>
(
o
.
_payload
));
break
;
break
;
}
}
...
@@ -164,11 +202,11 @@ JSON& JSON::operator=(const JSON& o) {
...
@@ -164,11 +202,11 @@ JSON& JSON::operator=(const JSON& o) {
JSON
::~
JSON
()
{
JSON
::~
JSON
()
{
switch
(
_type
)
{
switch
(
_type
)
{
case
(
array
):
{
case
(
array
):
{
delete
static_cast
<
std
::
vector
<
JSON
>
*>
(
_payload
);
delete
static_cast
<
array_t
*>
(
_payload
);
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
delete
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_payload
);
delete
static_cast
<
object_t
*>
(
_payload
);
break
;
break
;
}
}
case
(
string
):
{
case
(
string
):
{
...
@@ -179,7 +217,7 @@ JSON::~JSON() {
...
@@ -179,7 +217,7 @@ JSON::~JSON() {
delete
static_cast
<
bool
*>
(
_payload
);
delete
static_cast
<
bool
*>
(
_payload
);
break
;
break
;
}
}
case
(
number
_int
):
{
case
(
number
):
{
delete
static_cast
<
int
*>
(
_payload
);
delete
static_cast
<
int
*>
(
_payload
);
break
;
break
;
}
}
...
@@ -210,7 +248,7 @@ JSON::operator const std::string() const {
...
@@ -210,7 +248,7 @@ JSON::operator const std::string() const {
JSON
::
operator
int
()
const
{
JSON
::
operator
int
()
const
{
switch
(
_type
)
{
switch
(
_type
)
{
case
(
number
_int
):
case
(
number
):
return
*
static_cast
<
int
*>
(
_payload
);
return
*
static_cast
<
int
*>
(
_payload
);
case
(
number_float
):
case
(
number_float
):
return
static_cast
<
int
>
(
*
static_cast
<
double
*>
(
_payload
));
return
static_cast
<
int
>
(
*
static_cast
<
double
*>
(
_payload
));
...
@@ -221,7 +259,7 @@ JSON::operator int() const {
...
@@ -221,7 +259,7 @@ JSON::operator int() const {
JSON
::
operator
double
()
const
{
JSON
::
operator
double
()
const
{
switch
(
_type
)
{
switch
(
_type
)
{
case
(
number
_int
):
case
(
number
):
return
static_cast
<
double
>
(
*
static_cast
<
int
*>
(
_payload
));
return
static_cast
<
double
>
(
*
static_cast
<
int
*>
(
_payload
));
case
(
number_float
):
case
(
number_float
):
return
*
static_cast
<
double
*>
(
_payload
);
return
*
static_cast
<
double
*>
(
_payload
);
...
@@ -253,7 +291,7 @@ const std::string JSON::toString() const {
...
@@ -253,7 +291,7 @@ const std::string JSON::toString() const {
return
*
static_cast
<
bool
*>
(
_payload
)
?
"true"
:
"false"
;
return
*
static_cast
<
bool
*>
(
_payload
)
?
"true"
:
"false"
;
}
}
case
(
number
_int
):
{
case
(
number
):
{
return
to_string
(
*
static_cast
<
int
*>
(
_payload
));
return
to_string
(
*
static_cast
<
int
*>
(
_payload
));
}
}
...
@@ -264,8 +302,8 @@ const std::string JSON::toString() const {
...
@@ -264,8 +302,8 @@ const std::string JSON::toString() const {
case
(
array
):
{
case
(
array
):
{
std
::
string
result
;
std
::
string
result
;
const
std
::
vector
<
JSON
>*
array
=
static_cast
<
std
::
vector
<
JSON
>
*>
(
_payload
);
const
array_t
*
array
=
static_cast
<
array_t
*>
(
_payload
);
for
(
std
::
vector
<
JSON
>
::
const_iterator
i
=
array
->
begin
();
i
!=
array
->
end
();
++
i
)
{
for
(
array_t
::
const_iterator
i
=
array
->
begin
();
i
!=
array
->
end
();
++
i
)
{
if
(
i
!=
array
->
begin
())
{
if
(
i
!=
array
->
begin
())
{
result
+=
", "
;
result
+=
", "
;
}
}
...
@@ -278,8 +316,8 @@ const std::string JSON::toString() const {
...
@@ -278,8 +316,8 @@ const std::string JSON::toString() const {
case
(
object
):
{
case
(
object
):
{
std
::
string
result
;
std
::
string
result
;
const
std
::
map
<
std
::
string
,
JSON
>*
object
=
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_payload
);
const
object_t
*
object
=
static_cast
<
object_t
*>
(
_payload
);
for
(
std
::
map
<
std
::
string
,
JSON
>
::
const_iterator
i
=
object
->
begin
();
i
!=
object
->
end
();
++
i
)
{
for
(
object_t
::
const_iterator
i
=
object
->
begin
();
i
!=
object
->
end
();
++
i
)
{
if
(
i
!=
object
->
begin
())
{
if
(
i
!=
object
->
begin
())
{
result
+=
", "
;
result
+=
", "
;
}
}
...
@@ -297,6 +335,36 @@ const std::string JSON::toString() const {
...
@@ -297,6 +335,36 @@ const std::string JSON::toString() const {
*****************************************/
*****************************************/
JSON
&
JSON
::
operator
+=
(
const
JSON
&
o
)
{
JSON
&
JSON
::
operator
+=
(
const
JSON
&
o
)
{
push_back
(
o
);
return
*
this
;
}
JSON
&
JSON
::
operator
+=
(
const
std
::
string
&
s
)
{
push_back
(
JSON
(
s
));
return
*
this
;
}
JSON
&
JSON
::
operator
+=
(
const
char
*
s
)
{
push_back
(
JSON
(
s
));
return
*
this
;
}
JSON
&
JSON
::
operator
+=
(
bool
b
)
{
push_back
(
JSON
(
b
));
return
*
this
;
}
JSON
&
JSON
::
operator
+=
(
int
i
)
{
push_back
(
JSON
(
i
));
return
*
this
;
}
JSON
&
JSON
::
operator
+=
(
double
f
)
{
push_back
(
JSON
(
f
));
return
*
this
;
}
void
JSON
::
push_back
(
const
JSON
&
o
)
{
#ifdef __cplusplus11
#ifdef __cplusplus11
std
::
lock_guard
<
std
::
mutex
>
lg
(
_token
);
std
::
lock_guard
<
std
::
mutex
>
lg
(
_token
);
#endif
#endif
...
@@ -307,42 +375,30 @@ JSON& JSON::operator+=(const JSON& o) {
...
@@ -307,42 +375,30 @@ JSON& JSON::operator+=(const JSON& o) {
if
(
_type
==
null
)
{
if
(
_type
==
null
)
{
_type
=
array
;
_type
=
array
;
_payload
=
new
std
::
vector
<
JSON
>
;
_payload
=
new
array_t
;
}
}
static_cast
<
std
::
vector
<
JSON
>*>
(
_payload
)
->
push_back
(
o
);
static_cast
<
array_t
*>
(
_payload
)
->
push_back
(
o
);
return
*
this
;
}
}
JSON
&
JSON
::
operator
+=
(
const
std
::
string
&
s
)
{
void
JSON
::
push_back
(
const
std
::
string
&
s
)
{
JSON
tmp
(
s
);
push_back
(
JSON
(
s
));
operator
+=
(
tmp
);
return
*
this
;
}
}
JSON
&
JSON
::
operator
+=
(
const
char
*
s
)
{
void
JSON
::
push_back
(
const
char
*
s
)
{
JSON
tmp
(
s
);
push_back
(
JSON
(
s
));
operator
+=
(
tmp
);
return
*
this
;
}
}
JSON
&
JSON
::
operator
+=
(
bool
b
)
{
void
JSON
::
push_back
(
bool
b
)
{
JSON
tmp
(
b
);
push_back
(
JSON
(
b
));
operator
+=
(
tmp
);
return
*
this
;
}
}
JSON
&
JSON
::
operator
+=
(
int
i
)
{
void
JSON
::
push_back
(
int
i
)
{
JSON
tmp
(
i
);
push_back
(
JSON
(
i
));
operator
+=
(
tmp
);
return
*
this
;
}
}
JSON
&
JSON
::
operator
+=
(
double
f
)
{
void
JSON
::
push_back
(
double
f
)
{
JSON
tmp
(
f
);
push_back
(
JSON
(
f
));
operator
+=
(
tmp
);
return
*
this
;
}
}
/// operator to set an element in an object
/// operator to set an element in an object
...
@@ -355,7 +411,7 @@ JSON& JSON::operator[](int index) {
...
@@ -355,7 +411,7 @@ JSON& JSON::operator[](int index) {
throw
std
::
runtime_error
(
"cannot add entry with index "
+
to_string
(
index
)
+
" to "
+
_typename
());
throw
std
::
runtime_error
(
"cannot add entry with index "
+
to_string
(
index
)
+
" to "
+
_typename
());
}
}
std
::
vector
<
JSON
>*
array
=
static_cast
<
std
::
vector
<
JSON
>
*>
(
_payload
);
array_t
*
array
=
static_cast
<
array_t
*>
(
_payload
);
if
(
index
>=
array
->
size
())
{
if
(
index
>=
array
->
size
())
{
throw
std
::
runtime_error
(
"cannot access element at index "
+
to_string
(
index
));
throw
std
::
runtime_error
(
"cannot access element at index "
+
to_string
(
index
));
...
@@ -370,7 +426,7 @@ const JSON& JSON::operator[](const int index) const {
...
@@ -370,7 +426,7 @@ const JSON& JSON::operator[](const int index) const {
throw
std
::
runtime_error
(
"cannot get entry with index "
+
to_string
(
index
)
+
" from "
+
_typename
());
throw
std
::
runtime_error
(
"cannot get entry with index "
+
to_string
(
index
)
+
" from "
+
_typename
());
}
}
std
::
vector
<
JSON
>*
array
=
static_cast
<
std
::
vector
<
JSON
>
*>
(
_payload
);
array_t
*
array
=
static_cast
<
array_t
*>
(
_payload
);
if
(
index
>=
array
->
size
())
{
if
(
index
>=
array
->
size
())
{
throw
std
::
runtime_error
(
"cannot access element at index "
+
to_string
(
index
));
throw
std
::
runtime_error
(
"cannot access element at index "
+
to_string
(
index
));
...
@@ -387,14 +443,14 @@ JSON& JSON::operator[](const std::string& key) {
...
@@ -387,14 +443,14 @@ JSON& JSON::operator[](const std::string& key) {
if
(
_type
==
null
)
{
if
(
_type
==
null
)
{
_type
=
object
;
_type
=
object
;
_payload
=
new
std
::
map
<
std
::
string
,
JSON
>
;
_payload
=
new
object_t
;
}
}
if
(
_type
!=
object
)
{
if
(
_type
!=
object
)
{
throw
std
::
runtime_error
(
"cannot add entry with key "
+
std
::
string
(
key
)
+
" to "
+
_typename
());
throw
std
::
runtime_error
(
"cannot add entry with key "
+
std
::
string
(
key
)
+
" to "
+
_typename
());
}
}
std
::
map
<
std
::
string
,
JSON
>*
object
=
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_payload
);
object_t
*
object
=
static_cast
<
object_t
*>
(
_payload
);
if
(
object
->
find
(
key
)
==
object
->
end
())
{
if
(
object
->
find
(
key
)
==
object
->
end
())
{
(
*
object
)[
key
]
=
JSON
();
(
*
object
)[
key
]
=
JSON
();
}
}
...
@@ -410,14 +466,14 @@ JSON& JSON::operator[](const char* key) {
...
@@ -410,14 +466,14 @@ JSON& JSON::operator[](const char* key) {
if
(
_type
==
null
)
{
if
(
_type
==
null
)
{
_type
=
object
;
_type
=
object
;
_payload
=
new
std
::
map
<
std
::
string
,
JSON
>
;
_payload
=
new
object_t
;
}
}
if
(
_type
!=
object
)
{
if
(
_type
!=
object
)
{
throw
std
::
runtime_error
(
"cannot add entry with key "
+
std
::
string
(
key
)
+
" to "
+
_typename
());
throw
std
::
runtime_error
(
"cannot add entry with key "
+
std
::
string
(
key
)
+
" to "
+
_typename
());
}
}
std
::
map
<
std
::
string
,
JSON
>*
object
=
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_payload
);
object_t
*
object
=
static_cast
<
object_t
*>
(
_payload
);
if
(
object
->
find
(
key
)
==
object
->
end
())
{
if
(
object
->
find
(
key
)
==
object
->
end
())
{
(
*
object
)[
key
]
=
JSON
();
(
*
object
)[
key
]
=
JSON
();
}
}
...
@@ -431,7 +487,7 @@ const JSON& JSON::operator[](const std::string& key) const {
...
@@ -431,7 +487,7 @@ const JSON& JSON::operator[](const std::string& key) const {
throw
std
::
runtime_error
(
"cannot get entry with key "
+
std
::
string
(
key
)
+
" from "
+
_typename
());
throw
std
::
runtime_error
(
"cannot get entry with key "
+
std
::
string
(
key
)
+
" from "
+
_typename
());
}
}
const
std
::
map
<
std
::
string
,
JSON
>*
object
=
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_payload
);
const
object_t
*
object
=
static_cast
<
object_t
*>
(
_payload
);
if
(
object
->
find
(
key
)
==
object
->
end
())
{
if
(
object
->
find
(
key
)
==
object
->
end
())
{
throw
std
::
runtime_error
(
"key "
+
key
+
" not found"
);
throw
std
::
runtime_error
(
"key "
+
key
+
" not found"
);
}
else
{
}
else
{
...
@@ -443,16 +499,16 @@ const JSON& JSON::operator[](const std::string& key) const {
...
@@ -443,16 +499,16 @@ const JSON& JSON::operator[](const std::string& key) const {
size_t
JSON
::
size
()
const
{
size_t
JSON
::
size
()
const
{
switch
(
_type
)
{
switch
(
_type
)
{
case
(
array
):
case
(
array
):
return
static_cast
<
std
::
vector
<
JSON
>
*>
(
_payload
)
->
size
();
return
static_cast
<
array_t
*>
(
_payload
)
->
size
();
case
(
object
):
case
(
object
):
return
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_payload
)
->
size
();
return
static_cast
<
object_t
*>
(
_payload
)
->
size
();
case
(
null
):
case
(
null
):
return
0
;
return
0
;
case
(
string
):
case
(
string
):
return
1
;
return
1
;
case
(
boolean
):
case
(
boolean
):
return
1
;
return
1
;
case
(
number
_int
):
case
(
number
):
return
1
;
return
1
;
case
(
number_float
):
case
(
number_float
):
return
1
;
return
1
;
...
@@ -463,16 +519,16 @@ size_t JSON::size() const {
...
@@ -463,16 +519,16 @@ size_t JSON::size() const {
bool
JSON
::
empty
()
const
{
bool
JSON
::
empty
()
const
{
switch
(
_type
)
{
switch
(
_type
)
{
case
(
array
):
case
(
array
):
return
static_cast
<
std
::
vector
<
JSON
>
*>
(
_payload
)
->
empty
();
return
static_cast
<
array_t
*>
(
_payload
)
->
empty
();
case
(
object
):
case
(
object
):
return
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_payload
)
->
empty
();
return
static_cast
<
object_t
*>
(
_payload
)
->
empty
();
case
(
null
):
case
(
null
):
return
true
;
return
true
;
case
(
string
):
case
(
string
):
return
false
;
return
false
;
case
(
boolean
):
case
(
boolean
):
return
false
;
return
false
;
case
(
number
_int
):
case
(
number
):
return
false
;
return
false
;
case
(
number_float
):
case
(
number_float
):
return
false
;
return
false
;
...
@@ -496,12 +552,12 @@ JSON::iterator JSON::find(const char* key) {
...
@@ -496,12 +552,12 @@ JSON::iterator JSON::find(const char* key) {
if
(
_type
!=
object
)
{
if
(
_type
!=
object
)
{
return
end
();
return
end
();
}
else
{
}
else
{
std
::
map
<
std
::
string
,
JSON
>*
o
=
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_payload
);
object_t
*
o
=
static_cast
<
object_t
*>
(
_payload
);
const
std
::
map
<
std
::
string
,
JSON
>
::
iterator
i
=
o
->
find
(
key
);
const
object_t
::
iterator
i
=
o
->
find
(
key
);
if
(
i
!=
o
->
end
())
{
if
(
i
!=
o
->
end
())
{
JSON
::
iterator
result
;
JSON
::
iterator
result
;
result
.
_object
=
this
;
result
.
_object
=
this
;
result
.
_oi
=
new
std
::
map
<
std
::
string
,
JSON
>
::
iterator
(
i
);
result
.
_oi
=
new
object_t
::
iterator
(
i
);
return
result
;
return
result
;
}
else
{
}
else
{
return
end
();
return
end
();
...
@@ -513,12 +569,12 @@ JSON::const_iterator JSON::find(const char* key) const {
...
@@ -513,12 +569,12 @@ JSON::const_iterator JSON::find(const char* key) const {
if
(
_type
!=
object
)
{
if
(
_type
!=
object
)
{
return
end
();
return
end
();
}
else
{
}
else
{
std
::
map
<
std
::
string
,
JSON
>*
o
=
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_payload
);
object_t
*
o
=
static_cast
<
object_t
*>
(
_payload
);
const
std
::
map
<
std
::
string
,
JSON
>
::
const_iterator
i
=
o
->
find
(
key
);
const
object_t
::
const_iterator
i
=
o
->
find
(
key
);
if
(
i
!=
o
->
end
())
{
if
(
i
!=
o
->
end
())
{
JSON
::
const_iterator
result
;
JSON
::
const_iterator
result
;
result
.
_object
=
this
;
result
.
_object
=
this
;
result
.
_oi
=
new
std
::
map
<
std
::
string
,
JSON
>
::
const_iterator
(
i
);
result
.
_oi
=
new
object_t
::
const_iterator
(
i
);
return
result
;
return
result
;
}
else
{
}
else
{
return
end
();
return
end
();
...
@@ -541,15 +597,15 @@ bool JSON::operator==(const JSON& o) const {
...
@@ -541,15 +597,15 @@ bool JSON::operator==(const JSON& o) const {
switch
(
_type
)
{
switch
(
_type
)
{
case
(
array
):
{
case
(
array
):
{
if
(
o
.
_type
==
array
)
{
if
(
o
.
_type
==
array
)
{
std
::
vector
<
JSON
>*
a
=
static_cast
<
std
::
vector
<
JSON
>
*>
(
_payload
);
array_t
*
a
=
static_cast
<
array_t
*>
(
_payload
);
std
::
vector
<
JSON
>*
b
=
static_cast
<
std
::
vector
<
JSON
>
*>
(
o
.
_payload
);
array_t
*
b
=
static_cast
<
array_t
*>
(
o
.
_payload
);
return
*
a
==
*
b
;
return
*
a
==
*
b
;
}
}
}
}
case
(
object
):
{
case
(
object
):
{
if
(
o
.
_type
==
object
)
{
if
(
o
.
_type
==
object
)
{
std
::
map
<
std
::
string
,
JSON
>*
a
=
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_payload
);
object_t
*
a
=
static_cast
<
object_t
*>
(
_payload
);
std
::
map
<
std
::
string
,
JSON
>*
b
=
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
o
.
_payload
);
object_t
*
b
=
static_cast
<
object_t
*>
(
o
.
_payload
);
return
*
a
==
*
b
;
return
*
a
==
*
b
;
}
}
}
}
...
@@ -572,15 +628,15 @@ bool JSON::operator==(const JSON& o) const {
...
@@ -572,15 +628,15 @@ bool JSON::operator==(const JSON& o) const {
return
a
==
b
;
return
a
==
b
;
}
}
}
}
case
(
number
_int
):
{
case
(
number
):
{
if
(
o
.
_type
==
number
_int
or
o
.
_type
==
number_float
)
{
if
(
o
.
_type
==
number
or
o
.
_type
==
number_float
)
{
int
a
=
*
this
;
int
a
=
*
this
;
int
b
=
o
;
int
b
=
o
;
return
a
==
b
;
return
a
==
b
;
}
}
}
}
case
(
number_float
):
{
case
(
number_float
):
{
if
(
o
.
_type
==
number
_int
or
o
.
_type
==
number_float
)
{
if
(
o
.
_type
==
number
or
o
.
_type
==
number_float
)
{
double
a
=
*
this
;
double
a
=
*
this
;
double
b
=
o
;
double
b
=
o
;
return
a
==
b
;
return
a
==
b
;
...
@@ -606,7 +662,7 @@ std::string JSON::_typename() const {
...
@@ -606,7 +662,7 @@ std::string JSON::_typename() const {
return
"string"
;
return
"string"
;
case
(
boolean
):
case
(
boolean
):
return
"boolean"
;
return
"boolean"
;
case
(
number
_int
):
case
(
number
):
return
"number"
;
return
"number"
;
case
(
number_float
):
case
(
number_float
):
return
"number"
;
return
"number"
;
...
@@ -761,7 +817,7 @@ void JSON::parser::parse(JSON& result) {
...
@@ -761,7 +817,7 @@ void JSON::parser::parse(JSON& result) {
case
(
'{'
):
{
case
(
'{'
):
{
// explicitly set result to object to cope with {}
// explicitly set result to object to cope with {}
result
.
_type
=
object
;
result
.
_type
=
object
;
result
.
_payload
=
new
std
::
map
<
std
::
string
,
JSON
>
;
result
.
_payload
=
new
object_t
;
next
();
next
();
...
@@ -788,7 +844,7 @@ void JSON::parser::parse(JSON& result) {
...
@@ -788,7 +844,7 @@ void JSON::parser::parse(JSON& result) {
case
(
'['
):
{
case
(
'['
):
{
// explicitly set result to array to cope with []
// explicitly set result to array to cope with []
result
.
_type
=
array
;
result
.
_type
=
array
;
result
.
_payload
=
new
std
::
vector
<
JSON
>
;
result
.
_payload
=
new
array_t
;
next
();
next
();
...
@@ -845,7 +901,7 @@ void JSON::parser::parse(JSON& result) {
...
@@ -845,7 +901,7 @@ void JSON::parser::parse(JSON& result) {
if
(
tmp
.
find
(
"."
)
==
std
::
string
::
npos
)
{
if
(
tmp
.
find
(
"."
)
==
std
::
string
::
npos
)
{
// integer (we use atof, because it can cope with e)
// integer (we use atof, because it can cope with e)
result
.
_type
=
number
_int
;
result
.
_type
=
number
;
result
.
_payload
=
new
int
(
std
::
atof
(
tmp
.
c_str
()));
result
.
_payload
=
new
int
(
std
::
atof
(
tmp
.
c_str
()));
}
else
{
}
else
{
// float
// float
...
@@ -874,13 +930,14 @@ JSON::iterator JSON::end() {
...
@@ -874,13 +930,14 @@ JSON::iterator JSON::end() {
JSON
::
iterator
::
iterator
()
:
_object
(
nullptr
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{}
JSON
::
iterator
::
iterator
()
:
_object
(
nullptr
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{}
JSON
::
iterator
::
iterator
(
JSON
*
j
)
:
_object
(
j
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{
JSON
::
iterator
::
iterator
(
JSON
*
j
)
:
_object
(
j
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{
if
(
_object
!=
nullptr
)
switch
(
_object
->
_type
)
{
switch
(
_object
->
_type
)
{
case
(
array
):
{
case
(
array
):
{
_vi
=
new
std
::
vector
<
JSON
>::
iterator
(
static_cast
<
std
::
vector
<
JSON
>
*>
(
_object
->
_payload
)
->
begin
());
_vi
=
new
array_t
::
iterator
(
static_cast
<
array_t
*>
(
_object
->
_payload
)
->
begin
());
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
_oi
=
new
std
::
map
<
std
::
string
,
JSON
>::
iterator
(
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_object
->
_payload
)
->
begin
());
_oi
=
new
object_t
::
iterator
(
static_cast
<
object_t
*>
(
_object
->
_payload
)
->
begin
());
break
;
break
;
}
}
default
:
default
:
...
@@ -889,13 +946,14 @@ JSON::iterator::iterator(JSON* j) : _object(j), _vi(nullptr), _oi(nullptr) {
...
@@ -889,13 +946,14 @@ JSON::iterator::iterator(JSON* j) : _object(j), _vi(nullptr), _oi(nullptr) {
}
}
JSON
::
iterator
::
iterator
(
const
JSON
::
iterator
&
o
)
:
_object
(
o
.
_object
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{
JSON
::
iterator
::
iterator
(
const
JSON
::
iterator
&
o
)
:
_object
(
o
.
_object
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{
if
(
_object
!=
nullptr
)
switch
(
_object
->
_type
)
{
switch
(
_object
->
_type
)
{
case
(
array
):
{
case
(
array
):
{
_vi
=
new
std
::
vector
<
JSON
>
::
iterator
(
*
(
o
.
_vi
));
_vi
=
new
array_t
::
iterator
(
*
(
o
.
_vi
));
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
_oi
=
new
std
::
map
<
std
::
string
,
JSON
>
::
iterator
(
*
(
o
.
_oi
));
_oi
=
new
object_t
::
iterator
(
*
(
o
.
_oi
));
break
;
break
;
}
}
default
:
default
:
...
@@ -910,13 +968,14 @@ JSON::iterator::~iterator() {
...
@@ -910,13 +968,14 @@ JSON::iterator::~iterator() {
JSON
::
iterator
&
JSON
::
iterator
::
operator
=
(
const
JSON
::
iterator
&
o
)
{
JSON
::
iterator
&
JSON
::
iterator
::
operator
=
(
const
JSON
::
iterator
&
o
)
{
_object
=
o
.
_object
;
_object
=
o
.
_object
;
if
(
_object
!=
nullptr
)
switch
(
_object
->
_type
)
{
switch
(
_object
->
_type
)
{
case
(
array
):
{
case
(
array
):
{
_vi
=
new
std
::
vector
<
JSON
>
::
iterator
(
*
(
o
.
_vi
));
_vi
=
new
array_t
::
iterator
(
*
(
o
.
_vi
));
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
_oi
=
new
std
::
map
<
std
::
string
,
JSON
>
::
iterator
(
*
(
o
.
_oi
));
_oi
=
new
object_t
::
iterator
(
*
(
o
.
_oi
));
break
;
break
;
}
}
default
:
default
:
...
@@ -942,13 +1001,13 @@ JSON::iterator& JSON::iterator::operator++() {
...
@@ -942,13 +1001,13 @@ JSON::iterator& JSON::iterator::operator++() {
switch
(
_object
->
_type
)
{
switch
(
_object
->
_type
)
{
case
(
array
):
{
case
(
array
):
{
if
(
++
(
*
_vi
)
==
static_cast
<
std
::
vector
<
JSON
>
*>
(
_object
->
_payload
)
->
end
())
{
if
(
++
(
*
_vi
)
==
static_cast
<
array_t
*>
(
_object
->
_payload
)
->
end
())
{
_object
=
nullptr
;
_object
=
nullptr
;
}
}
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
if
(
++
(
*
_oi
)
==
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_object
->
_payload
)
->
end
())
{
if
(
++
(
*
_oi
)
==
static_cast
<
object_t
*>
(
_object
->
_payload
)
->
end
())
{
_object
=
nullptr
;
_object
=
nullptr
;
}
}
break
;
break
;
...
@@ -1033,13 +1092,14 @@ JSON::const_iterator JSON::cend() const {
...
@@ -1033,13 +1092,14 @@ JSON::const_iterator JSON::cend() const {
JSON
::
const_iterator
::
const_iterator
()
:
_object
(
nullptr
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{}
JSON
::
const_iterator
::
const_iterator
()
:
_object
(
nullptr
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{}
JSON
::
const_iterator
::
const_iterator
(
const
JSON
*
j
)
:
_object
(
j
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{
JSON
::
const_iterator
::
const_iterator
(
const
JSON
*
j
)
:
_object
(
j
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{
if
(
_object
!=
nullptr
)
switch
(
_object
->
_type
)
{
switch
(
_object
->
_type
)
{
case
(
array
):
{
case
(
array
):
{
_vi
=
new
std
::
vector
<
JSON
>::
const_iterator
(
static_cast
<
std
::
vector
<
JSON
>
*>
(
_object
->
_payload
)
->
begin
());
_vi
=
new
array_t
::
const_iterator
(
static_cast
<
array_t
*>
(
_object
->
_payload
)
->
begin
());
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
_oi
=
new
std
::
map
<
std
::
string
,
JSON
>::
const_iterator
(
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_object
->
_payload
)
->
begin
());
_oi
=
new
object_t
::
const_iterator
(
static_cast
<
object_t
*>
(
_object
->
_payload
)
->
begin
());
break
;
break
;
}
}
default
:
default
:
...
@@ -1048,13 +1108,14 @@ JSON::const_iterator::const_iterator(const JSON* j) : _object(j), _vi(nullptr),
...
@@ -1048,13 +1108,14 @@ JSON::const_iterator::const_iterator(const JSON* j) : _object(j), _vi(nullptr),
}
}
JSON
::
const_iterator
::
const_iterator
(
const
JSON
::
const_iterator
&
o
)
:
_object
(
o
.
_object
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{
JSON
::
const_iterator
::
const_iterator
(
const
JSON
::
const_iterator
&
o
)
:
_object
(
o
.
_object
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{
if
(
_object
!=
nullptr
)
switch
(
_object
->
_type
)
{
switch
(
_object
->
_type
)
{
case
(
array
):
{
case
(
array
):
{
_vi
=
new
std
::
vector
<
JSON
>
::
const_iterator
(
*
(
o
.
_vi
));
_vi
=
new
array_t
::
const_iterator
(
*
(
o
.
_vi
));
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
_oi
=
new
std
::
map
<
std
::
string
,
JSON
>
::
const_iterator
(
*
(
o
.
_oi
));
_oi
=
new
object_t
::
const_iterator
(
*
(
o
.
_oi
));
break
;
break
;
}
}
default
:
default
:
...
@@ -1063,13 +1124,14 @@ JSON::const_iterator::const_iterator(const JSON::const_iterator& o) : _object(o.
...
@@ -1063,13 +1124,14 @@ JSON::const_iterator::const_iterator(const JSON::const_iterator& o) : _object(o.
}
}
JSON
::
const_iterator
::
const_iterator
(
const
JSON
::
iterator
&
o
)
:
_object
(
o
.
_object
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{
JSON
::
const_iterator
::
const_iterator
(
const
JSON
::
iterator
&
o
)
:
_object
(
o
.
_object
),
_vi
(
nullptr
),
_oi
(
nullptr
)
{
if
(
_object
!=
nullptr
)
switch
(
_object
->
_type
)
{
switch
(
_object
->
_type
)
{
case
(
array
):
{
case
(
array
):
{
_vi
=
new
std
::
vector
<
JSON
>
::
const_iterator
(
*
(
o
.
_vi
));
_vi
=
new
array_t
::
const_iterator
(
*
(
o
.
_vi
));
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
_oi
=
new
std
::
map
<
std
::
string
,
JSON
>
::
const_iterator
(
*
(
o
.
_oi
));
_oi
=
new
object_t
::
const_iterator
(
*
(
o
.
_oi
));
break
;
break
;
}
}
default
:
default
:
...
@@ -1084,13 +1146,14 @@ JSON::const_iterator::~const_iterator() {
...
@@ -1084,13 +1146,14 @@ JSON::const_iterator::~const_iterator() {
JSON
::
const_iterator
&
JSON
::
const_iterator
::
operator
=
(
const
JSON
::
const_iterator
&
o
)
{
JSON
::
const_iterator
&
JSON
::
const_iterator
::
operator
=
(
const
JSON
::
const_iterator
&
o
)
{
_object
=
o
.
_object
;
_object
=
o
.
_object
;
if
(
_object
!=
nullptr
)
switch
(
_object
->
_type
)
{
switch
(
_object
->
_type
)
{
case
(
array
):
{
case
(
array
):
{
_vi
=
new
std
::
vector
<
JSON
>
::
const_iterator
(
*
(
o
.
_vi
));
_vi
=
new
array_t
::
const_iterator
(
*
(
o
.
_vi
));
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
_oi
=
new
std
::
map
<
std
::
string
,
JSON
>
::
const_iterator
(
*
(
o
.
_oi
));
_oi
=
new
object_t
::
const_iterator
(
*
(
o
.
_oi
));
break
;
break
;
}
}
default
:
default
:
...
@@ -1116,13 +1179,13 @@ JSON::const_iterator& JSON::const_iterator::operator++() {
...
@@ -1116,13 +1179,13 @@ JSON::const_iterator& JSON::const_iterator::operator++() {
switch
(
_object
->
_type
)
{
switch
(
_object
->
_type
)
{
case
(
array
):
{
case
(
array
):
{
if
(
++
(
*
_vi
)
==
static_cast
<
std
::
vector
<
JSON
>
*>
(
_object
->
_payload
)
->
end
())
{
if
(
++
(
*
_vi
)
==
static_cast
<
array_t
*>
(
_object
->
_payload
)
->
end
())
{
_object
=
nullptr
;
_object
=
nullptr
;
}
}
break
;
break
;
}
}
case
(
object
):
{
case
(
object
):
{
if
(
++
(
*
_oi
)
==
static_cast
<
std
::
map
<
std
::
string
,
JSON
>
*>
(
_object
->
_payload
)
->
end
())
{
if
(
++
(
*
_oi
)
==
static_cast
<
object_t
*>
(
_object
->
_payload
)
->
end
())
{
_object
=
nullptr
;
_object
=
nullptr
;
}
}
break
;
break
;
...
...
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