25#ifndef JOIN_DATA_PACK_HPP
26#define JOIN_DATA_PACK_HPP
116 virtual int setInt (int32_t value)
override
118 if (value < -(1 << 15))
121 pack (
static_cast<uint32_t
> (value));
123 else if (value < -(1 << 7))
126 pack (
static_cast<uint16_t
> (value));
128 else if (value < -(1 << 5))
131 pack (
static_cast<uint8_t
> (value));
133 else if (value < (1 << 7))
135 append (
static_cast<uint8_t
> (value));
137 else if (value < (1 << 8))
140 pack (
static_cast<uint8_t
> (value));
142 else if (value < (1 << 16))
145 pack (
static_cast<uint16_t
> (value));
150 pack (
static_cast<uint32_t
> (value));
162 if (value < (1 << 7))
164 append (
static_cast<uint8_t
> (value));
166 else if (value < (1 << 8))
169 pack (
static_cast<uint8_t
> (value));
171 else if (value < (1 << 16))
174 pack (
static_cast<uint16_t
> (value));
191 if (value < -(1LL << 31))
194 pack (
static_cast<uint64_t
> (value));
196 else if (value < -(1LL << 15))
199 pack (
static_cast<uint32_t
> (value));
201 else if (value < -(1LL << 7))
204 pack (
static_cast<uint16_t
> (value));
206 else if (value < -(1LL << 5))
209 pack (
static_cast<uint8_t
> (value));
211 else if (value < (1LL << 7))
213 append (
static_cast<uint8_t
> (value));
215 else if (value < (1LL << 8))
218 pack (
static_cast<uint8_t
> (value));
220 else if (value < (1LL << 16))
223 pack (
static_cast<uint16_t
> (value));
225 else if (value < (1LL << 32))
228 pack (
static_cast<uint32_t
> (value));
233 pack (
static_cast<uint64_t
> (value));
245 if (value < (1ULL << 7))
247 append (
static_cast<uint8_t
> (value));
249 else if (value < (1ULL << 8))
252 pack (
static_cast<uint8_t
> (value));
254 else if (value < (1ULL << 16))
257 pack (
static_cast<uint16_t
> (value));
259 else if (value < (1ULL << 32))
262 pack (
static_cast<uint32_t
> (value));
279 float f =
static_cast<float> (value);
280 if (std::isfinite (value) &&
static_cast<double> (f) == value)
298 virtual int setString (
const std::string& value)
override
300 if (value.size () < 32)
302 append (
static_cast<char> (0xa0 | value.size ()));
304 else if (value.size () < 256)
307 pack (
static_cast<uint8_t
> (value.size ()));
309 else if (value.size () < 65536)
312 pack (
static_cast<uint16_t
> (value.size ()));
317 pack (
static_cast<uint32_t
> (value.size ()));
319 append (value.c_str (),
static_cast<uint32_t
> (value.size ()));
332 append (
static_cast<char> (0x90 | size));
334 else if (size < 65536)
337 pack (
static_cast<uint16_t
> (size));
356 append (
static_cast<char> (0x80 | size));
358 else if (size < 65536)
361 pack (
static_cast<uint16_t
> (size));
391 template <
typename Type>
394 append (
reinterpret_cast<const char*
> (&
swap (value)),
sizeof (value));
475 StringView in (document.c_str (), document.size ());
551 template <
typename ViewType>
556 if (
JOIN_LIKELY (document.peek () == std::char_traits<char>::eof ()))
572 template <
typename ViewType>
575 uint8_t head =
static_cast<uint8_t
> (document.peek ());
603 else if (
isBin (head))
630 template <
typename ViewType>
631 int readNull (ViewType& document,
bool isKey =
false)
643 template <
typename ViewType>
656 template <
typename ViewType>
657 int readTrue (ViewType& document,
bool isKey =
false)
668 template <
typename ViewType>
673 if (document.getIf (0xdd))
677 else if (document.getIf (0xdc))
709 template <
typename ViewType>
714 if (document.getIf (0xdf))
718 else if (document.getIf (0xde))
755 template <
typename ViewType>
758 uint8_t head =
static_cast<uint8_t
> (document.peek ());
801 template <
typename ViewType>
806 if (document.getIf (0xdb))
810 else if (document.getIf (0xda))
814 else if (document.getIf (0xd9))
840 template <
typename ViewType>
845 if (document.getIf (0xc6))
849 else if (document.getIf (0xc5))
853 else if (document.getIf (0xc4))
876 template <
typename ViewType>
879 uint8_t head = document.get ();
883 auto number = uint32_t (head);
886 else if (head >= 0xe0)
888 auto number = int32_t (
static_cast<int8_t
> (head));
891 else if (head == 0xd3)
896 else if (head == 0xcf)
901 else if (head == 0xcb)
906 else if (head == 0xd2)
911 else if (head == 0xce)
916 else if (head == 0xca)
921 else if (head == 0xd1)
926 else if (head == 0xcd)
931 else if (head == 0xd0)
936 else if (head == 0xcc)
953 template <
typename Type,
typename ViewType>
954 std::enable_if_t<std::is_arithmetic<Type>::value, Type>
static unpack (ViewType& document)
957 if (
JOIN_UNLIKELY (document.read (
reinterpret_cast<char*
> (&value), sizeof (value)) !=
sizeof (value)))
959 throw std::range_error (
"not enough data to unpack");
1001 return ((c <= 0x7f) || (c >= 0xe0)) || (c == 0xd0) || (c == 0xd1) || (c == 0xd2);
1011 return (c == 0xcc) || (c == 0xcd) || (c == 0xce);
1041 return (c == 0xca) || (c == 0xcb);
1061 return ((c >= 0xa0) && (c <= 0xbf)) || (c == 0xd9) || (c == 0xda) || (c == 0xdb);
1071 return (c == 0xc4) || (c == 0xc5) || (c == 0xc6);
1081 return ((c >= 0x90) && (c <= 0x9f)) || (c == 0xdc) || (c == 0xdd);
1091 return ((c >= 0x80) && (c <= 0x8f)) || (c == 0xde) || (c == 0xdf);
basic stream view.
Definition view.hpp:373
message pack reader class.
Definition pack.hpp:402
constexpr bool isString(uint8_t c)
check if string.
Definition pack.hpp:1059
int readFalse(ViewType &document, bool isKey=false)
parse a false value.
Definition pack.hpp:644
int deserialize(std::ifstream &document) override
deserialize a document.
Definition pack.hpp:517
int readValue(ViewType &document)
parse value.
Definition pack.hpp:573
PackReader & operator=(const PackReader &other)=delete
copy assignment.
constexpr bool isInt64(uint8_t c)
check if 64 bits integer.
Definition pack.hpp:1019
int deserialize(const char *document, size_t length) override
deserialize a document.
Definition pack.hpp:450
constexpr bool isInt(uint8_t c)
check if 32 bits integer.
Definition pack.hpp:999
constexpr bool isNull(uint8_t c)
check if null.
Definition pack.hpp:969
PackReader(const PackReader &other)=delete
copy constructor.
int readKey(ViewType &document)
parse a key of any type.
Definition pack.hpp:756
int deserialize(std::fstream &document) override
deserialize a document.
Definition pack.hpp:506
constexpr bool isTrue(uint8_t c)
check if true.
Definition pack.hpp:989
int read(ViewType &document)
parse a document.
Definition pack.hpp:552
int deserialize(const char *first, const char *last) override
deserialize a document.
Definition pack.hpp:462
int deserialize(std::iostream &document) override
deserialize a document.
Definition pack.hpp:528
int deserialize(std::stringstream &document) override
deserialize a document.
Definition pack.hpp:484
constexpr bool isObject(uint8_t c)
check if object.
Definition pack.hpp:1089
int readArray(ViewType &document)
parse an array value.
Definition pack.hpp:669
virtual ~PackReader()=default
destroy instance.
constexpr bool isUint(uint8_t c)
check if unsigned 32 bits integer.
Definition pack.hpp:1009
int readTrue(ViewType &document, bool isKey=false)
parse a true value.
Definition pack.hpp:657
constexpr bool isUint64(uint8_t c)
check if unsigned 64 bits integer.
Definition pack.hpp:1029
constexpr bool isFalse(uint8_t c)
check if false.
Definition pack.hpp:979
int readNumber(ViewType &document, bool isKey=false)
parse a number value.
Definition pack.hpp:877
constexpr bool isReal(uint8_t c)
check if real.
Definition pack.hpp:1039
int deserialize(std::istringstream &document) override
deserialize a document.
Definition pack.hpp:495
PackReader(PackReader &&other)=delete
move constructor.
static std::enable_if_t< std::is_arithmetic< Type >::value, Type > unpack(ViewType &document)
unpack value from stream.
Definition pack.hpp:954
int readObject(ViewType &document)
parse an object value.
Definition pack.hpp:710
int readNull(ViewType &document, bool isKey=false)
parse a null value.
Definition pack.hpp:631
int deserialize(const std::string &document) override
deserialize a document.
Definition pack.hpp:473
constexpr bool isNumber(uint8_t c)
check if number.
Definition pack.hpp:1049
constexpr bool isBin(uint8_t c)
check if binary data.
Definition pack.hpp:1069
int readString(ViewType &document, bool isKey=false)
parse a string value.
Definition pack.hpp:802
int deserialize(std::istream &document) override
deserialize a document.
Definition pack.hpp:539
constexpr bool isArray(uint8_t c)
check if array.
Definition pack.hpp:1079
PackReader(Value &root)
default constructor.
Definition pack.hpp:408
int readBin(ViewType &document)
parse binary data.
Definition pack.hpp:841
message pack writer class.
Definition pack.hpp:41
virtual int setNull() override
set null value.
Definition pack.hpp:87
PackWriter(std::ostream &document)
create instance.
Definition pack.hpp:47
virtual int setDouble(double value) override
set real value.
Definition pack.hpp:277
virtual int setString(const std::string &value) override
set string value.
Definition pack.hpp:298
virtual int setInt64(int64_t value) override
set 64 bits integer value.
Definition pack.hpp:189
virtual int setUint(uint32_t value) override
set unsigned integer value.
Definition pack.hpp:160
virtual int setBool(bool value) override
set boolean value.
Definition pack.hpp:98
virtual int setUint64(uint64_t value) override
set unsigned 64 bits integer value.
Definition pack.hpp:243
virtual ~PackWriter()=default
destroy instance.
virtual int startArray(uint32_t size=0) override
start array.
Definition pack.hpp:328
virtual int setKey(const Value &key) override
set key.
Definition pack.hpp:376
void pack(Type value)
pack value into stream.
Definition pack.hpp:392
PackWriter & operator=(const PackWriter &other)=delete
copy assignment.
virtual int setInt(int32_t value) override
set integer value.
Definition pack.hpp:116
virtual int startObject(uint32_t size=0) override
start object.
Definition pack.hpp:352
PackWriter(PackWriter &&other)=delete
move constructor.
PackWriter(const PackWriter &other)=delete
copy constructor.
stream deserializer abstract class.
Definition sax.hpp:462
virtual int stopObject() override
stop object.
Definition sax.hpp:753
virtual int startArray(uint32_t size=0) override
start array.
Definition sax.hpp:655
virtual int setInt64(int64_t value) override
set 64 bits integer value.
Definition sax.hpp:615
virtual int setUint(uint32_t value) override
set unsigned integer value.
Definition sax.hpp:605
virtual int setNull() override
set null value.
Definition sax.hpp:575
virtual int setUint64(uint64_t value) override
set unsigned 64 bits integer value.
Definition sax.hpp:625
virtual int setKey(const Value &key) override
set key.
Definition sax.hpp:743
virtual int setInt(int32_t value) override
set integer value.
Definition sax.hpp:595
virtual int startObject(uint32_t size=0) override
start object.
Definition sax.hpp:706
virtual int setBool(bool value) override
set boolean value.
Definition sax.hpp:585
virtual int stopArray() override
stop array.
Definition sax.hpp:691
virtual int setDouble(double value) override
set real value.
Definition sax.hpp:635
virtual int setString(const std::string &value) override
set string value.
Definition sax.hpp:645
stream serializer abstract class.
Definition sax.hpp:244
int setValue(const Value &value)
set value.
Definition sax.hpp:301
void append(char data) noexcept
append character to output stream in batch.
Definition sax.hpp:392
string view.
Definition view.hpp:83
value class.
Definition value.hpp:64
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
Type & swap(Type &val)
swaps byte orders.
Definition utils.hpp:184
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:150
thread_local std::error_code lastError
last error.
Definition error.cpp:32
disambiguation tag to indicate that the contained object should be constructed in-place.
Definition traits.hpp:57
#define JOIN_LIKELY(x)
Definition utils.hpp:46
#define JOIN_UNLIKELY(x)
Definition utils.hpp:47