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
Hide 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
*/
JSON
JSON
::
parse
(
const
std
::
string
&
s
)
{
JSON
j
;
Parser
(
s
).
parse
(
j
);
return
j
;
return
Parser
(
s
).
parse
();
}
/*!
...
...
@@ -302,9 +300,7 @@ JSON JSON::parse(const std::string& s)
*/
JSON
JSON
::
parse
(
const
char
*
s
)
{
JSON
j
;
Parser
(
s
).
parse
(
j
);
return
j
;
return
Parser
(
s
).
parse
();
}
...
...
@@ -1793,18 +1789,14 @@ JSON::Parser::Parser(std::istream& _is)
next
();
}
/*!
@param[out] result the JSON to parse to
*/
void
JSON
::
Parser
::
parse
(
JSON
&
result
)
JSON
JSON
::
Parser
::
parse
()
{
switch
(
_current
)
{
case
(
'{'
):
{
// explicitly set result to object to cope with {}
result
.
_type
=
value_type
::
object
;
result
.
_value
.
object
=
new
object_t
;
JSON
result
(
value_type
::
object
);
next
();
...
...
@@ -1820,7 +1812,7 @@ void JSON::Parser::parse(JSON& result)
expect
(
':'
);
// value
parse
(
result
[
std
::
move
(
key
)]
);
result
[
std
::
move
(
key
)]
=
parse
(
);
}
while
(
_current
==
','
and
next
());
}
...
...
@@ -1828,14 +1820,13 @@ void JSON::Parser::parse(JSON& result)
// closing brace
expect
(
'}'
);
break
;
return
result
;
}
case
(
'['
):
{
// explicitly set result to array to cope with []
result
.
_type
=
value_type
::
array
;
result
.
_value
.
array
=
new
array_t
;
JSON
result
(
value_type
::
array
);
next
();
...
...
@@ -1845,9 +1836,7 @@ void JSON::Parser::parse(JSON& result)
size_t
element_count
=
0
;
do
{
// add a dummy value and continue parsing at its position
result
+=
JSON
();
parse
(
result
[
element_count
++
]);
result
.
push_back
(
parse
());
}
while
(
_current
==
','
and
next
());
}
...
...
@@ -1855,37 +1844,30 @@ void JSON::Parser::parse(JSON& result)
// closing bracket
expect
(
']'
);
break
;
return
result
;
}
case
(
'\"'
):
{
result
.
_type
=
value_type
::
string
;
result
.
_value
.
string
=
new
string_t
(
std
::
move
(
parseString
()));
break
;
return
JSON
(
parseString
());
}
case
(
't'
):
{
parseTrue
();
result
.
_type
=
value_type
::
boolean
;
result
.
_value
.
boolean
=
true
;
break
;
return
JSON
(
true
);
}
case
(
'f'
):
{
parseFalse
();
result
.
_type
=
value_type
::
boolean
;
result
.
_value
.
boolean
=
false
;
break
;
return
JSON
(
false
);
}
case
(
'n'
):
{
parseNull
();
// nothing to do with result: is null by default
break
;
return
JSON
();
}
default
:
...
...
@@ -1911,14 +1893,12 @@ void JSON::Parser::parse(JSON& result)
if
(
float_val
==
int_val
)
{
// we would not lose precision -> int
result
.
_type
=
value_type
::
number
;
result
.
_value
.
number
=
int_val
;
return
JSON
(
int_val
);
}
else
{
// we would lose precision -> float
result
.
_type
=
value_type
::
number_float
;
result
.
_value
.
number_float
=
float_val
;
return
JSON
(
float_val
);
}
}
catch
(...)
...
...
src/JSON.h
View file @
18364aac
...
...
@@ -194,13 +194,13 @@ class JSON
/// read from stream
friend
std
::
istream
&
operator
>>
(
std
::
istream
&
i
,
JSON
&
j
)
{
Parser
(
i
).
parse
(
j
);
j
=
Parser
(
i
).
parse
(
);
return
i
;
}
/// read from stream
friend
std
::
istream
&
operator
<<
(
JSON
&
j
,
std
::
istream
&
i
)
{
Parser
(
i
).
parse
(
j
);
j
=
Parser
(
i
).
parse
(
);
return
i
;
}
...
...
@@ -406,8 +406,8 @@ class JSON
// no copy assignment
Parser
&
operator
=
(
Parser
)
=
delete
;
/// parse
into a given
JSON object
void
parse
(
JSON
&
);
/// parse
and return a
JSON object
JSON
parse
(
);
private
:
/// 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