join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
sax.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_DATA_SAX_HPP
26#define JOIN_DATA_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 <string>
40#include <stack>
41
42// C.
43#include <cstddef>
44
45namespace join
46{
48 class Value;
49
53 enum class SaxErrc
54 {
55 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 Value& 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 if (startArray (array.size ()) == -1)
344 {
345 return -1;
346 }
347
348 for (auto const& element : array)
349 {
350 if (setValue (element) == -1)
351 {
352 return -1;
353 }
354 }
355
356 return stopArray ();
357 }
358
364 virtual int setObject (const Object& object)
365 {
366 if (startObject (object.size ()) == -1)
367 {
368 return -1;
369 }
370
371 for (auto const& member : object)
372 {
373 if (setKey (member.first) == -1)
374 {
375 return -1;
376 }
377
378 if (setValue (member.second) == -1)
379 {
380 return -1;
381 }
382 }
383
384 return stopObject ();
385 }
386
391 inline void append (char data) noexcept
392 {
393 _out->sputc (data);
394 }
395
400 inline void append2 (const char* data) noexcept
401 {
402 _out->sputc (data[0]);
403 _out->sputc (data[1]);
404 }
405
410 inline void append3 (const char* data) noexcept
411 {
412 _out->sputc (data[0]);
413 _out->sputc (data[1]);
414 _out->sputc (data[2]);
415 }
416
421 inline void append4 (const char* data) noexcept
422 {
423 _out->sputc (data[0]);
424 _out->sputc (data[1]);
425 _out->sputc (data[2]);
426 _out->sputc (data[3]);
427 }
428
433 inline void append5 (const char* data) noexcept
434 {
435 _out->sputc (data[0]);
436 _out->sputc (data[1]);
437 _out->sputc (data[2]);
438 _out->sputc (data[3]);
439 _out->sputc (data[4]);
440 }
441
447 inline void append (const char* data, uint32_t size) noexcept
448 {
449 _out->sputn (data, size);
450 }
451
452 protected:
454 std::streambuf* _out;
455 };
456
460 class StreamReader : protected SaxHandler
461 {
462 public:
468 : SaxHandler ()
469 , _root (root)
470 {
471 }
472
477 StreamReader (const StreamReader& other) = delete;
478
484 StreamReader& operator= (const StreamReader& other) = delete;
485
490 StreamReader (StreamReader&& other) = delete;
491
498
502 virtual ~StreamReader () = default;
503
510 virtual int deserialize (const char* document, size_t length) = 0;
511
518 virtual int deserialize (const char* first, const char* last) = 0;
519
525 virtual int deserialize (const std::string& document) = 0;
526
532 virtual int deserialize (std::stringstream& document) = 0;
533
539 virtual int deserialize (std::istringstream& document) = 0;
540
546 virtual int deserialize (std::fstream& document) = 0;
547
553 virtual int deserialize (std::ifstream& document) = 0;
554
560 virtual int deserialize (std::iostream& document) = 0;
561
567 virtual int deserialize (std::istream& document) = 0;
568
569 protected:
574 virtual int setNull () override
575 {
576 return setValue (Value (in_place_index_t<Value::Null>{}, nullptr));
577 }
578
584 virtual int setBool (bool value) override
585 {
587 }
588
594 virtual int setInt (int32_t value) override
595 {
597 }
598
604 virtual int setUint (uint32_t value) override
605 {
607 }
608
614 virtual int setInt64 (int64_t value) override
615 {
617 }
618
624 virtual int setUint64 (uint64_t value) override
625 {
627 }
628
634 virtual int setDouble (double value) override
635 {
636 return setValue (Value (in_place_index_t<Value::Real>{}, value));
637 }
638
644 virtual int setString (const std::string& value) override
645 {
647 }
648
654 virtual int startArray (uint32_t size = 0) override
655 {
656 Array array;
657 array.reserve (size ? size : 8);
658
659 if (JOIN_UNLIKELY (_stack.empty ()))
660 {
661 _root = std::move (array);
662 _stack.push (&_root);
663 return 0;
664 }
665
666 if (JOIN_UNLIKELY (_stack.size () >= _maxdepth))
667 {
669 return -1;
670 }
671
672 Value::Ptr parent = _stack.top ();
673
674 if (parent->index () == Value::ObjectValue)
675 {
676 _stack.push (&parent->insert ({std::move (_curkey), std::move (array)}));
677 }
678 else
679 {
680 _stack.push (&parent->pushBack (std::move (array)));
681 }
682
683 return 0;
684 }
685
690 virtual int stopArray () override
691 {
692 if (JOIN_LIKELY (_stack.size ()))
693 {
694 _stack.pop ();
695 }
696
697 return 0;
698 }
699
705 virtual int startObject (uint32_t size = 0) override
706 {
707 Object object;
708 object.reserve (size ? size : 8);
709
710 if (JOIN_UNLIKELY (_stack.empty ()))
711 {
712 _root = std::move (object);
713 _stack.push (&_root);
714 return 0;
715 }
716
717 if (JOIN_UNLIKELY (_stack.size () >= _maxdepth))
718 {
720 return -1;
721 }
722
723 Value::Ptr parent = _stack.top ();
724
725 if (parent->index () == Value::ObjectValue)
726 {
727 _stack.push (&parent->insert ({std::move (_curkey), std::move (object)}));
728 }
729 else
730 {
731 _stack.push (&parent->pushBack (std::move (object)));
732 }
733
734 return 0;
735 }
736
742 virtual int setKey (const Value& key) override
743 {
744 _curkey = key;
745 return 0;
746 }
747
752 virtual int stopObject () override
753 {
754 if (JOIN_LIKELY (_stack.size ()))
755 {
756 _stack.pop ();
757 }
758
759 return 0;
760 }
761
767 virtual int setValue (Value&& value)
768 {
769 if (JOIN_UNLIKELY (_stack.empty ()))
770 {
772 return -1;
773 }
774
775 Value::Ptr parent = _stack.top ();
776
777 if (parent->index () == Value::ObjectValue)
778 {
779 parent->insert ({std::move (_curkey), std::move (value)});
780 }
781 else
782 {
783 parent->pushBack (std::move (value));
784 }
785
786 return 0;
787 }
788
790 static constexpr size_t _maxdepth = 19;
791
793 std::stack<Value*> _stack;
794
797
800 };
801}
802
803namespace std
804{
806 template <>
807 struct is_error_condition_enum<join::SaxErrc> : public true_type
808 {
809 };
810}
811
812#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 setKey(const Value &key)=0
set key.
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 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:461
virtual int stopObject() override
stop object.
Definition sax.hpp:752
virtual int startArray(uint32_t size=0) override
start array.
Definition sax.hpp:654
virtual int deserialize(std::ifstream &document)=0
deserialize a document.
StreamReader(Value &root)
default constructor.
Definition sax.hpp:467
virtual int setInt64(int64_t value) override
set 64 bits integer value.
Definition sax.hpp:614
virtual int setUint(uint32_t value) override
set unsigned integer value.
Definition sax.hpp:604
virtual int deserialize(std::istringstream &document)=0
deserialize a document.
StreamReader(StreamReader &&other)=delete
move constructor.
std::stack< Value * > _stack
stack.
Definition sax.hpp:793
Value _curkey
current key.
Definition sax.hpp:796
virtual int setNull() override
set null value.
Definition sax.hpp:574
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:767
virtual int deserialize(const char *document, size_t length)=0
deserialize a document.
static constexpr size_t _maxdepth
max stack depth.
Definition sax.hpp:790
virtual int setUint64(uint64_t value) override
set unsigned 64 bits integer value.
Definition sax.hpp:624
virtual int deserialize(const char *first, const char *last)=0
deserialize a document.
virtual int deserialize(std::stringstream &document)=0
deserialize a document.
virtual int setKey(const Value &key) override
set key.
Definition sax.hpp:742
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.
StreamReader & operator=(const StreamReader &other)=delete
copy assignment.
virtual int setInt(int32_t value) override
set integer value.
Definition sax.hpp:594
virtual int startObject(uint32_t size=0) override
start object.
Definition sax.hpp:705
virtual int setBool(bool value) override
set boolean value.
Definition sax.hpp:584
virtual int stopArray() override
stop array.
Definition sax.hpp:690
Value & _root
root.
Definition sax.hpp:799
virtual int setDouble(double value) override
set real value.
Definition sax.hpp:634
virtual int setString(const std::string &value) override
set string value.
Definition sax.hpp:644
virtual ~StreamReader()=default
destroy instance.
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:447
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:454
virtual int setObject(const Object &object)
set object value.
Definition sax.hpp:364
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:410
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:391
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:433
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:421
void append2(const char *data) noexcept
append 2-character literal to output stream in batch.
Definition sax.hpp:400
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:1278
Value & insert(const Member &member)
insert element in the nested container.
Definition value.hpp:1215
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:548
constexpr std::size_t index() const noexcept
return the index of the alternative that is currently held by the variant.
Definition variant.hpp:659
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
const std::error_category & saxCategory() noexcept
get error category.
Definition sax.cpp:67
SaxErrc
SAX API generic error codes.
Definition sax.hpp:54
std::vector< Value > Array
array.
Definition value.hpp:50
std::error_code make_error_code(join::Errc code) noexcept
Create an std::error_code object.
Definition error.cpp:195
thread_local std::error_code lastError
last error.
Definition error.cpp:32
std::vector< Member > Object
object.
Definition value.hpp:56
std::error_condition make_error_condition(join::Errc code) noexcept
Create an std::error_condition object.
Definition error.cpp:204
Definition error.hpp:144
disambiguation tag to indicate that the contained object should be constructed in-place.
Definition traits.hpp:57
#define JOIN_LIKELY(x)
Definition utils.hpp:45
#define JOIN_UNLIKELY(x)
Definition utils.hpp:46