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
5b9d03cf
Unverified
Commit
5b9d03cf
authored
Mar 05, 2018
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🔨
added SAX-DOM-Parser
parent
9d274295
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
325 additions
and
124 deletions
+325
-124
benchmarks/src/benchmarks.cpp
benchmarks/src/benchmarks.cpp
+56
-0
include/nlohmann/detail/input/json_sax.hpp
include/nlohmann/detail/input/json_sax.hpp
+132
-0
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+2
-0
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+134
-0
test/src/unit-class_parser.cpp
test/src/unit-class_parser.cpp
+1
-124
No files found.
benchmarks/src/benchmarks.cpp
View file @
5b9d03cf
...
...
@@ -37,6 +37,35 @@ BENCHMARK_CAPTURE(ParseFile, signed_ints, "data/numbers/signed_ints.json");
BENCHMARK_CAPTURE
(
ParseFile
,
unsigned_ints
,
"data/numbers/unsigned_ints.json"
);
static
void
ParseFileSax
(
benchmark
::
State
&
state
,
const
char
*
filename
)
{
while
(
state
.
KeepRunning
())
{
state
.
PauseTiming
();
auto
*
f
=
new
std
::
ifstream
(
filename
);
auto
*
sdp
=
new
nlohmann
::
json_sax_dom_parser
<
json
>
();
state
.
ResumeTiming
();
json
::
sax_parse
(
*
f
,
sdp
);
state
.
PauseTiming
();
delete
f
;
delete
sdp
;
state
.
ResumeTiming
();
}
std
::
ifstream
file
(
filename
,
std
::
ios
::
binary
|
std
::
ios
::
ate
);
state
.
SetBytesProcessed
(
state
.
iterations
()
*
file
.
tellg
());
}
BENCHMARK_CAPTURE
(
ParseFileSax
,
jeopardy
,
"data/jeopardy/jeopardy.json"
);
BENCHMARK_CAPTURE
(
ParseFileSax
,
canada
,
"data/nativejson-benchmark/canada.json"
);
BENCHMARK_CAPTURE
(
ParseFileSax
,
citm_catalog
,
"data/nativejson-benchmark/citm_catalog.json"
);
BENCHMARK_CAPTURE
(
ParseFileSax
,
twitter
,
"data/nativejson-benchmark/twitter.json"
);
BENCHMARK_CAPTURE
(
ParseFileSax
,
floats
,
"data/numbers/floats.json"
);
BENCHMARK_CAPTURE
(
ParseFileSax
,
signed_ints
,
"data/numbers/signed_ints.json"
);
BENCHMARK_CAPTURE
(
ParseFileSax
,
unsigned_ints
,
"data/numbers/unsigned_ints.json"
);
//////////////////////////////////////////////////////////////////////////////
// parse JSON from string
//////////////////////////////////////////////////////////////////////////////
...
...
@@ -69,6 +98,33 @@ BENCHMARK_CAPTURE(ParseString, floats, "data/numbers/floats.json");
BENCHMARK_CAPTURE
(
ParseString
,
signed_ints
,
"data/numbers/signed_ints.json"
);
BENCHMARK_CAPTURE
(
ParseString
,
unsigned_ints
,
"data/numbers/unsigned_ints.json"
);
static
void
ParseStringSax
(
benchmark
::
State
&
state
,
const
char
*
filename
)
{
std
::
ifstream
f
(
filename
);
std
::
string
str
((
std
::
istreambuf_iterator
<
char
>
(
f
)),
std
::
istreambuf_iterator
<
char
>
());
while
(
state
.
KeepRunning
())
{
state
.
PauseTiming
();
auto
*
sdp
=
new
nlohmann
::
json_sax_dom_parser
<
json
>
();
state
.
ResumeTiming
();
json
::
sax_parse
(
str
,
sdp
);
state
.
PauseTiming
();
delete
sdp
;
state
.
ResumeTiming
();
}
state
.
SetBytesProcessed
(
state
.
iterations
()
*
str
.
size
());
}
BENCHMARK_CAPTURE
(
ParseStringSax
,
jeopardy
,
"data/jeopardy/jeopardy.json"
);
BENCHMARK_CAPTURE
(
ParseStringSax
,
canada
,
"data/nativejson-benchmark/canada.json"
);
BENCHMARK_CAPTURE
(
ParseStringSax
,
citm_catalog
,
"data/nativejson-benchmark/citm_catalog.json"
);
BENCHMARK_CAPTURE
(
ParseStringSax
,
twitter
,
"data/nativejson-benchmark/twitter.json"
);
BENCHMARK_CAPTURE
(
ParseStringSax
,
floats
,
"data/numbers/floats.json"
);
BENCHMARK_CAPTURE
(
ParseStringSax
,
signed_ints
,
"data/numbers/signed_ints.json"
);
BENCHMARK_CAPTURE
(
ParseStringSax
,
unsigned_ints
,
"data/numbers/unsigned_ints.json"
);
//////////////////////////////////////////////////////////////////////////////
// serialize JSON
...
...
include/nlohmann/detail/input/json_sax.hpp
View file @
5b9d03cf
...
...
@@ -122,5 +122,137 @@ struct json_sax
virtual
~
json_sax
()
=
default
;
};
template
<
typename
BasicJsonType
>
class
json_sax_dom_parser
:
public
json_sax
<
BasicJsonType
>
{
public:
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
bool
null
()
override
{
handle_value
(
nullptr
);
return
true
;
}
bool
boolean
(
bool
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
number_integer
(
number_integer_t
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
number_unsigned
(
number_unsigned_t
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
number_float
(
number_float_t
val
,
const
std
::
string
&
)
override
{
handle_value
(
val
);
return
true
;
}
bool
string
(
std
::
string
&&
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
start_object
(
std
::
size_t
)
override
{
ref_stack
.
push_back
(
handle_value
(
BasicJsonType
::
value_t
::
object
));
return
true
;
}
bool
key
(
std
::
string
&&
val
)
override
{
last_key
=
val
;
return
true
;
}
bool
end_object
()
override
{
ref_stack
.
pop_back
();
return
true
;
}
bool
start_array
(
std
::
size_t
)
override
{
ref_stack
.
push_back
(
handle_value
(
BasicJsonType
::
value_t
::
array
));
return
true
;
}
bool
end_array
()
override
{
ref_stack
.
pop_back
();
return
true
;
}
bool
binary
(
const
std
::
vector
<
uint8_t
>&
)
override
{
return
true
;
}
bool
parse_error
(
std
::
size_t
position
,
const
std
::
string
&
,
const
std
::
string
&
error_msg
)
override
{
JSON_THROW
(
BasicJsonType
::
parse_error
::
create
(
101
,
position
,
error_msg
));
return
false
;
}
BasicJsonType
&
get_value
()
{
return
root
;
}
private:
/// the parsed JSON value
BasicJsonType
root
;
/// stack to model hierarchy of values
std
::
vector
<
BasicJsonType
*>
ref_stack
;
/// helper variable for object keys
std
::
string
last_key
;
/*!
@invariant If the ref stack is empty, then the passed value will be the new
root.
@invariant If the ref stack contains a value, then it is an array or an
object to which we can add elements
*/
template
<
typename
Value
>
BasicJsonType
*
handle_value
(
Value
&&
v
)
{
if
(
ref_stack
.
empty
())
{
assert
(
root
.
is_null
());
root
=
BasicJsonType
(
std
::
forward
<
Value
>
(
v
));
return
&
root
;
}
else
{
assert
(
ref_stack
.
back
()
->
is_array
()
or
ref_stack
.
back
()
->
is_object
());
if
(
ref_stack
.
back
()
->
is_array
())
{
ref_stack
.
back
()
->
m_value
.
array
->
push_back
(
BasicJsonType
(
std
::
forward
<
Value
>
(
v
)));
return
&
(
ref_stack
.
back
()
->
m_value
.
array
->
back
());
}
else
{
BasicJsonType
&
r
=
ref_stack
.
back
()
->
m_value
.
object
->
operator
[](
last_key
)
=
BasicJsonType
(
std
::
forward
<
Value
>
(
v
));
return
&
r
;
}
}
}
};
}
include/nlohmann/json.hpp
View file @
5b9d03cf
...
...
@@ -171,6 +171,8 @@ class basic_json
friend
class
::
nlohmann
::
detail
::
binary_writer
;
template
<
typename
BasicJsonType
>
friend
class
::
nlohmann
::
detail
::
binary_reader
;
template
<
typename
BasicJsonType
>
friend
class
::
nlohmann
::
json_sax_dom_parser
;
/// workaround type for MSVC
using
basic_json_t
=
NLOHMANN_BASIC_JSON_TPL
;
...
...
single_include/nlohmann/json.hpp
View file @
5b9d03cf
...
...
@@ -3256,6 +3256,138 @@ struct json_sax
virtual
~
json_sax
()
=
default
;
};
template
<
typename
BasicJsonType
>
class
json_sax_dom_parser
:
public
json_sax
<
BasicJsonType
>
{
public:
using
number_integer_t
=
typename
BasicJsonType
::
number_integer_t
;
using
number_unsigned_t
=
typename
BasicJsonType
::
number_unsigned_t
;
using
number_float_t
=
typename
BasicJsonType
::
number_float_t
;
bool
null
()
override
{
handle_value
(
nullptr
);
return
true
;
}
bool
boolean
(
bool
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
number_integer
(
number_integer_t
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
number_unsigned
(
number_unsigned_t
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
number_float
(
number_float_t
val
,
const
std
::
string
&
)
override
{
handle_value
(
val
);
return
true
;
}
bool
string
(
std
::
string
&&
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
start_object
(
std
::
size_t
)
override
{
ref_stack
.
push_back
(
handle_value
(
BasicJsonType
::
value_t
::
object
));
return
true
;
}
bool
key
(
std
::
string
&&
val
)
override
{
last_key
=
val
;
return
true
;
}
bool
end_object
()
override
{
ref_stack
.
pop_back
();
return
true
;
}
bool
start_array
(
std
::
size_t
)
override
{
ref_stack
.
push_back
(
handle_value
(
BasicJsonType
::
value_t
::
array
));
return
true
;
}
bool
end_array
()
override
{
ref_stack
.
pop_back
();
return
true
;
}
bool
binary
(
const
std
::
vector
<
uint8_t
>&
)
override
{
return
true
;
}
bool
parse_error
(
std
::
size_t
position
,
const
std
::
string
&
,
const
std
::
string
&
error_msg
)
override
{
JSON_THROW
(
BasicJsonType
::
parse_error
::
create
(
101
,
position
,
error_msg
));
return
false
;
}
BasicJsonType
&
get_value
()
{
return
root
;
}
private:
/// the parsed JSON value
BasicJsonType
root
;
/// stack to model hierarchy of values
std
::
vector
<
BasicJsonType
*>
ref_stack
;
/// helper variable for object keys
std
::
string
last_key
;
/*!
@invariant If the ref stack is empty, then the passed value will be the new
root.
@invariant If the ref stack contains a value, then it is an array or an
object to which we can add elements
*/
template
<
typename
Value
>
BasicJsonType
*
handle_value
(
Value
&&
v
)
{
if
(
ref_stack
.
empty
())
{
assert
(
root
.
is_null
());
root
=
BasicJsonType
(
std
::
forward
<
Value
>
(
v
));
return
&
root
;
}
else
{
assert
(
ref_stack
.
back
()
->
is_array
()
or
ref_stack
.
back
()
->
is_object
());
if
(
ref_stack
.
back
()
->
is_array
())
{
ref_stack
.
back
()
->
m_value
.
array
->
push_back
(
BasicJsonType
(
std
::
forward
<
Value
>
(
v
)));
return
&
(
ref_stack
.
back
()
->
m_value
.
array
->
back
());
}
else
{
BasicJsonType
&
r
=
ref_stack
.
back
()
->
m_value
.
object
->
operator
[](
last_key
)
=
BasicJsonType
(
std
::
forward
<
Value
>
(
v
));
return
&
r
;
}
}
}
};
}
...
...
@@ -10096,6 +10228,8 @@ class basic_json
friend
class
::
nlohmann
::
detail
::
binary_writer
;
template
<
typename
BasicJsonType
>
friend
class
::
nlohmann
::
detail
::
binary_reader
;
template
<
typename
BasicJsonType
>
friend
class
::
nlohmann
::
json_sax_dom_parser
;
/// workaround type for MSVC
using
basic_json_t
=
NLOHMANN_BASIC_JSON_TPL
;
...
...
test/src/unit-class_parser.cpp
View file @
5b9d03cf
...
...
@@ -134,129 +134,6 @@ class SaxEventLogger : public nlohmann::json::json_sax_t
bool
errored
=
false
;
};
class
SaxDomParser
:
public
nlohmann
::
json
::
json_sax_t
{
public:
bool
null
()
override
{
handle_value
(
nullptr
);
return
true
;
}
bool
boolean
(
bool
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
number_integer
(
json
::
number_integer_t
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
number_unsigned
(
json
::
number_unsigned_t
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
number_float
(
json
::
number_float_t
val
,
const
std
::
string
&
)
override
{
handle_value
(
val
);
return
true
;
}
bool
string
(
std
::
string
&&
val
)
override
{
handle_value
(
val
);
return
true
;
}
bool
start_object
(
std
::
size_t
)
override
{
ref_stack
.
push_back
(
handle_value
(
json
::
value_t
::
object
));
return
true
;
}
bool
key
(
std
::
string
&&
val
)
override
{
last_key
=
val
;
return
true
;
}
bool
end_object
()
override
{
ref_stack
.
pop_back
();
return
true
;
}
bool
start_array
(
std
::
size_t
)
override
{
ref_stack
.
push_back
(
handle_value
(
json
::
value_t
::
array
));
return
true
;
}
bool
end_array
()
override
{
ref_stack
.
pop_back
();
return
true
;
}
bool
binary
(
const
std
::
vector
<
uint8_t
>&
)
override
{
return
true
;
}
bool
parse_error
(
std
::
size_t
position
,
const
std
::
string
&
,
const
std
::
string
&
)
override
{
return
false
;
}
json
&
get_value
()
{
return
root
;
}
private:
/// the parsed JSON value
json
root
;
/// stack to model hierarchy of values
std
::
vector
<
json
*>
ref_stack
;
/// helper variable for object keys
std
::
string
last_key
;
/*!
@invariant If the ref stack is empty, then the passed value will be the new
root.
@invariant If the ref stack contains a value, then it is an array or an
object to which we can add elements
*/
json
*
handle_value
(
json
&&
j
)
{
if
(
ref_stack
.
empty
())
{
assert
(
root
.
is_null
());
root
=
j
;
return
&
root
;
}
else
{
assert
(
ref_stack
.
back
()
->
is_array
()
or
ref_stack
.
back
()
->
is_object
());
if
(
ref_stack
.
back
()
->
is_array
())
{
ref_stack
.
back
()
->
push_back
(
j
);
return
&
(
ref_stack
.
back
()
->
back
());
}
else
{
json
&
r
=
ref_stack
.
back
()
->
operator
[](
last_key
)
=
j
;
return
&
r
;
}
}
}
};
json
parser_helper
(
const
std
::
string
&
s
);
bool
accept_helper
(
const
std
::
string
&
s
);
...
...
@@ -271,7 +148,7 @@ json parser_helper(const std::string& s)
CHECK_NOTHROW
(
json
::
parser
(
nlohmann
::
detail
::
input_adapter
(
s
),
nullptr
,
false
).
parse
(
true
,
j_nothrow
));
CHECK
(
j_nothrow
==
j
);
SaxDomParser
sdp
;
nlohmann
::
json_sax_dom_parser
<
json
>
sdp
;
json
::
sax_parse
(
s
,
&
sdp
);
CHECK
(
sdp
.
get_value
()
==
j
);
...
...
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