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
9e07e9b4
Unverified
Commit
9e07e9b4
authored
Mar 19, 2018
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✨
implemented non-throwing binary reader
parent
a271ee5f
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
250 additions
and
168 deletions
+250
-168
include/nlohmann/detail/input/binary_reader.hpp
include/nlohmann/detail/input/binary_reader.hpp
+36
-72
include/nlohmann/json.hpp
include/nlohmann/json.hpp
+50
-12
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+85
-84
test/src/unit-cbor.cpp
test/src/unit-cbor.cpp
+79
-0
No files found.
include/nlohmann/detail/input/binary_reader.hpp
View file @
9e07e9b4
...
...
@@ -41,6 +41,9 @@ class binary_reader
using
json_sax_t
=
json_sax
<
BasicJsonType
>
;
public:
/// the supported binary input formats
enum
class
binary_format_t
{
cbor
,
msgpack
,
ubjson
};
/*!
@brief create a binary reader
...
...
@@ -52,77 +55,50 @@ class binary_reader
}
/*!
@
brief create a JSON value from CBOR input
@
param[in] format the binary format to parse
@param[in] sax_ a SAX event processor
@param[in] strict whether to expect the input to be consumed completed
@return JSON value created from CBOR input
@throw parse_error.110 if input ended unexpectedly or the end of file was
not reached when @a strict was set to true
@throw parse_error.112 if unsupported byte was read
@return
*/
BasicJsonType
parse_cbor
(
const
bool
strict
)
bool
sax_parse
(
const
binary_format_t
format
,
json_sax_t
*
sax_
,
const
bool
strict
)
{
BasicJsonType
result
;
json_sax_dom_parser
<
BasicJsonType
>
sdp
(
result
);
sax
=
&
sdp
;
parse_cbor_internal
();
result
.
assert_invariant
();
if
(
strict
)
{
get
();
expect_eof
();
}
return
result
;
}
/*!
@brief create a JSON value from MessagePack input
@param[in] strict whether to expect the input to be consumed completed
@return JSON value created from MessagePack input
sax
=
sax_
;
bool
result
;
@throw parse_error.110 if input ended unexpectedly or the end of file was
not reached when @a strict was set to true
@throw parse_error.112 if unsupported byte was read
*/
BasicJsonType
parse_msgpack
(
const
bool
strict
)
{
BasicJsonType
result
;
json_sax_dom_parser
<
BasicJsonType
>
sdp
(
result
);
sax
=
&
sdp
;
parse_msgpack_internal
();
result
.
assert_invariant
();
if
(
strict
)
switch
(
format
)
{
get
();
expect_eof
();
}
return
result
;
}
case
binary_format_t
:
:
cbor
:
result
=
parse_cbor_internal
();
break
;
/*!
@brief create a JSON value from UBJSON input
case
binary_format_t
:
:
msgpack
:
result
=
parse_msgpack_internal
();
break
;
@param[in] strict whether to expect the input to be consumed completed
@return JSON value created from UBJSON input
case
binary_format_t
:
:
ubjson
:
result
=
parse_ubjson_internal
();
break
;
}
@throw parse_error.110 if input ended unexpectedly or the end of file was
not reached when @a strict was set to true
@throw parse_error.112 if unsupported byte was read
*/
BasicJsonType
parse_ubjson
(
const
bool
strict
)
{
BasicJsonType
result
;
json_sax_dom_parser
<
BasicJsonType
>
sdp
(
result
);
sax
=
&
sdp
;
parse_ubjson_internal
();
result
.
assert_invariant
();
if
(
strict
)
// strict mode: next byte must be EOF
if
(
result
and
strict
)
{
get_ignore_noop
();
expect_eof
();
if
(
format
==
binary_format_t
::
ubjson
)
{
get_ignore_noop
();
}
else
{
get
();
}
if
(
JSON_UNLIKELY
(
current
!=
std
::
char_traits
<
char
>::
eof
()))
{
return
sax
->
parse_error
(
chars_read
,
get_token_string
(),
parse_error
::
create
(
110
,
chars_read
,
"expected end of input"
));
}
}
return
result
;
}
...
...
@@ -1662,18 +1638,6 @@ class binary_reader
return
sax
->
end_object
();
}
/*!
@return whether input was completely read
*/
bool
expect_eof
()
const
{
if
(
JSON_UNLIKELY
(
current
!=
std
::
char_traits
<
char
>::
eof
()))
{
return
sax
->
parse_error
(
chars_read
,
get_token_string
(),
parse_error
::
create
(
110
,
chars_read
,
"expected end of input"
));
}
return
true
;
}
/*!
@return whether the last read character is not EOF
*/
...
...
include/nlohmann/json.hpp
View file @
9e07e9b4
...
...
@@ -6611,6 +6611,9 @@ class basic_json
@param[in] i an input in CBOR format convertible to an input adapter
@param[in] strict whether to expect the input to be consumed until EOF
(true by default)
@param[in] allow_exceptions whether to throw exceptions in case of a
parse error (optional, true by default)
@return deserialized JSON value
@throw parse_error.110 if the given input ends prematurely or the end of
...
...
@@ -6636,9 +6639,13 @@ class basic_json
@a strict parameter since 3.0.0
*/
static
basic_json
from_cbor
(
detail
::
input_adapter
i
,
const
bool
strict
=
true
)
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
return
binary_reader
(
i
).
parse_cbor
(
strict
);
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
i
)).
sax_parse
(
binary_reader
::
binary_format_t
::
cbor
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
/*!
...
...
@@ -6646,9 +6653,14 @@ class basic_json
*/
template
<
typename
A1
,
typename
A2
,
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
input_adapter
,
A1
,
A2
>
::
value
,
int
>
=
0
>
static
basic_json
from_cbor
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
)
static
basic_json
from_cbor
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
return
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
))).
parse_cbor
(
strict
);
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
))).
sax_parse
(
binary_reader
::
binary_format_t
::
cbor
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
/*!
...
...
@@ -6701,6 +6713,10 @@ class basic_json
adapter
@param[in] strict whether to expect the input to be consumed until EOF
(true by default)
@param[in] allow_exceptions whether to throw exceptions in case of a
parse error (optional, true by default)
@return deserialized JSON value
@throw parse_error.110 if the given input ends prematurely or the end of
file was not reached when @a strict was set to true
...
...
@@ -6725,9 +6741,13 @@ class basic_json
@a strict parameter since 3.0.0
*/
static
basic_json
from_msgpack
(
detail
::
input_adapter
i
,
const
bool
strict
=
true
)
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
return
binary_reader
(
i
).
parse_msgpack
(
strict
);
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
i
)).
sax_parse
(
binary_reader
::
binary_format_t
::
msgpack
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
/*!
...
...
@@ -6735,9 +6755,14 @@ class basic_json
*/
template
<
typename
A1
,
typename
A2
,
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
input_adapter
,
A1
,
A2
>
::
value
,
int
>
=
0
>
static
basic_json
from_msgpack
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
)
static
basic_json
from_msgpack
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
return
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
))).
parse_msgpack
(
strict
);
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
))).
sax_parse
(
binary_reader
::
binary_format_t
::
msgpack
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
/*!
...
...
@@ -6772,6 +6797,10 @@ class basic_json
@param[in] i an input in UBJSON format convertible to an input adapter
@param[in] strict whether to expect the input to be consumed until EOF
(true by default)
@param[in] allow_exceptions whether to throw exceptions in case of a
parse error (optional, true by default)
@return deserialized JSON value
@throw parse_error.110 if the given input ends prematurely or the end of
file was not reached when @a strict was set to true
...
...
@@ -6794,16 +6823,25 @@ class basic_json
@since version 3.1.0
*/
static
basic_json
from_ubjson
(
detail
::
input_adapter
i
,
const
bool
strict
=
true
)
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
return
binary_reader
(
i
).
parse_ubjson
(
strict
);
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
i
)).
sax_parse
(
binary_reader
::
binary_format_t
::
ubjson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
template
<
typename
A1
,
typename
A2
,
detail
::
enable_if_t
<
std
::
is_constructible
<
detail
::
input_adapter
,
A1
,
A2
>
::
value
,
int
>
=
0
>
static
basic_json
from_ubjson
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
)
static
basic_json
from_ubjson
(
A1
&&
a1
,
A2
&&
a2
,
const
bool
strict
=
true
,
const
bool
allow_exceptions
=
true
)
{
return
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
))).
parse_ubjson
(
strict
);
basic_json
result
;
detail
::
json_sax_dom_parser
<
basic_json
>
sdp
(
result
,
allow_exceptions
);
const
bool
res
=
binary_reader
(
detail
::
input_adapter
(
std
::
forward
<
A1
>
(
a1
),
std
::
forward
<
A2
>
(
a2
))).
sax_parse
(
binary_reader
::
binary_format_t
::
ubjson
,
&
sdp
,
strict
);
return
res
?
result
:
basic_json
(
value_t
::
discarded
);
}
/// @}
...
...
single_include/nlohmann/json.hpp
View file @
9e07e9b4
This diff is collapsed.
Click to expand it.
test/src/unit-cbor.cpp
View file @
9e07e9b4
This diff is collapsed.
Click to expand it.
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