simdjson 5.0.1
Ridiculously Fast JSON
Loading...
Searching...
No Matches
field-inl.h
1#ifndef SIMDJSON_GENERIC_ONDEMAND_FIELD_INL_H
2
3#ifndef SIMDJSON_CONDITIONAL_INCLUDE
4#define SIMDJSON_GENERIC_ONDEMAND_FIELD_INL_H
5#include "simdjson/generic/ondemand/base.h"
6#include "simdjson/generic/ondemand/field.h"
7#include "simdjson/generic/ondemand/value-inl.h"
8#include "simdjson/generic/ondemand/value_iterator-inl.h"
9#endif // SIMDJSON_CONDITIONAL_INCLUDE
10
11namespace simdjson {
12namespace SIMDJSON_IMPLEMENTATION {
13namespace ondemand {
14
15// clang 6 does not think the default constructor can be noexcept, so we make it explicit
16simdjson_inline field::field() noexcept : std::pair<raw_json_string, ondemand::value>() {}
17
18simdjson_inline field::field(raw_json_string key, ondemand::value &&value) noexcept
19 : std::pair<raw_json_string, ondemand::value>(key, std::forward<ondemand::value>(value))
20{
21}
22
23simdjson_inline simdjson_result<field> field::start(value_iterator &parent_iter) noexcept {
24 raw_json_string key;
25 SIMDJSON_TRY( parent_iter.field_key().get(key) );
26 SIMDJSON_TRY( parent_iter.field_value() );
27 return field::start(parent_iter, key);
28}
29
30simdjson_inline simdjson_result<field> field::start(const value_iterator &parent_iter, raw_json_string key) noexcept {
31 return field(key, parent_iter.child());
32}
33
34simdjson_inline simdjson_warn_unused simdjson_result<std::string_view> field::unescaped_key(bool allow_replacement) noexcept {
35 SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() but Visual Studio won't let us.
36 simdjson_result<std::string_view> answer = first.unescape(second.iter.json_iter(), allow_replacement);
37 first.consume();
38 return answer;
39}
40
41#if SIMDJSON_SUPPORTS_CHAR8_T
42simdjson_inline simdjson_warn_unused simdjson_result<std::u8string_view> field::unescaped_u8key(bool allow_replacement) noexcept {
43 std::string_view key;
44 SIMDJSON_TRY( unescaped_key(allow_replacement).get(key) );
45 return internal::as_u8string_view(key);
46}
47#endif // SIMDJSON_SUPPORTS_CHAR8_T
48
49template <typename string_type>
50simdjson_inline simdjson_warn_unused error_code field::unescaped_key(string_type& receiver, bool allow_replacement) noexcept {
51 std::string_view key;
52 SIMDJSON_TRY( unescaped_key(allow_replacement).get(key) );
53 internal::assign_utf8(receiver, key);
54 return SUCCESS;
55}
56
57simdjson_inline raw_json_string field::key() const noexcept {
58 SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() by Visual Studio won't let us.
59 return first;
60}
61
62
63simdjson_inline std::string_view field::key_raw_json_token() const noexcept {
64 SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() by Visual Studio won't let us.
65 return std::string_view(reinterpret_cast<const char*>(first.buf-1), second.iter._json_iter->token.peek(-1) - first.buf + 1);
66}
67
68simdjson_inline std::string_view field::escaped_key() const noexcept {
69 SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() by Visual Studio won't let us.
70 auto end_quote = second.iter._json_iter->token.peek(-1);
71 while(*end_quote != '"') end_quote--;
72 return std::string_view(reinterpret_cast<const char*>(first.buf), end_quote - first.buf);
73}
74
75#if SIMDJSON_SUPPORTS_CHAR8_T
76simdjson_inline std::u8string_view field::escaped_u8key() const noexcept {
77 return internal::as_u8string_view(escaped_key());
78}
79#endif // SIMDJSON_SUPPORTS_CHAR8_T
80
81simdjson_inline value &field::value() & noexcept {
82 return second;
83}
84
85simdjson_inline value field::value() && noexcept {
86 return std::forward<field>(*this).second;
87}
88
89} // namespace ondemand
90} // namespace SIMDJSON_IMPLEMENTATION
91} // namespace simdjson
92
93namespace simdjson {
94
95simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field>::simdjson_result(
96 SIMDJSON_IMPLEMENTATION::ondemand::field &&value
97) noexcept :
98 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::field>(
99 std::forward<SIMDJSON_IMPLEMENTATION::ondemand::field>(value)
100 )
101{
102}
103simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field>::simdjson_result(
104 error_code error
105) noexcept :
106 implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::field>(error)
107{
108}
109
110simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::raw_json_string> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field>::key() noexcept {
111 if (error()) { return error(); }
112 return first.key();
113}
114
115simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field>::key_raw_json_token() noexcept {
116 if (error()) { return error(); }
117 return first.key_raw_json_token();
118}
119
120simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field>::escaped_key() noexcept {
121 if (error()) { return error(); }
122 return first.escaped_key();
123}
124
125#if SIMDJSON_SUPPORTS_CHAR8_T
126simdjson_inline simdjson_result<std::u8string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field>::escaped_u8key() noexcept {
127 if (error()) { return error(); }
128 return first.escaped_u8key();
129}
130#endif // SIMDJSON_SUPPORTS_CHAR8_T
131
132simdjson_inline simdjson_result<std::string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field>::unescaped_key(bool allow_replacement) noexcept {
133 if (error()) { return error(); }
134 return first.unescaped_key(allow_replacement);
135}
136
137#if SIMDJSON_SUPPORTS_CHAR8_T
138simdjson_inline simdjson_result<std::u8string_view> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field>::unescaped_u8key(bool allow_replacement) noexcept {
139 if (error()) { return error(); }
140 return first.unescaped_u8key(allow_replacement);
141}
142#endif // SIMDJSON_SUPPORTS_CHAR8_T
143
144template<typename string_type>
145simdjson_warn_unused simdjson_inline error_code simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field>::unescaped_key(string_type &receiver, bool allow_replacement) noexcept {
146 if (error()) { return error(); }
147 return first.unescaped_key(receiver, allow_replacement);
148}
149
150simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::value> simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::field>::value() noexcept {
151 if (error()) { return error(); }
152 return std::move(first.value());
153}
154
155} // namespace simdjson
156
157#endif // SIMDJSON_GENERIC_ONDEMAND_FIELD_INL_H
simdjson_inline std::string_view key_raw_json_token() const noexcept
Get the unprocessed key as a string_view.
Definition field-inl.h:63
simdjson_inline simdjson_warn_unused simdjson_result< std::string_view > unescaped_key(bool allow_replacement=false) noexcept
Get the key as a string_view (for higher speed, consider raw_key).
Definition field-inl.h:34
simdjson_inline raw_json_string key() const noexcept
Get the key as a raw_json_string.
Definition field-inl.h:57
simdjson_inline std::string_view escaped_key() const noexcept
Get the key as a string_view.
Definition field-inl.h:68
simdjson_inline field() noexcept
Create a new invalid field.
Definition field-inl.h:16
simdjson_inline ondemand::value & value() &noexcept
Get the field value.
Definition field-inl.h:81
A string escaped per JSON rules, terminated with quote (").
An ephemeral JSON value returned during iteration.
Definition value.h:22
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8
error_code
All possible errors returned by simdjson.
Definition error.h:19
@ SUCCESS
No error.
Definition error.h:20
The result of a simdjson operation that could fail.
Definition error.h:281
simdjson_inline T & value() &noexcept(false)
Get the result value.
Definition error-inl.h:175