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
18364aac
Commit
18364aac
authored
Jan 02, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
parser returns an object
parent
16c30cff
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
39 deletions
+19
-39
JSON.cc
src/JSON.cc
+15
-35
JSON.h
src/JSON.h
+4
-4
No files found.
src/JSON.cc
View file @
18364aac
...
@@ -291,9 +291,7 @@ JSON::~JSON() noexcept
...
@@ -291,9 +291,7 @@ JSON::~JSON() noexcept
*/
*/
JSON
JSON
::
parse
(
const
std
::
string
&
s
)
JSON
JSON
::
parse
(
const
std
::
string
&
s
)
{
{
JSON
j
;
return
Parser
(
s
).
parse
();
Parser
(
s
).
parse
(
j
);
return
j
;
}
}
/*!
/*!
...
@@ -302,9 +300,7 @@ JSON JSON::parse(const std::string& s)
...
@@ -302,9 +300,7 @@ JSON JSON::parse(const std::string& s)
*/
*/
JSON
JSON
::
parse
(
const
char
*
s
)
JSON
JSON
::
parse
(
const
char
*
s
)
{
{
JSON
j
;
return
Parser
(
s
).
parse
();
Parser
(
s
).
parse
(
j
);
return
j
;
}
}
...
@@ -1793,18 +1789,14 @@ JSON::Parser::Parser(std::istream& _is)
...
@@ -1793,18 +1789,14 @@ JSON::Parser::Parser(std::istream& _is)
next
();
next
();
}
}
/*!
JSON
JSON
::
Parser
::
parse
()
@param[out] result the JSON to parse to
*/
void
JSON
::
Parser
::
parse
(
JSON
&
result
)
{
{
switch
(
_current
)
switch
(
_current
)
{
{
case
(
'{'
):
case
(
'{'
):
{
{
// explicitly set result to object to cope with {}
// explicitly set result to object to cope with {}
result
.
_type
=
value_type
::
object
;
JSON
result
(
value_type
::
object
);
result
.
_value
.
object
=
new
object_t
;
next
();
next
();
...
@@ -1820,7 +1812,7 @@ void JSON::Parser::parse(JSON& result)
...
@@ -1820,7 +1812,7 @@ void JSON::Parser::parse(JSON& result)
expect
(
':'
);
expect
(
':'
);
// value
// value
parse
(
result
[
std
::
move
(
key
)]
);
result
[
std
::
move
(
key
)]
=
parse
(
);
}
}
while
(
_current
==
','
and
next
());
while
(
_current
==
','
and
next
());
}
}
...
@@ -1828,14 +1820,13 @@ void JSON::Parser::parse(JSON& result)
...
@@ -1828,14 +1820,13 @@ void JSON::Parser::parse(JSON& result)
// closing brace
// closing brace
expect
(
'}'
);
expect
(
'}'
);
break
;
return
result
;
}
}
case
(
'['
):
case
(
'['
):
{
{
// explicitly set result to array to cope with []
// explicitly set result to array to cope with []
result
.
_type
=
value_type
::
array
;
JSON
result
(
value_type
::
array
);
result
.
_value
.
array
=
new
array_t
;
next
();
next
();
...
@@ -1845,9 +1836,7 @@ void JSON::Parser::parse(JSON& result)
...
@@ -1845,9 +1836,7 @@ void JSON::Parser::parse(JSON& result)
size_t
element_count
=
0
;
size_t
element_count
=
0
;
do
do
{
{
// add a dummy value and continue parsing at its position
result
.
push_back
(
parse
());
result
+=
JSON
();
parse
(
result
[
element_count
++
]);
}
}
while
(
_current
==
','
and
next
());
while
(
_current
==
','
and
next
());
}
}
...
@@ -1855,37 +1844,30 @@ void JSON::Parser::parse(JSON& result)
...
@@ -1855,37 +1844,30 @@ void JSON::Parser::parse(JSON& result)
// closing bracket
// closing bracket
expect
(
']'
);
expect
(
']'
);
break
;
return
result
;
}
}
case
(
'\"'
):
case
(
'\"'
):
{
{
result
.
_type
=
value_type
::
string
;
return
JSON
(
parseString
());
result
.
_value
.
string
=
new
string_t
(
std
::
move
(
parseString
()));
break
;
}
}
case
(
't'
):
case
(
't'
):
{
{
parseTrue
();
parseTrue
();
result
.
_type
=
value_type
::
boolean
;
return
JSON
(
true
);
result
.
_value
.
boolean
=
true
;
break
;
}
}
case
(
'f'
):
case
(
'f'
):
{
{
parseFalse
();
parseFalse
();
result
.
_type
=
value_type
::
boolean
;
return
JSON
(
false
);
result
.
_value
.
boolean
=
false
;
break
;
}
}
case
(
'n'
):
case
(
'n'
):
{
{
parseNull
();
parseNull
();
// nothing to do with result: is null by default
return
JSON
();
break
;
}
}
default
:
default
:
...
@@ -1911,14 +1893,12 @@ void JSON::Parser::parse(JSON& result)
...
@@ -1911,14 +1893,12 @@ void JSON::Parser::parse(JSON& result)
if
(
float_val
==
int_val
)
if
(
float_val
==
int_val
)
{
{
// we would not lose precision -> int
// we would not lose precision -> int
result
.
_type
=
value_type
::
number
;
return
JSON
(
int_val
);
result
.
_value
.
number
=
int_val
;
}
}
else
else
{
{
// we would lose precision -> float
// we would lose precision -> float
result
.
_type
=
value_type
::
number_float
;
return
JSON
(
float_val
);
result
.
_value
.
number_float
=
float_val
;
}
}
}
}
catch
(...)
catch
(...)
...
...
src/JSON.h
View file @
18364aac
...
@@ -194,13 +194,13 @@ class JSON
...
@@ -194,13 +194,13 @@ class JSON
/// read from stream
/// read from stream
friend
std
::
istream
&
operator
>>
(
std
::
istream
&
i
,
JSON
&
j
)
friend
std
::
istream
&
operator
>>
(
std
::
istream
&
i
,
JSON
&
j
)
{
{
Parser
(
i
).
parse
(
j
);
j
=
Parser
(
i
).
parse
(
);
return
i
;
return
i
;
}
}
/// read from stream
/// read from stream
friend
std
::
istream
&
operator
<<
(
JSON
&
j
,
std
::
istream
&
i
)
friend
std
::
istream
&
operator
<<
(
JSON
&
j
,
std
::
istream
&
i
)
{
{
Parser
(
i
).
parse
(
j
);
j
=
Parser
(
i
).
parse
(
);
return
i
;
return
i
;
}
}
...
@@ -406,8 +406,8 @@ class JSON
...
@@ -406,8 +406,8 @@ class JSON
// no copy assignment
// no copy assignment
Parser
&
operator
=
(
Parser
)
=
delete
;
Parser
&
operator
=
(
Parser
)
=
delete
;
/// parse
into a given
JSON object
/// parse
and return a
JSON object
void
parse
(
JSON
&
);
JSON
parse
(
);
private
:
private
:
/// read the next character, stripping whitespace
/// read the next character, stripping whitespace
...
...
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