1 #ifndef ZSERIO_JSON_DECODER_H_INC 2 #define ZSERIO_JSON_DECODER_H_INC 22 template <
typename ALLOC = std::allocator<u
int8_t>>
51 DecoderResult(
size_t numRead, T&& decodedValue,
const ALLOC& allocator) :
64 DecoderResult(
size_t numRead, T&& decodedValue,
bool overflow,
const ALLOC& allocator) :
76 AnyHolder<ALLOC> createValue(T&& decodedValue,
bool overflow,
const ALLOC& allocator)
114 return decodeLiteral(input,
"null"_sv,
nullptr);
116 return decodeLiteral(input,
"true"_sv,
true);
118 return decodeLiteral(input,
"false"_sv,
false);
120 return decodeLiteral(input,
"NaN"_sv, static_cast<double>(NAN));
122 return decodeLiteral(input,
"Infinity"_sv, static_cast<double>(INFINITY));
124 return decodeString(input);
126 if (input.
size() > 1 && input[1] ==
'I')
127 return decodeLiteral(input,
"-Infinity"_sv, -static_cast<double>(INFINITY));
128 return decodeNumber(input);
130 return decodeNumber(input);
135 template <
typename T>
140 static char decodeHex(
char ch);
141 size_t checkNumber(
StringView input,
bool& isDouble,
bool& isSigned);
148 template <
typename ALLOC>
149 template <
typename T>
155 while (inputIt != input.
end() && literalIt != literal.
end())
157 if (*inputIt++ != *literalIt++)
164 if (literalIt != literal.
end())
174 template <
typename ALLOC>
180 while (inputIt != input.
end())
182 if (*inputIt ==
'\\')
185 if (inputIt == input.
end())
191 char nextChar = *inputIt;
196 value.push_back(nextChar);
200 value.push_back(
'\b');
204 value.push_back(
'\f');
208 value.push_back(
'\n');
212 value.push_back(
'\r');
216 value.push_back(
'\t');
222 if (!decodeUnicodeEscape(input, inputIt, value))
235 else if (*inputIt ==
'"')
244 value.push_back(*inputIt++);
252 template <
typename ALLOC>
258 if (inputIt == input.
end() || *inputIt++ !=
'0')
260 if (inputIt == input.
end() || *inputIt++ !=
'0')
263 if (inputIt == input.
end())
265 const char ch1 = decodeHex(*inputIt++);
269 if (inputIt == input.
end())
271 const char ch2 = decodeHex(*inputIt++);
275 value.push_back(static_cast<char>((static_cast<uint32_t>(ch1) << 4U) | static_cast<uint32_t>(ch2)));
279 template <
typename ALLOC>
282 if (ch >=
'0' && ch <=
'9')
283 return static_cast<char>(ch -
'0');
284 else if (ch >=
'a' && ch <=
'f')
285 return static_cast<char>(ch -
'a' + 10);
286 else if (ch >=
'A' && ch <=
'F')
287 return static_cast<char>(ch -
'A' + 10);
292 template <
typename ALLOC>
296 bool acceptExpSign =
false;
309 while (inputIt != input.
end())
313 acceptExpSign =
false;
314 if (*inputIt ==
'+' || *inputIt ==
'-')
320 if (*inputIt >=
'0' && *inputIt <=
'9')
325 if (!isDouble && (*inputIt ==
'.' || *inputIt ==
'e' || *inputIt ==
'E'))
328 if (*inputIt ==
'e' || *inputIt ==
'E')
329 acceptExpSign =
true;
337 const size_t numberLen =
static_cast<size_t>(inputIt - input.
begin());
338 if (isSigned && numberLen == 1)
344 template <
typename ALLOC>
347 bool isDouble =
false;
348 bool isSigned =
false;
349 const size_t numChars = checkNumber(input, isDouble, isSigned);
356 return decodeDouble(input, numChars);
358 return decodeSigned(input);
360 return decodeUnsigned(input);
363 template <
typename ALLOC>
366 char* pEnd =
nullptr;
368 const int64_t value = std::strtoll(input.
begin(), &pEnd, 10);
370 const bool overflow = (errno == ERANGE);
375 template <
typename ALLOC>
378 char* pEnd =
nullptr;
380 const uint64_t value = std::strtoull(input.
begin(), &pEnd, 10);
382 const bool overflow = (errno == ERANGE);
387 template <
typename ALLOC>
391 char* pEnd =
nullptr;
392 const double value = std::strtod(input.
begin(), &pEnd);
393 if (static_cast<size_t>(pEnd - input.
begin()) != numChars)
401 #endif // ZSERIO_JSON_DECODER_H_INC constexpr size_type size() const noexcept
BasicJsonDecoder(const ALLOC &allocator)
DecoderResult decodeValue(StringView input)
constexpr const_iterator end() const noexcept
const_pointer const_iterator
DecoderResult(size_t numRead, T &&decodedValue, bool overflow, const ALLOC &allocator)
DecoderResult(size_t numRead, T &&decodedValue, const ALLOC &allocator)
allocator_type get_allocator() const
constexpr const_iterator begin() const noexcept
DecoderResult(size_t numRead, const ALLOC &allocator)
std::basic_string< char, std::char_traits< char >, RebindAlloc< ALLOC, char >> string
constexpr bool empty() const noexcept