join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
sax.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_SAX_HPP__
26#define __JOIN_SAX_HPP__
27
28// libjoin.
29#include <join/error.hpp>
30#include <join/value.hpp>
31#include <join/utils.hpp>
32#include <join/view.hpp>
33
34// C++.
35#include <system_error>
36#include <iostream>
37#include <fstream>
38#include <sstream>
39#include <memory>
40#include <string>
41#include <stack>
42
43// C.
44#include <cstddef>
45
46namespace join
47{
49 class Value;
50
54 enum class SaxErrc
55 {
56 StackOverflow = 1,
60 };
61
65 class SaxCategory : public std::error_category
66 {
67 public:
72 virtual const char* name () const noexcept;
73
79 virtual std::string message (int code) const;
80 };
81
86 const std::error_category& saxCategory () noexcept;
87
93 std::error_code make_error_code (SaxErrc code) noexcept;
94
100 std::error_condition make_error_condition (SaxErrc code) noexcept;
101
106 {
107 public:
111 SaxHandler () = default;
112
117 SaxHandler (const SaxHandler& other) = default;
118
124 SaxHandler& operator= (const SaxHandler& other) = default;
125
130 SaxHandler (SaxHandler&& other) = default;
131
137 SaxHandler& operator= (SaxHandler&& other) = default;
138
142 virtual ~SaxHandler () = default;
143
148 virtual int setNull () = 0;
149
155 virtual int setBool (bool value) = 0;
156
162 virtual int setInt (int32_t value) = 0;
163
169 virtual int setUint (uint32_t value) = 0;
170
176 virtual int setInt64 (int64_t value) = 0;
177
183 virtual int setUint64 (uint64_t value) = 0;
184
190 virtual int setDouble (double value) = 0;
191
197 virtual int setString (const std::string& value) = 0;
198
204 virtual int startArray (uint32_t size = 0) = 0;
205
210 virtual int stopArray ()
211 {
212 return 0;
213 }
214
220 virtual int startObject (uint32_t size = 0) = 0;
221
227 virtual int setKey (const std::string& key) = 0;
228
233 virtual int stopObject ()
234 {
235 return 0;
236 }
237 };
238
243 {
244 public:
249 StreamWriter (std::ostream& document)
250 : SaxHandler ()
251 , _out (document.rdbuf ())
252 {
253 }
254
259 StreamWriter (const StreamWriter& other) = delete;
260
265 StreamWriter& operator= (const StreamWriter& other) = delete;
266
271 StreamWriter (StreamWriter&& other) = delete;
272
278
282 virtual ~StreamWriter () = default;
283
289 virtual int serialize (const Value& value)
290 {
291 return setValue (value);
292 }
293
294 protected:
300 int setValue (const Value& value)
301 {
302 switch (value.index ())
303 {
304 case Value::Boolean:
305 return setBool (value.get <Value::Boolean> ());
306
307 case Value::Integer:
308 return setInt (value.get <Value::Integer> ());
309
310 case Value::Unsigned:
311 return setUint (value.get <Value::Unsigned> ());
312
313 case Value::Integer64:
314 return setInt64 (value.get <Value::Integer64> ());
315
317 return setUint64 (value.get <Value::Unsigned64> ());
318
319 case Value::Real:
320 return setDouble (value.get <Value::Real> ());
321
322 case Value::String:
323 return setString (value.get <Value::String> ());
324
326 return setArray (value.get <Value::ArrayValue> ());
327
329 return setObject (value.get <Value::ObjectValue> ());
330
331 default:
332 return setNull ();
333 }
334 }
335
341 virtual int setArray (const Array& array)
342 {
343 startArray (array.size ());
344 for (auto const& element : array)
345 {
346 setValue (element);
347 }
348 stopArray ();
349 return 0;
350 }
351
357 virtual int setObject (const Object& object)
358 {
359 startObject (object.size ());
360 for (auto const& member : object)
361 {
362 setKey (member.first);
363 setValue (member.second);
364 }
365 stopObject ();
366 return 0;
367 }
368
373 inline void append (char data) noexcept
374 {
375 _out->sputc (data);
376 }
377
382 inline void append2 (const char* data) noexcept
383 {
384 _out->sputc (data[0]);
385 _out->sputc (data[1]);
386 }
387
392 inline void append3 (const char* data) noexcept
393 {
394 _out->sputc (data[0]);
395 _out->sputc (data[1]);
396 _out->sputc (data[2]);
397 }
398
403 inline void append4 (const char* data) noexcept
404 {
405 _out->sputc (data[0]);
406 _out->sputc (data[1]);
407 _out->sputc (data[2]);
408 _out->sputc (data[3]);
409 }
410
415 inline void append5 (const char* data) noexcept
416 {
417 _out->sputc (data[0]);
418 _out->sputc (data[1]);
419 _out->sputc (data[2]);
420 _out->sputc (data[3]);
421 _out->sputc (data[4]);
422 }
423
429 inline void append (const char* data, uint32_t size) noexcept
430 {
431 _out->sputn (data, size);
432 }
433
434 protected:
436 std::streambuf* _out;
437 };
438
442 class StreamReader : protected SaxHandler
443 {
444 public:
450 : SaxHandler ()
451 , _root (root)
452 {
453 }
454
459 StreamReader (const StreamReader& other) = delete;
460
466 StreamReader& operator= (const StreamReader& other) = delete;
467
472 StreamReader (StreamReader&& other) = delete;
473
480
484 virtual ~StreamReader () = default;
485
492 virtual int deserialize (const char* document, size_t length) = 0;
493
500 virtual int deserialize (const char* first, const char* last) = 0;
501
507 virtual int deserialize (const std::string& document) = 0;
508
514 virtual int deserialize (std::stringstream& document) = 0;
515
521 virtual int deserialize (std::istringstream& document) = 0;
522
528 virtual int deserialize (std::fstream& document) = 0;
529
535 virtual int deserialize (std::ifstream& document) = 0;
536
542 virtual int deserialize (std::iostream& document) = 0;
543
549 virtual int deserialize (std::istream& document) = 0;
550
551 protected:
556 virtual int setNull () override
557 {
558 return setValue (Value (in_place_index_t <Value::Null> {}, nullptr));
559 }
560
566 virtual int setBool (bool value) override
567 {
568 return setValue (Value (in_place_index_t <Value::Boolean> {}, value));
569 }
570
576 virtual int setInt (int32_t value) override
577 {
578 return setValue (Value (in_place_index_t <Value::Integer> {}, value));
579 }
580
586 virtual int setUint (uint32_t value) override
587 {
588 return setValue (Value (in_place_index_t <Value::Unsigned> {}, value));
589 }
590
596 virtual int setInt64 (int64_t value) override
597 {
598 return setValue (Value (in_place_index_t <Value::Integer64> {}, value));
599 }
600
606 virtual int setUint64 (uint64_t value) override
607 {
608 return setValue (Value (in_place_index_t <Value::Unsigned64> {}, value));
609 }
610
616 virtual int setDouble (double value) override
617 {
618 return setValue (Value (in_place_index_t <Value::Real> {}, value));
619 }
620
626 virtual int setString (const std::string& value) override
627 {
628 return setValue (Value (in_place_index_t <Value::String> {}, value));
629 }
630
636 virtual int startArray (uint32_t size = 0) override
637 {
638 Array array;
639 array.reserve (size ? size : 8);
640
641 if (JOIN_UNLIKELY (_stack.empty ()))
642 {
643 _root = std::move (array);
644 _stack.push (&_root);
645 return 0;
646 }
647
648 if (JOIN_UNLIKELY (_stack.size () >= _maxdepth))
649 {
651 return -1;
652 }
653
654 Value::Ptr parent = _stack.top ();
655
656 if (parent->index () == Value::ObjectValue)
657 {
658 _stack.push (&parent->insert ({std::move (_curkey), std::move (array)}));
659 }
660 else
661 {
662 _stack.push (&parent->pushBack (std::move (array)));
663 }
664
665 return 0;
666 }
667
672 virtual int stopArray () override
673 {
674 if (JOIN_LIKELY (_stack.size ()))
675 {
676 _stack.pop ();
677 }
678
679 return 0;
680 }
681
687 virtual int startObject (uint32_t size = 0) override
688 {
689 Object object;
690 object.reserve (size ? size : 8);
691
692 if (JOIN_UNLIKELY (_stack.empty ()))
693 {
694 _root = std::move (object);
695 _stack.push (&_root);
696 return 0;
697 }
698
699 if (JOIN_UNLIKELY (_stack.size () >= _maxdepth))
700 {
702 return -1;
703 }
704
705 Value::Ptr parent = _stack.top ();
706
707 if (parent->index () == Value::ObjectValue)
708 {
709 _stack.push (&parent->insert ({std::move (_curkey), std::move (object)}));
710 }
711 else
712 {
713 _stack.push (&parent->pushBack (std::move (object)));
714 }
715
716 return 0;
717 }
718
724 virtual int setKey (const std::string& key) override
725 {
726 _curkey = key;
727 return 0;
728 }
729
734 virtual int stopObject () override
735 {
736 if (JOIN_LIKELY (_stack.size ()))
737 {
738 _stack.pop ();
739 }
740
741 return 0;
742 }
743
749 virtual int setValue (Value&& value)
750 {
751 if (JOIN_UNLIKELY (_stack.empty ()))
752 {
754 return -1;
755 }
756
757 Value::Ptr parent = _stack.top ();
758
759 if (parent->index () == Value::ObjectValue)
760 {
761 parent->insert ({std::move (_curkey), std::move (value)});
762 }
763 else
764 {
765 parent->pushBack (std::move (value));
766 }
767
768 return 0;
769 }
770
772 static constexpr size_t _maxdepth = 19;
773
775 std::stack <Value*> _stack;
776
778 std::string _curkey;
779
782 };
783}
784
785namespace std
786{
788 template <> struct is_error_condition_enum <join::SaxErrc> : public true_type {};
789}
790
791#endif
SAX API generic error category.
Definition sax.hpp:66
virtual std::string message(int code) const
translate SAX API generic error code to human readable error string.
Definition sax.cpp:44
virtual const char * name() const noexcept
get SAX API generic error category name.
Definition sax.cpp:35
SAX API interface class.
Definition sax.hpp:106
virtual int setInt(int32_t value)=0
set integer value.
virtual int setInt64(int64_t value)=0
set 64 bits integer value.
virtual int setString(const std::string &value)=0
set string value.
SaxHandler(SaxHandler &&other)=default
move constructor.
virtual int startArray(uint32_t size=0)=0
start array.
SaxHandler()=default
default constructor.
virtual int setNull()=0
set null value.
virtual int stopArray()
stop array.
Definition sax.hpp:210
SaxHandler(const SaxHandler &other)=default
copy constructor.
virtual int setBool(bool value)=0
set boolean value.
virtual int setDouble(double value)=0
set real value.
virtual int startObject(uint32_t size=0)=0
start object.
virtual int setUint64(uint64_t value)=0
set unsigned 64 bits integer value.
virtual ~SaxHandler()=default
destroy instance.
virtual int setKey(const std::string &key)=0
set key.
virtual int setUint(uint32_t value)=0
set unsigned integer value.
virtual int stopObject()
stop object.
Definition sax.hpp:233
stream deserializer abstract class.
Definition sax.hpp:443
virtual int stopObject() override
stop object.
Definition sax.hpp:734
virtual int startArray(uint32_t size=0) override
start array.
Definition sax.hpp:636
virtual int deserialize(std::ifstream &document)=0
deserialize a document.
StreamReader(Value &root)
default constructor.
Definition sax.hpp:449
virtual int setInt64(int64_t value) override
set 64 bits integer value.
Definition sax.hpp:596
virtual int setUint(uint32_t value) override
set unsigned integer value.
Definition sax.hpp:586
virtual int deserialize(std::istringstream &document)=0
deserialize a document.
StreamReader(StreamReader &&other)=delete
move constructor.
std::stack< Value * > _stack
stack.
Definition sax.hpp:775
virtual int setNull() override
set null value.
Definition sax.hpp:556
virtual int deserialize(std::istream &document)=0
deserialize a document.
virtual int deserialize(std::iostream &document)=0
deserialize a document.
virtual int setValue(Value &&value)
set value.
Definition sax.hpp:749
virtual int deserialize(const char *document, size_t length)=0
deserialize a document.
static constexpr size_t _maxdepth
max stack depth.
Definition sax.hpp:772
virtual int setUint64(uint64_t value) override
set unsigned 64 bits integer value.
Definition sax.hpp:606
virtual int deserialize(const char *first, const char *last)=0
deserialize a document.
virtual int deserialize(std::stringstream &document)=0
deserialize a document.
StreamReader(const StreamReader &other)=delete
copy constructor.
virtual int deserialize(std::fstream &document)=0
deserialize a document.
virtual int deserialize(const std::string &document)=0
deserialize a document.
virtual int setKey(const std::string &key) override
set key.
Definition sax.hpp:724
StreamReader & operator=(const StreamReader &other)=delete
copy assignment.
virtual int setInt(int32_t value) override
set integer value.
Definition sax.hpp:576
virtual int startObject(uint32_t size=0) override
start object.
Definition sax.hpp:687
virtual int setBool(bool value) override
set boolean value.
Definition sax.hpp:566
virtual int stopArray() override
stop array.
Definition sax.hpp:672
Value & _root
root.
Definition sax.hpp:781
virtual int setDouble(double value) override
set real value.
Definition sax.hpp:616
virtual int setString(const std::string &value) override
set string value.
Definition sax.hpp:626
virtual ~StreamReader()=default
destroy instance.
std::string _curkey
current key.
Definition sax.hpp:778
stream serializer abstract class.
Definition sax.hpp:243
void append(const char *data, uint32_t size) noexcept
append characters to output stream in batch.
Definition sax.hpp:429
virtual ~StreamWriter()=default
Destroy the Writer instance.
int setValue(const Value &value)
set value.
Definition sax.hpp:300
virtual int setArray(const Array &array)
set array value.
Definition sax.hpp:341
std::streambuf * _out
underlying output stream.
Definition sax.hpp:436
virtual int setObject(const Object &object)
set object value.
Definition sax.hpp:357
virtual int serialize(const Value &value)
Serialize data.
Definition sax.hpp:289
StreamWriter(StreamWriter &&other)=delete
Create the Writer instance by move.
void append3(const char *data) noexcept
append 3-character literal to output stream in batch.
Definition sax.hpp:392
StreamWriter(const StreamWriter &other)=delete
Create the Writer instance by copy.
void append(char data) noexcept
append character to output stream in batch.
Definition sax.hpp:373
StreamWriter & operator=(const StreamWriter &other)=delete
Assign the Writer instance by copy.
void append5(const char *data) noexcept
append 5-character literal to output stream in batch.
Definition sax.hpp:415
StreamWriter(std::ostream &document)
create instance.
Definition sax.hpp:249
void append4(const char *data) noexcept
append 4-character literal to output stream in batch.
Definition sax.hpp:403
void append2(const char *data) noexcept
append 2-character literal to output stream in batch.
Definition sax.hpp:382
value class.
Definition value.hpp:63
@ Integer64
Definition value.hpp:74
@ Unsigned64
Definition value.hpp:75
@ Real
Definition value.hpp:76
@ Boolean
Definition value.hpp:71
@ String
Definition value.hpp:77
@ ArrayValue
Definition value.hpp:78
@ Integer
Definition value.hpp:72
@ Unsigned
Definition value.hpp:73
@ ObjectValue
Definition value.hpp:79
Value & pushBack(const Value &value)
appends element at the end of the nested container.
Definition value.hpp:1299
Value & insert(const Member &member)
insert element in the nested container.
Definition value.hpp:1236
constexpr std::enable_if_t< is_unique< T, Ts... >::value, T & > get()
get the variable value of the object type identified by type.
Definition variant.hpp:584
constexpr std::size_t index() const noexcept
return the index of the alternative that is currently held by the variant.
Definition variant.hpp:704
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
std::error_code make_error_code(join::Errc code)
Create an std::error_code object.
Definition error.cpp:154
const std::error_category & saxCategory() noexcept
get error category.
Definition sax.cpp:65
SaxErrc
SAX API generic error codes.
Definition sax.hpp:55
std::vector< Value > Array
array.
Definition value.hpp:51
std::vector< Member > Object
object.
Definition value.hpp:57
thread_local std::error_code lastError
last error.
Definition error.cpp:32
std::error_condition make_error_condition(join::Errc code)
Create an std::error_condition object.
Definition error.cpp:163
Definition error.hpp:106
#define JOIN_LIKELY(x)
Definition utils.hpp:46
#define JOIN_UNLIKELY(x)
Definition utils.hpp:47