simdjson  3.11.0
Ridiculously Fast JSON
json_type.h
1 #ifndef SIMDJSON_GENERIC_ONDEMAND_JSON_TYPE_H
2 
3 #ifndef SIMDJSON_CONDITIONAL_INCLUDE
4 #define SIMDJSON_GENERIC_ONDEMAND_JSON_TYPE_H
5 #include "simdjson/generic/ondemand/base.h"
6 #include "simdjson/generic/implementation_simdjson_result_base.h"
7 #include "simdjson/generic/numberparsing.h"
8 #endif // SIMDJSON_CONDITIONAL_INCLUDE
9 
10 namespace simdjson {
11 namespace SIMDJSON_IMPLEMENTATION {
12 namespace ondemand {
13 
17 enum class json_type {
18  // Start at 1 to catch uninitialized / default values more easily
19  array=1,
20  object,
21  number,
22  string,
23  boolean,
24  null
25 };
26 
32 struct number {
33 
45  simdjson_inline ondemand::number_type get_number_type() const noexcept;
50  simdjson_inline bool is_uint64() const noexcept;
54  simdjson_inline uint64_t get_uint64() const noexcept;
55  simdjson_inline operator uint64_t() const noexcept;
56 
61  simdjson_inline bool is_int64() const noexcept;
65  simdjson_inline int64_t get_int64() const noexcept;
66  simdjson_inline operator int64_t() const noexcept;
67 
68 
73  simdjson_inline bool is_double() const noexcept;
77  simdjson_inline double get_double() const noexcept;
78  simdjson_inline operator double() const noexcept;
79 
84  simdjson_inline double as_double() const noexcept;
85 
86 
87 protected:
93  friend class value_iterator;
94  template<typename W>
95  friend error_code numberparsing::write_float(const uint8_t *const src, bool negative, uint64_t i, const uint8_t * start_digits, size_t digit_count, int64_t exponent, W &writer);
96  template<typename W>
97  friend error_code numberparsing::parse_number(const uint8_t *const src, W &writer);
99  simdjson_inline void append_s64(int64_t value) noexcept;
101  simdjson_inline void append_u64(uint64_t value) noexcept;
103  simdjson_inline void append_double(double value) noexcept;
105  simdjson_inline void skip_double() noexcept;
114  union {
115  double floating_point_number;
116  int64_t signed_integer;
117  uint64_t unsigned_integer;
118  } payload{0};
119  number_type type{number_type::signed_integer};
120 };
121 
128 inline std::ostream& operator<<(std::ostream& out, json_type type) noexcept;
129 
130 #if SIMDJSON_EXCEPTIONS
140 inline std::ostream& operator<<(std::ostream& out, simdjson_result<json_type> &type) noexcept(false);
141 #endif
142 
143 } // namespace ondemand
144 } // namespace SIMDJSON_IMPLEMENTATION
145 } // namespace simdjson
146 
147 namespace simdjson {
148 
149 template<>
150 struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::json_type> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::json_type> {
151 public:
153  simdjson_inline simdjson_result(error_code error) noexcept;
154  simdjson_inline simdjson_result() noexcept = default;
155  simdjson_inline ~simdjson_result() noexcept = default;
156 };
157 
158 } // namespace simdjson
159 
160 #endif // SIMDJSON_GENERIC_ONDEMAND_JSON_TYPE_H
An ephemeral JSON value returned during iteration.
Definition: value.h:21
simdjson::SIMDJSON_IMPLEMENTATION::number_type number_type
Definition: base.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
A type representing a JSON number.
Definition: json_type.h:32
friend class value_iterator
The next block of declaration is designed so that we can call the number parsing functions on a numbe...
Definition: json_type.h:93
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
simdjson_inline error_code error() const noexcept
The error.
Definition: error-inl.h:131
simdjson_inline T & value() &noexcept(false)
Get the result value.