simdjson  3.11.0
Ridiculously Fast JSON
json_type-inl.h
1 #ifndef SIMDJSON_GENERIC_ONDEMAND_JSON_TYPE_INL_H
2 
3 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
4 #define SIMDJSON_GENERIC_ONDEMAND_JSON_TYPE_INL_H
5 #include "simdjson/generic/ondemand/base.h"
6 #include "simdjson/generic/ondemand/json_type.h"
7 #include "simdjson/generic/implementation_simdjson_result_base-inl.h"
8 #endif // SIMDJSON_CONDITIONAL_INCLUDE
9 
10 namespace simdjson {
11 namespace SIMDJSON_IMPLEMENTATION {
12 namespace ondemand {
13 
14 inline std::ostream& operator<<(std::ostream& out, json_type type) noexcept {
15  switch (type) {
16  case json_type::array: out << "array"; break;
17  case json_type::object: out << "object"; break;
18  case json_type::number: out << "number"; break;
19  case json_type::string: out << "string"; break;
20  case json_type::boolean: out << "boolean"; break;
21  case json_type::null: out << "null"; break;
22  default: SIMDJSON_UNREACHABLE();
23  }
24  return out;
25 }
26 
27 #if SIMDJSON_EXCEPTIONS
28 inline std::ostream& operator<<(std::ostream& out, simdjson_result<json_type> &type) noexcept(false) {
29  return out << type.value();
30 }
31 #endif
32 
33 
34 
35 simdjson_inline number_type number::get_number_type() const noexcept {
36  return type;
37 }
38 
39 simdjson_inline bool number::is_uint64() const noexcept {
40  return get_number_type() == number_type::unsigned_integer;
41 }
42 
43 simdjson_inline uint64_t number::get_uint64() const noexcept {
44  return payload.unsigned_integer;
45 }
46 
47 simdjson_inline number::operator uint64_t() const noexcept {
48  return get_uint64();
49 }
50 
51 simdjson_inline bool number::is_int64() const noexcept {
52  return get_number_type() == number_type::signed_integer;
53 }
54 
55 simdjson_inline int64_t number::get_int64() const noexcept {
56  return payload.signed_integer;
57 }
58 
59 simdjson_inline number::operator int64_t() const noexcept {
60  return get_int64();
61 }
62 
63 simdjson_inline bool number::is_double() const noexcept {
64  return get_number_type() == number_type::floating_point_number;
65 }
66 
67 simdjson_inline double number::get_double() const noexcept {
68  return payload.floating_point_number;
69 }
70 
71 simdjson_inline number::operator double() const noexcept {
72  return get_double();
73 }
74 
75 simdjson_inline double number::as_double() const noexcept {
76  if(is_double()) {
77  return payload.floating_point_number;
78  }
79  if(is_int64()) {
80  return double(payload.signed_integer);
81  }
82  return double(payload.unsigned_integer);
83 }
84 
85 simdjson_inline void number::append_s64(int64_t value) noexcept {
86  payload.signed_integer = value;
87  type = number_type::signed_integer;
88 }
89 
90 simdjson_inline void number::append_u64(uint64_t value) noexcept {
91  payload.unsigned_integer = value;
92  type = number_type::unsigned_integer;
93 }
94 
95 simdjson_inline void number::append_double(double value) noexcept {
96  payload.floating_point_number = value;
97  type = number_type::floating_point_number;
98 }
99 
100 simdjson_inline void number::skip_double() noexcept {
101  type = number_type::floating_point_number;
102 }
103 
104 } // namespace ondemand
105 } // namespace SIMDJSON_IMPLEMENTATION
106 } // namespace simdjson
107 
108 namespace simdjson {
109 
110 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_type>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::json_type &&value) noexcept
111  : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::json_type>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::json_type>(value)) {}
112 simdjson_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_type>::simdjson_result(error_code error) noexcept
113  : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::json_type>(error) {}
114 
115 } // namespace simdjson
116 
117 #endif // SIMDJSON_GENERIC_ONDEMAND_JSON_TYPE_INL_H
An ephemeral JSON value returned during iteration.
Definition: value.h:21
std::ostream & operator<<(std::ostream &out, json_type type) noexcept
Write the JSON type to the output stream.
Definition: json_type-inl.h:14
json_type
The type of a JSON value.
Definition: json_type.h:17
@ object
A JSON object ( { "a": 1, "b" 2, ... } )
@ number
A JSON number ( 1 or -2.3 or 4.5e6 ...)
@ string
A JSON string ( "a" or "hello world\n" ...)
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
simdjson_inline bool is_int64() const noexcept
return true if the automatically determined type of the number is number_type::signed_integer.
Definition: json_type-inl.h:51
simdjson_inline double as_double() const noexcept
Convert the number to a double.
Definition: json_type-inl.h:75
simdjson_inline bool is_double() const noexcept
return true if the automatically determined type of the number is number_type::floating_point_number.
Definition: json_type-inl.h:63
simdjson_inline ondemand::number_type get_number_type() const noexcept
return the automatically determined type of the number: number_type::floating_point_number,...
Definition: json_type-inl.h:35
simdjson_inline int64_t get_int64() const noexcept
return the value as a int64_t, only valid if is_int64() is true.
Definition: json_type-inl.h:55
union simdjson::SIMDJSON_IMPLEMENTATION::ondemand::number::@0 payload
End of friend declarations.
simdjson_inline void append_u64(uint64_t value) noexcept
Store an unsigned 64-bit value to the number.
Definition: json_type-inl.h:90
simdjson_inline double get_double() const noexcept
return the value as a double, only valid if is_double() is true.
Definition: json_type-inl.h:67
simdjson_inline void skip_double() noexcept
Specifies that the value is a double, but leave it undefined.
simdjson_inline void append_s64(int64_t value) noexcept
Store a signed 64-bit value to the number.
Definition: json_type-inl.h:85
simdjson_inline void append_double(double value) noexcept
Store a double value to the number.
Definition: json_type-inl.h:95
simdjson_inline bool is_uint64() const noexcept
return true if the automatically determined type of the number is number_type::unsigned_integer.
Definition: json_type-inl.h:39
simdjson_inline uint64_t get_uint64() const noexcept
return the value as a uint64_t, only valid if is_uint64() is true.
Definition: json_type-inl.h:43
The result of a simdjson operation that could fail.
Definition: error.h:215