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
89c98dfc
Unverified
Commit
89c98dfc
authored
4 years ago
by
Niels Lohmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add option to process binary subtypes in CBOR
parent
51a98800
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
114 additions
and
20 deletions
+114
-20
include/nlohmann/detail/input/binary_reader.hpp
include/nlohmann/detail/input/binary_reader.hpp
+53
-10
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+53
-10
test/src/unit-cbor.cpp
test/src/unit-cbor.cpp
+8
-0
No files found.
include/nlohmann/detail/input/binary_reader.hpp
View file @
89c98dfc
...
...
@@ -30,8 +30,9 @@ namespace detail
/// how to treat CBOR tags
enum
class
cbor_tag_handler_t
{
error
,
///< throw a parse_error exception in case of a tag
ignore
///< ignore tags
error
,
///< throw a parse_error exception in case of a tag
ignore
,
///< ignore tags
store
///< store tags as binary type
};
/*!
...
...
@@ -722,30 +723,31 @@ class binary_reader
case
cbor_tag_handler_t
:
:
ignore
:
{
// ignore binary subtype
switch
(
current
)
{
case
0xD8
:
{
std
::
uint8_t
len
{};
get_number
(
input_format_t
::
cbor
,
len
);
std
::
uint8_t
subtype_to_ignore
{};
get_number
(
input_format_t
::
cbor
,
subtype_to_ignore
);
break
;
}
case
0xD9
:
{
std
::
uint16_t
len
{};
get_number
(
input_format_t
::
cbor
,
len
);
std
::
uint16_t
subtype_to_ignore
{};
get_number
(
input_format_t
::
cbor
,
subtype_to_ignore
);
break
;
}
case
0xDA
:
{
std
::
uint32_t
len
{};
get_number
(
input_format_t
::
cbor
,
len
);
std
::
uint32_t
subtype_to_ignore
{};
get_number
(
input_format_t
::
cbor
,
subtype_to_ignore
);
break
;
}
case
0xDB
:
{
std
::
uint64_t
len
{};
get_number
(
input_format_t
::
cbor
,
len
);
std
::
uint64_t
subtype_to_ignore
{};
get_number
(
input_format_t
::
cbor
,
subtype_to_ignore
);
break
;
}
default:
...
...
@@ -754,6 +756,47 @@ class binary_reader
return
parse_cbor_internal
(
true
,
tag_handler
);
}
case
cbor_tag_handler_t
:
:
store
:
{
binary_t
b
;
// use binary subtype and store in binary container
switch
(
current
)
{
case
0xD8
:
{
std
::
uint8_t
subtype
{};
get_number
(
input_format_t
::
cbor
,
subtype
);
b
.
set_subtype
(
detail
::
conditional_static_cast
<
typename
binary_t
::
subtype_type
>
(
subtype
));
break
;
}
case
0xD9
:
{
std
::
uint16_t
subtype
{};
get_number
(
input_format_t
::
cbor
,
subtype
);
b
.
set_subtype
(
detail
::
conditional_static_cast
<
typename
binary_t
::
subtype_type
>
(
subtype
));
break
;
}
case
0xDA
:
{
std
::
uint32_t
subtype
{};
get_number
(
input_format_t
::
cbor
,
subtype
);
b
.
set_subtype
(
detail
::
conditional_static_cast
<
typename
binary_t
::
subtype_type
>
(
subtype
));
break
;
}
case
0xDB
:
{
std
::
uint64_t
subtype
{};
get_number
(
input_format_t
::
cbor
,
subtype
);
b
.
set_subtype
(
detail
::
conditional_static_cast
<
typename
binary_t
::
subtype_type
>
(
subtype
));
break
;
}
default:
break
;
}
get
();
return
get_cbor_binary
(
b
)
&&
sax
->
binary
(
b
);
}
default:
// LCOV_EXCL_LINE
JSON_ASSERT
(
false
);
// NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
return
false
;
// LCOV_EXCL_LINE
...
...
This diff is collapsed.
Click to expand it.
single_include/nlohmann/json.hpp
View file @
89c98dfc
...
...
@@ -8269,8 +8269,9 @@ namespace detail
/// how to treat CBOR tags
enum class cbor_tag_handler_t
{
error, ///< throw a parse_error exception in case of a tag
ignore ///< ignore tags
error, ///< throw a parse_error exception in case of a tag
ignore, ///< ignore tags
store ///< store tags as binary type
};
/*!
...
...
@@ -8961,30 +8962,31 @@ class binary_reader
case cbor_tag_handler_t::ignore:
{
// ignore binary subtype
switch (current)
{
case 0xD8:
{
std::uint8_t
len
{};
get_number(input_format_t::cbor,
len
);
std::uint8_t
subtype_to_ignore
{};
get_number(input_format_t::cbor,
subtype_to_ignore
);
break;
}
case 0xD9:
{
std::uint16_t
len
{};
get_number(input_format_t::cbor,
len
);
std::uint16_t
subtype_to_ignore
{};
get_number(input_format_t::cbor,
subtype_to_ignore
);
break;
}
case 0xDA:
{
std::uint32_t
len
{};
get_number(input_format_t::cbor,
len
);
std::uint32_t
subtype_to_ignore
{};
get_number(input_format_t::cbor,
subtype_to_ignore
);
break;
}
case 0xDB:
{
std::uint64_t
len
{};
get_number(input_format_t::cbor,
len
);
std::uint64_t
subtype_to_ignore
{};
get_number(input_format_t::cbor,
subtype_to_ignore
);
break;
}
default:
...
...
@@ -8993,6 +8995,47 @@ class binary_reader
return parse_cbor_internal(true, tag_handler);
}
case cbor_tag_handler_t::store:
{
binary_t b;
// use binary subtype and store in binary container
switch (current)
{
case 0xD8:
{
std::uint8_t subtype{};
get_number(input_format_t::cbor, subtype);
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
break;
}
case 0xD9:
{
std::uint16_t subtype{};
get_number(input_format_t::cbor, subtype);
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
break;
}
case 0xDA:
{
std::uint32_t subtype{};
get_number(input_format_t::cbor, subtype);
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
break;
}
case 0xDB:
{
std::uint64_t subtype{};
get_number(input_format_t::cbor, subtype);
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
break;
}
default:
break;
}
get();
return get_cbor_binary(b) && sax->binary(b);
}
default: // LCOV_EXCL_LINE
JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
return false; // LCOV_EXCL_LINE
...
...
This diff is collapsed.
Click to expand it.
test/src/unit-cbor.cpp
View file @
89c98dfc
...
...
@@ -2488,12 +2488,20 @@ TEST_CASE("examples from RFC 7049 Appendix A")
// 0xd8
CHECK
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
0x42
))
==
std
::
vector
<
uint8_t
>
{
0xd8
,
0x42
,
0x40
});
CHECK
(
!
json
::
from_cbor
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
0x42
)),
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
).
get_binary
().
has_subtype
());
CHECK
(
json
::
from_cbor
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
0x42
)),
true
,
true
,
json
::
cbor_tag_handler_t
::
store
).
get_binary
().
subtype
()
==
0x42
);
// 0xd9
CHECK
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
1000
))
==
std
::
vector
<
uint8_t
>
{
0xd9
,
0x03
,
0xe8
,
0x40
});
CHECK
(
!
json
::
from_cbor
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
1000
)),
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
).
get_binary
().
has_subtype
());
CHECK
(
json
::
from_cbor
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
1000
)),
true
,
true
,
json
::
cbor_tag_handler_t
::
store
).
get_binary
().
subtype
()
==
1000
);
// 0xda
CHECK
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
394216
))
==
std
::
vector
<
uint8_t
>
{
0xda
,
0x00
,
0x06
,
0x03
,
0xe8
,
0x40
});
CHECK
(
!
json
::
from_cbor
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
394216
)),
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
).
get_binary
().
has_subtype
());
CHECK
(
json
::
from_cbor
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
394216
)),
true
,
true
,
json
::
cbor_tag_handler_t
::
store
).
get_binary
().
subtype
()
==
394216
);
// 0xdb
CHECK
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
8589934590
))
==
std
::
vector
<
uint8_t
>
{
0xdb
,
0x00
,
0x00
,
0x00
,
0x01
,
0xff
,
0xff
,
0xff
,
0xfe
,
0x40
});
CHECK
(
!
json
::
from_cbor
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
8589934590
)),
true
,
true
,
json
::
cbor_tag_handler_t
::
ignore
).
get_binary
().
has_subtype
());
CHECK
(
json
::
from_cbor
(
json
::
to_cbor
(
json
::
binary
(
std
::
vector
<
uint8_t
>
{},
8589934590
)),
true
,
true
,
json
::
cbor_tag_handler_t
::
store
).
get_binary
().
subtype
()
==
8589934590
);
}
SECTION
(
"arrays"
)
...
...
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