Skip to content

Commit 035f441

Browse files
committed
Merge branch 'staj_cursor' of https://github.com/danielaparker/jsoncons
2 parents 7c51d5a + 73cf10d commit 035f441

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ Performance Enhancement:
66
- Use `std::from_chars` for chars to double conversion when
77
supported in GCC and VC.
88

9+
Enhancements:
10+
11+
- Added a `size()` accessor function to `basic_staj_event`.
12+
If the event type is a `semantic_tag::key` or a `semantic_tag::string_value` or a `semantic_tag::byte_string_value`,
13+
returns the size of the key or string or byte string value.
14+
If the event type is a `semantic_tag::begin_object` or a `semantic_tag::begin_array`, returns the size of the object
15+
or array if known, otherwise 0.
16+
For all other event types, returns 0.
17+
918
Changes:
1019

1120
- For consistency with library naming conventions, the directory

doc/ref/basic_staj_event.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ If `tag()` == `semantic_tag::ext`, returns a format specific tag associated with
4848
otherwise return 0. An example is a MessagePack `type` in the range 0-127 associated with the
4949
MessagePack ext format family, or a CBOR tag preceeding a byte string.
5050
51+
size_t size() const
52+
If `event_type()` is a `staj_event_type::key` or a `staj_event_type::string_value` or a `staj_event_type::byte_string_value`,
53+
returns the size of the key or string or byte string value.
54+
If `event_type()` is a `staj_event_type::begin_object` or a `staj_event_type::begin_array`, returns the size of the object
55+
or array if known, otherwise 0.
56+
For all other event types, returns 0.
57+
5158
template <class T, class... Args>
5259
T get() const;
5360
Attempts to convert the json value to the template value type.

include/jsoncons/staj_cursor.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ class basic_staj_event
160160
{
161161
}
162162

163+
basic_staj_event(staj_event_type event_type, std::size_t length, semantic_tag tag = semantic_tag::none)
164+
: event_type_(event_type), tag_(tag), ext_tag_(0), value_(), length_(length)
165+
{
166+
}
167+
163168
basic_staj_event(null_type, semantic_tag tag)
164169
: event_type_(staj_event_type::null_value), tag_(tag), ext_tag_(0), value_(), length_(0)
165170
{
@@ -219,6 +224,11 @@ class basic_staj_event
219224
value_.byte_string_data_ = s.data();
220225
}
221226

227+
std::size_t size() const
228+
{
229+
return length_;
230+
}
231+
222232
template <class T>
223233
T get() const
224234
{
@@ -994,6 +1004,12 @@ class basic_staj_visitor : public basic_json_visitor<CharT>
9941004
return !pred_(event_, context);
9951005
}
9961006

1007+
bool visit_begin_object(std::size_t length, semantic_tag tag, const ser_context& context, std::error_code&) override
1008+
{
1009+
event_ = basic_staj_event<CharT>(staj_event_type::begin_object, length, tag);
1010+
return !pred_(event_, context);
1011+
}
1012+
9971013
bool visit_end_object(const ser_context& context, std::error_code&) override
9981014
{
9991015
event_ = basic_staj_event<CharT>(staj_event_type::end_object);
@@ -1006,6 +1022,12 @@ class basic_staj_visitor : public basic_json_visitor<CharT>
10061022
return !pred_(event_, context);
10071023
}
10081024

1025+
bool visit_begin_array(std::size_t length, semantic_tag tag, const ser_context& context, std::error_code&) override
1026+
{
1027+
event_ = basic_staj_event<CharT>(staj_event_type::begin_array, length, tag);
1028+
return !pred_(event_, context);
1029+
}
1030+
10091031
bool visit_end_array(const ser_context& context, std::error_code&) override
10101032
{
10111033
event_ = basic_staj_event<CharT>(staj_event_type::end_array);

test/cbor/src/cbor_cursor_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ TEST_CASE("cbor_cursor reputon test")
4040
cbor::cbor_bytes_cursor cursor(data);
4141

4242
CHECK(cursor.current().event_type() == staj_event_type::begin_object);
43+
CHECK(cursor.current().size() == 2);
4344
cursor.next();
4445
CHECK(cursor.current().event_type() == staj_event_type::key);
4546
cursor.next();
@@ -48,6 +49,7 @@ TEST_CASE("cbor_cursor reputon test")
4849
CHECK(cursor.current().event_type() == staj_event_type::key);
4950
cursor.next();
5051
CHECK(cursor.current().event_type() == staj_event_type::begin_array);
52+
CHECK(cursor.current().size() == 1);
5153
cursor.next();
5254
CHECK(cursor.current().event_type() == staj_event_type::begin_object);
5355
cursor.next();

0 commit comments

Comments
 (0)