Skip to content

Commit b47a829

Browse files
committed
try_as_byte_string, try_as_byte_string_view
1 parent 722a955 commit b47a829

File tree

85 files changed

+1117
-1130
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1117
-1130
lines changed

CHANGELOG.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@
2323
- `try_decode_msgpack`
2424
- `try_decode_ubjson`
2525

26-
- New non-throwing accessors for `basic_json` that return a `std::expected`-like result,
27-
28-
- `try_as<T>()`
29-
- `try_as_integer<T>`
30-
- `try_as_double`
31-
- `try_as_string_view`
26+
- New non-throwing accessor `try_as<T>()` for `basic_json` that return a `std::expected`-like result,
3227

3328
- Breaking changes to staj iterator classes
3429

doc/Reference.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ All core jsoncons classes and functions are in namespace `jsoncons`.
88

99
[json_type_traits](ref/corelib/json_type_traits.md)
1010
[encode_json](ref/corelib/encode_json.md)
11-
[decode_json](ref/corelib/decode_json.md)
12-
[try_decode_json](ref/corelib/try_decode_json.md) (since 1.4.0)
11+
[decode_json, try_decode_json](ref/corelib/decode_json.md)
1312
[basic_json_options](ref/corelib/basic_json_options.md)
1413

1514
#### Streaming API for JSON (StAJ)

doc/Tutorials/Basics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Examples
22

3-
The examples below illustrate the use of the [json](../ref/basic_json.md) class and [json_query](../ref/jsonpath/json_query.md) function.
3+
The examples below illustrate the use of the [json](../ref/corelib/basic_json.md) class and [json_query](../ref/jsonpath/json_query.md) function.
44

55
### json construction
66

doc/ref/bson/basic_bson_cursor.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ Graham Greene
281281

282282
### See also
283283

284-
[staj_event](../basic_staj_event.md)
284+
[staj_event](../corelib/basic_staj_event.md)
285285

286-
[staj_array_iterator](../staj_array_iterator.md)
286+
[staj_array_iterator](../corelib/staj_array_iterator.md)
287287

288-
[staj_object_iterator](../staj_object_iterator.md)
288+
[staj_object_iterator](../corelib/staj_object_iterator.md)
289289

doc/ref/bson/basic_bson_encoder.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,4 @@ Output:
345345

346346
### See also
347347

348-
[byte_string_view](../byte_string_view.md)
348+
[byte_string_view](../corelib/byte_string_view.md)

doc/ref/bson/bson.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
### bson extension
22

33
The bson extension implements decode from and encode to the [Binary JSON](http://bsonspec.org/) data format.
4-
You can either parse into or serialize from a variant-like data structure, [basic_json](../basic_json.md), or your own
5-
data structures, using [json_type_traits](../json_type_traits.md).
4+
You can either parse into or serialize from a variant-like data structure, [basic_json](../corelib/basic_json.md), or your own
5+
data structures, using [json_type_traits](../corelib/json_type_traits.md).
66

7-
[decode_bson](decode_bson.md)
8-
[try_decode_bson](try_decode_bson.md) (1.4.0)
7+
[decode_bson, try_decode_bson](decode_bson.md)
98

109
[basic_bson_cursor](basic_bson_cursor.md)
1110

@@ -292,7 +291,7 @@ Output:
292291
293292
### See also
294293
295-
[byte_string_view](../byte_string_view.md)
294+
[byte_string_view](../corelib/byte_string_view.md)
296295
297296
298297
### Acknowledgements

doc/ref/bson/bson_options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ Constructs a `bson_options` with default values.
2222
void max_nesting_depth(int depth)
2323
The maximum nesting depth allowed when decoding and encoding BSON.
2424
Default is 1024. Parsing can have an arbitrarily large depth
25-
limited only by available memory. Serializing a [basic_json](../basic_json.md) to
25+
limited only by available memory. Serializing a [basic_json](../corelib/basic_json.md) to
2626
BSON is limited by stack size.
2727

doc/ref/bson/decode_bson.md

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,78 @@ template <typename T>
1313
T decode_bson(std::istream& is,
1414
const bson_decode_options& options = bson_decode_options()); (2)
1515

16-
template <typename T,typename BytesLike,typename Allocator,typename TempAllocator>
17-
T decode_bson(const allocator_set<Allocator,TempAllocator>& alloc_set,
16+
template <typename T,typename BytesLike,typename Alloc,typename TempAlloc>
17+
T decode_bson(const allocator_set<Alloc,TempAlloc>& alloc_set,
1818
const BytesLike& source,
1919
const bson_decode_options& options = bson_decode_options()); (3) (since 0.171.0)
2020

21-
template <typename T,typename Allocator,typename TempAllocator>
22-
T decode_bson(const allocator_set<Allocator,TempAllocator>& alloc_set,
21+
template <typename T,typename Alloc,typename TempAlloc>
22+
T decode_bson(const allocator_set<Alloc,TempAlloc>& alloc_set,
2323
std::istream& is,
2424
const bson_decode_options& options = bson_decode_options()); (4) (since 0.171.0)
2525

2626
template <typename T,typename InputIt>
2727
T decode_bson(InputIt first, InputIt last,
2828
const bson_decode_options& options = bson_decode_options()); (5) (since 0.153.0)
29+
30+
template <typename T,typename BytesLike>
31+
read_result<T> try_decode_bson(const BytesLike& source,
32+
const bson_decode_options& options = bson_decode_options()); (6) (since 1.4.0)
33+
34+
template <typename T>
35+
read_result<T> try_decode_bson(std::istream& is,
36+
const bson_decode_options& options = bson_decode_options()); (7) (since 1.4.0)
37+
38+
template <typename T,typename BytesLike,typename Alloc,typename TempAlloc>
39+
read_result<T> try_decode_bson(const allocator_set<Alloc,TempAlloc>& alloc_set,
40+
const BytesLike& source,
41+
const bson_decode_options& options = bson_decode_options()); (8) (since 1.4.0)
42+
43+
template <typename T,typename Alloc,typename TempAlloc>
44+
read_result<T> try_decode_bson(const allocator_set<Alloc,TempAlloc>& alloc_set,
45+
std::istream& is,
46+
const bson_decode_options& options = bson_decode_options()); (9) (since 1.4.0)
47+
48+
template <typename T,typename InputIt>
49+
read_result<T> try_decode_bson(InputIt first, InputIt last,
50+
const bson_decode_options& options = bson_decode_options()); (10) (since 1.4.0)
2951
```
3052
3153
(1) Reads BSON data from a contiguous byte sequence provided by `source` into a type T, using the specified (or defaulted) [options](bson_options.md).
3254
Type `BytesLike` must be a container that has member functions `data()` and `size()`,
3355
and member type `value_type` with size exactly 8 bits (since 0.152.0.)
3456
Any of the values types `int8_t`, `uint8_t`, `char`, `unsigned char` and `std::byte` (since C++17) are allowed.
35-
Type 'T' must be an instantiation of [basic_json](../basic_json.md)
36-
or support [json_type_traits](../json_type_traits.md).
57+
Type 'T' must be an instantiation of [basic_json](../corelib/basic_json.md)
58+
or support jsoncons reflection traits.
3759
3860
(2) Reads BSON data from a binary stream into a type T, using the specified (or defaulted) [options](bson_options.md).
39-
Type 'T' must be an instantiation of [basic_json](../basic_json.md)
40-
or support [json_type_traits](../json_type_traits.md).
61+
Type 'T' must be an instantiation of [basic_json](../corelib/basic_json.md)
62+
or support jsoncons reflection traits.
4163
4264
(3)-(4) are identical to (1)-(2) except an [allocator_set](allocator_set.md) is passed as an additional argument and
4365
provides allocators for result data and temporary allocations.
4466
4567
(5) Reads BSON data from the range [`first`,`last`) into a type T, using the specified (or defaulted) [options](bson_options.md).
46-
Type 'T' must be an instantiation of [basic_json](../basic_json.md)
47-
or support [json_type_traits](../json_type_traits.md).
68+
Type 'T' must be an instantiation of [basic_json](../corelib/basic_json.md)
69+
or support jsoncons reflection traits.
70+
71+
(6)-(10) Non-throwing versions of (1)-(5)
72+
73+
#### Return value
74+
75+
(1)-(5) Deserialized value
76+
77+
(6)-(10) [read_result<T>](../corelib/read_result.md)
4878
4979
#### Exceptions
5080
51-
Throws a [ser_error](../ser_error.md) if parsing fails, and a [conv_error](conv_error.md) if type conversion fails.
81+
(1)-(5) Throw [ser_error](../corelib/ser_error.md) if read fails.
82+
83+
Any overload may throw `std::bad_alloc` if memory allocation fails.
5284
5385
### Examples
5486
55-
#### Binary example
87+
#### Throwing overload
5688
5789
```cpp
5890
#include <jsoncons/json.hpp>
@@ -92,6 +124,53 @@ binary data: 48,65,6c,6c,6f
92124
```
93125
Note that printing a json value by default encodes byte strings as base64url strings, but the json value holds the actual bytes.
94126

127+
#### Non-throwing overload
128+
129+
```cpp
130+
#include <jsoncons/json.hpp>
131+
#include <jsoncons_ext/bson/bson.hpp>
132+
#include <iostream>
133+
134+
namespace bson = jsoncons::bson;
135+
136+
int main()
137+
{
138+
std::vector<uint8_t> input = {0x13, 0x00, 0x00, 0x00, // Document has 19 bytes
139+
0x05, // Binary data
140+
0x70, 0x44, 0x00, // "pD"
141+
0x05, 0x00, 0x00, 0x00, // Length is 5
142+
0x80, // Subtype is 128
143+
0x48, 0x65, 0x6c, 0x6c, 0x6f, // 'H','e','l','l','o'
144+
0x00 // terminating null
145+
};
146+
147+
auto result = bson::try_decode_bson<jsoncons::json>(input);
148+
if (!result)
149+
{
150+
std::cout << result.error() << "\n";
151+
exit(1);
152+
}
153+
jsoncons::json& j(*result);
154+
std::cout << pretty_print(j) << "\n\n";
155+
std::cout << "tag: " << j["pD"].tag() << "\n";
156+
std::cout << "ext_tag: " << j["pD"].ext_tag() << "\n";
157+
auto bytes = j["pD"].as<std::vector<uint8_t>>();
158+
std::cout << "binary data: " << jsoncons::byte_string_view{bytes} << "\n";
159+
}
160+
161+
```
162+
Output:
163+
```
164+
165+
{
166+
"pD": "SGVsbG8"
167+
}
168+
169+
tag: ext
170+
ext_tag: 128
171+
binary data: 48,65,6c,6c,6f
172+
```
173+
95174
### See also
96175
97176
[encode_bson](encode_bson.md) encodes a json value to the [Bin­ary JSON](http://bsonspec.org/) data format.

doc/ref/bson/encode_bson.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@ void encode_bson(const T& jval, std::ostream& os,
1414
const bson_decode_options& options = bson_decode_options()); (2)
1515

1616
template <typename T,typename ByteContainer>
17-
void encode_bson(const allocator_set<Allocator,TempAllocator>& alloc_set,
17+
void encode_bson(const allocator_set<Alloc,TempAlloc>& alloc_set,
1818
const T& jval, ByteContainer& cont,
1919
const bson_decode_options& options = bson_decode_options()); (3) (since 0.171.0)
2020

2121
template <typename T>
22-
void encode_bson(const allocator_set<Allocator,TempAllocator>& alloc_set,
22+
void encode_bson(const allocator_set<Alloc,TempAlloc>& alloc_set,
2323
const T& jval, std::ostream& os,
2424
const bson_decode_options& options = bson_decode_options()); (4) (since 0.171.0)
2525
```
2626
2727
(1) Writes a value of type T into a byte container in the BSON data format, using the specified (or defaulted) [options](bson_options.md).
2828
Type 'T' must be an instantiation of [basic_json](basic_json.md)
29-
or support [json_type_traits](../json_type_traits.md).
29+
or support jsoncons reflection traits.
3030
Type `ByteContainer` must be back insertable and have member type `value_type` with size exactly 8 bits (since 0.152.0.)
3131
Any of the values types `int8_t`, `uint8_t`, `char`, `unsigned char` and `std::byte` (since C++17) are allowed.
3232
3333
(2) Writes a value of type T into a binary stream in the BSON data format, using the specified (or defaulted) [options](bson_options.md).
3434
Type 'T' must be an instantiation of [basic_json](basic_json.md)
35-
or support [json_type_traits](../json_type_traits.md).
35+
or support jsoncons reflection traits.
3636
37-
Functions (3)-(4) are identical to (1)-(2) except an [allocator_set](../allocator_set.md) is passed as an additional argument.
37+
Functions (3)-(4) are identical to (1)-(2) except an [allocator_set](../corelib/allocator_set.md) is passed as an additional argument.
3838
3939
### Examples
4040

doc/ref/bson/try_decode_bson.md

Lines changed: 0 additions & 109 deletions
This file was deleted.

doc/ref/cbor/basic_cbor_cursor.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,9 @@ end_array (n/a)
493493

494494
### See also
495495

496-
[staj_event](../basic_staj_event.md)
496+
[staj_event](../corelib/basic_staj_event.md)
497497

498-
[staj_array_iterator](staj_array_iterator.md)
498+
[staj_array_iterator](../corelib/staj_array_iterator.md)
499499

500-
[staj_object_iterator](staj_object_iterator.md)
500+
[staj_object_iterator](../corelib/staj_object_iterator.md)
501501

doc/ref/cbor/basic_cbor_encoder.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Reset encoder to write a new value to a new sink
7070
void int64_value_with_tag(int64_t value, uint64_t raw_tag); // (since 1.2.0)
7171
7272
73-
#### Inherited from [jsoncons::basic_json_visitor](../basic_json_visitor.md)
73+
#### Inherited from [jsoncons::basic_json_visitor](../corelib/basic_json_visitor.md)
7474
7575
void flush(); (1)
7676
@@ -553,4 +553,4 @@ d9 04 10 82 82 02 03 86 02 04 08 04 10 19 01 00
553553

554554
### See also
555555

556-
[byte_string_view](../byte_string_view.md)
556+
[byte_string_view](../corelib/byte_string_view.md)

0 commit comments

Comments
 (0)