jsoncons 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. data/ext/jsoncons/extconf.rb +43 -0
  3. data/ext/jsoncons/jsoncons.cpp +161 -0
  4. data/ext/jsoncons/jsoncons.h +10 -0
  5. data/jsoncons.gemspec +44 -0
  6. data/lib/jsoncons/jsoncons/examples/input/address-book.json +13 -0
  7. data/lib/jsoncons/jsoncons/examples/input/books.json +28 -0
  8. data/lib/jsoncons/jsoncons/examples/input/countries.json +7 -0
  9. data/lib/jsoncons/jsoncons/examples/input/employees.json +30 -0
  10. data/lib/jsoncons/jsoncons/examples/input/jsonschema/name.json +15 -0
  11. data/lib/jsoncons/jsoncons/examples/input/multiple-json-objects.json +3 -0
  12. data/lib/jsoncons/jsoncons/examples/input/sales.csv +6 -0
  13. data/lib/jsoncons/jsoncons/examples/input/store.json +28 -0
  14. data/lib/jsoncons/jsoncons/examples/input/tasks.csv +6 -0
  15. data/lib/jsoncons/jsoncons/include/jsoncons/allocator_holder.hpp +38 -0
  16. data/lib/jsoncons/jsoncons/include/jsoncons/basic_json.hpp +5905 -0
  17. data/lib/jsoncons/jsoncons/include/jsoncons/bigint.hpp +1611 -0
  18. data/lib/jsoncons/jsoncons/include/jsoncons/byte_string.hpp +820 -0
  19. data/lib/jsoncons/jsoncons/include/jsoncons/config/binary_config.hpp +226 -0
  20. data/lib/jsoncons/jsoncons/include/jsoncons/config/compiler_support.hpp +375 -0
  21. data/lib/jsoncons/jsoncons/include/jsoncons/config/jsoncons_config.hpp +309 -0
  22. data/lib/jsoncons/jsoncons/include/jsoncons/config/version.hpp +40 -0
  23. data/lib/jsoncons/jsoncons/include/jsoncons/conv_error.hpp +218 -0
  24. data/lib/jsoncons/jsoncons/include/jsoncons/decode_json.hpp +209 -0
  25. data/lib/jsoncons/jsoncons/include/jsoncons/decode_traits.hpp +651 -0
  26. data/lib/jsoncons/jsoncons/include/jsoncons/detail/endian.hpp +44 -0
  27. data/lib/jsoncons/jsoncons/include/jsoncons/detail/grisu3.hpp +312 -0
  28. data/lib/jsoncons/jsoncons/include/jsoncons/detail/optional.hpp +483 -0
  29. data/lib/jsoncons/jsoncons/include/jsoncons/detail/parse_number.hpp +1133 -0
  30. data/lib/jsoncons/jsoncons/include/jsoncons/detail/span.hpp +188 -0
  31. data/lib/jsoncons/jsoncons/include/jsoncons/detail/string_view.hpp +537 -0
  32. data/lib/jsoncons/jsoncons/include/jsoncons/detail/string_wrapper.hpp +370 -0
  33. data/lib/jsoncons/jsoncons/include/jsoncons/detail/write_number.hpp +567 -0
  34. data/lib/jsoncons/jsoncons/include/jsoncons/encode_json.hpp +315 -0
  35. data/lib/jsoncons/jsoncons/include/jsoncons/encode_traits.hpp +378 -0
  36. data/lib/jsoncons/jsoncons/include/jsoncons/json.hpp +18 -0
  37. data/lib/jsoncons/jsoncons/include/jsoncons/json_array.hpp +324 -0
  38. data/lib/jsoncons/jsoncons/include/jsoncons/json_content_handler.hpp +12 -0
  39. data/lib/jsoncons/jsoncons/include/jsoncons/json_cursor.hpp +448 -0
  40. data/lib/jsoncons/jsoncons/include/jsoncons/json_decoder.hpp +420 -0
  41. data/lib/jsoncons/jsoncons/include/jsoncons/json_encoder.hpp +1587 -0
  42. data/lib/jsoncons/jsoncons/include/jsoncons/json_error.hpp +156 -0
  43. data/lib/jsoncons/jsoncons/include/jsoncons/json_exception.hpp +241 -0
  44. data/lib/jsoncons/jsoncons/include/jsoncons/json_filter.hpp +653 -0
  45. data/lib/jsoncons/jsoncons/include/jsoncons/json_fwd.hpp +23 -0
  46. data/lib/jsoncons/jsoncons/include/jsoncons/json_object.hpp +1772 -0
  47. data/lib/jsoncons/jsoncons/include/jsoncons/json_options.hpp +862 -0
  48. data/lib/jsoncons/jsoncons/include/jsoncons/json_parser.hpp +2900 -0
  49. data/lib/jsoncons/jsoncons/include/jsoncons/json_reader.hpp +731 -0
  50. data/lib/jsoncons/jsoncons/include/jsoncons/json_traits_macros.hpp +1072 -0
  51. data/lib/jsoncons/jsoncons/include/jsoncons/json_traits_macros_deprecated.hpp +144 -0
  52. data/lib/jsoncons/jsoncons/include/jsoncons/json_type.hpp +206 -0
  53. data/lib/jsoncons/jsoncons/include/jsoncons/json_type_traits.hpp +1830 -0
  54. data/lib/jsoncons/jsoncons/include/jsoncons/json_visitor.hpp +1560 -0
  55. data/lib/jsoncons/jsoncons/include/jsoncons/json_visitor2.hpp +2079 -0
  56. data/lib/jsoncons/jsoncons/include/jsoncons/pretty_print.hpp +89 -0
  57. data/lib/jsoncons/jsoncons/include/jsoncons/ser_context.hpp +62 -0
  58. data/lib/jsoncons/jsoncons/include/jsoncons/sink.hpp +289 -0
  59. data/lib/jsoncons/jsoncons/include/jsoncons/source.hpp +777 -0
  60. data/lib/jsoncons/jsoncons/include/jsoncons/source_adaptor.hpp +148 -0
  61. data/lib/jsoncons/jsoncons/include/jsoncons/staj2_cursor.hpp +1189 -0
  62. data/lib/jsoncons/jsoncons/include/jsoncons/staj_cursor.hpp +1254 -0
  63. data/lib/jsoncons/jsoncons/include/jsoncons/staj_iterator.hpp +449 -0
  64. data/lib/jsoncons/jsoncons/include/jsoncons/tag_type.hpp +245 -0
  65. data/lib/jsoncons/jsoncons/include/jsoncons/text_source_adaptor.hpp +144 -0
  66. data/lib/jsoncons/jsoncons/include/jsoncons/traits_extension.hpp +884 -0
  67. data/lib/jsoncons/jsoncons/include/jsoncons/typed_array_view.hpp +250 -0
  68. data/lib/jsoncons/jsoncons/include/jsoncons/unicode_traits.hpp +1330 -0
  69. data/lib/jsoncons/jsoncons/include/jsoncons/uri.hpp +635 -0
  70. data/lib/jsoncons/jsoncons/include/jsoncons/value_converter.hpp +340 -0
  71. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson.hpp +23 -0
  72. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_cursor.hpp +320 -0
  73. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_decimal128.hpp +865 -0
  74. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_encoder.hpp +585 -0
  75. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_error.hpp +103 -0
  76. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_oid.hpp +245 -0
  77. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_options.hpp +75 -0
  78. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_parser.hpp +645 -0
  79. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_reader.hpp +92 -0
  80. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/bson_type.hpp +44 -0
  81. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/decode_bson.hpp +201 -0
  82. data/lib/jsoncons/jsoncons/include/jsoncons_ext/bson/encode_bson.hpp +144 -0
  83. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor.hpp +26 -0
  84. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_cursor.hpp +351 -0
  85. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_cursor2.hpp +265 -0
  86. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_detail.hpp +93 -0
  87. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_encoder.hpp +1766 -0
  88. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_error.hpp +105 -0
  89. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_options.hpp +113 -0
  90. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_parser.hpp +1942 -0
  91. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/cbor_reader.hpp +116 -0
  92. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/decode_cbor.hpp +203 -0
  93. data/lib/jsoncons/jsoncons/include/jsoncons_ext/cbor/encode_cbor.hpp +151 -0
  94. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv.hpp +17 -0
  95. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_cursor.hpp +358 -0
  96. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_encoder.hpp +954 -0
  97. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_error.hpp +85 -0
  98. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_options.hpp +973 -0
  99. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_parser.hpp +2099 -0
  100. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_reader.hpp +348 -0
  101. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/csv_serializer.hpp +12 -0
  102. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/decode_csv.hpp +208 -0
  103. data/lib/jsoncons/jsoncons/include/jsoncons_ext/csv/encode_csv.hpp +122 -0
  104. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jmespath/jmespath.hpp +5215 -0
  105. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jmespath/jmespath_error.hpp +215 -0
  106. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpatch/jsonpatch.hpp +579 -0
  107. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpatch/jsonpatch_error.hpp +121 -0
  108. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/expression.hpp +3329 -0
  109. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/flatten.hpp +432 -0
  110. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/json_location.hpp +445 -0
  111. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/json_query.hpp +115 -0
  112. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath.hpp +13 -0
  113. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath_error.hpp +240 -0
  114. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp +2612 -0
  115. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpath/jsonpath_selector.hpp +1322 -0
  116. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpointer/jsonpointer.hpp +1577 -0
  117. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonpointer/jsonpointer_error.hpp +119 -0
  118. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/format_validator.hpp +968 -0
  119. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/json_validator.hpp +120 -0
  120. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/jsonschema.hpp +13 -0
  121. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/jsonschema_error.hpp +105 -0
  122. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/jsonschema_version.hpp +18 -0
  123. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/keyword_validator.hpp +1745 -0
  124. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/keyword_validator_factory.hpp +556 -0
  125. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/schema_draft7.hpp +198 -0
  126. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/schema_location.hpp +200 -0
  127. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/schema_version.hpp +35 -0
  128. data/lib/jsoncons/jsoncons/include/jsoncons_ext/jsonschema/subschema.hpp +144 -0
  129. data/lib/jsoncons/jsoncons/include/jsoncons_ext/mergepatch/mergepatch.hpp +103 -0
  130. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/decode_msgpack.hpp +202 -0
  131. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/encode_msgpack.hpp +142 -0
  132. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack.hpp +24 -0
  133. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_cursor.hpp +343 -0
  134. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_cursor2.hpp +259 -0
  135. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_encoder.hpp +753 -0
  136. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_error.hpp +94 -0
  137. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_options.hpp +74 -0
  138. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_parser.hpp +748 -0
  139. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_reader.hpp +116 -0
  140. data/lib/jsoncons/jsoncons/include/jsoncons_ext/msgpack/msgpack_type.hpp +63 -0
  141. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/decode_ubjson.hpp +201 -0
  142. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/encode_ubjson.hpp +142 -0
  143. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson.hpp +23 -0
  144. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_cursor.hpp +307 -0
  145. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_encoder.hpp +502 -0
  146. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_error.hpp +100 -0
  147. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_options.hpp +87 -0
  148. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_parser.hpp +880 -0
  149. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_reader.hpp +92 -0
  150. data/lib/jsoncons/jsoncons/include/jsoncons_ext/ubjson/ubjson_type.hpp +43 -0
  151. data/lib/jsoncons/version.rb +5 -0
  152. data/lib/jsoncons.rb +33 -0
  153. data/test/jsoncons_test.rb +108 -0
  154. data/test/test_helper.rb +7 -0
  155. metadata +268 -0
@@ -0,0 +1,89 @@
1
+ // Copyright 2013 Daniel Parker
2
+ // Distributed under the Boost license, Version 1.0.
3
+ // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4
+
5
+ // See https://github.com/danielaparker/jsoncons for latest version
6
+
7
+ #ifndef JSONCONS_PRETTY_PRINT_HPP
8
+ #define JSONCONS_PRETTY_PRINT_HPP
9
+
10
+ #include <string>
11
+ #include <exception>
12
+ #include <cstring>
13
+ #include <ostream>
14
+ #include <memory>
15
+ #include <typeinfo>
16
+ #include <cstring>
17
+ #include <jsoncons/json_exception.hpp>
18
+ #include <jsoncons/json_options.hpp>
19
+ #include <jsoncons/json_encoder.hpp>
20
+ #include <jsoncons/json_type_traits.hpp>
21
+ #include <jsoncons/json_error.hpp>
22
+
23
+ namespace jsoncons {
24
+
25
+ template<class Json>
26
+ class json_printable
27
+ {
28
+ public:
29
+ using char_type = typename Json::char_type;
30
+
31
+ json_printable(const Json& j, indenting line_indent)
32
+ : j_(&j), indenting_(line_indent)
33
+ {
34
+ }
35
+
36
+ json_printable(const Json& j,
37
+ const basic_json_encode_options<char_type>& options,
38
+ indenting line_indent)
39
+ : j_(&j), options_(options), indenting_(line_indent)
40
+ {
41
+ }
42
+
43
+ void dump(std::basic_ostream<char_type>& os) const
44
+ {
45
+ j_->dump(os, options_, indenting_);
46
+ }
47
+
48
+ friend std::basic_ostream<char_type>& operator<<(std::basic_ostream<char_type>& os, const json_printable<Json>& pr)
49
+ {
50
+ pr.dump(os);
51
+ return os;
52
+ }
53
+
54
+ const Json *j_;
55
+ basic_json_encode_options<char_type> options_;
56
+ indenting indenting_;
57
+ private:
58
+ json_printable();
59
+ };
60
+
61
+ template<class Json>
62
+ json_printable<Json> print(const Json& j)
63
+ {
64
+ return json_printable<Json>(j, indenting::no_indent);
65
+ }
66
+
67
+ template<class Json>
68
+ json_printable<Json> print(const Json& j,
69
+ const basic_json_encode_options<typename Json::char_type>& options)
70
+ {
71
+ return json_printable<Json>(j, options, indenting::no_indent);
72
+ }
73
+
74
+ template<class Json>
75
+ json_printable<Json> pretty_print(const Json& j)
76
+ {
77
+ return json_printable<Json>(j, indenting::indent);
78
+ }
79
+
80
+ template<class Json>
81
+ json_printable<Json> pretty_print(const Json& j,
82
+ const basic_json_encode_options<typename Json::char_type>& options)
83
+ {
84
+ return json_printable<Json>(j, options, indenting::indent);
85
+ }
86
+
87
+ }
88
+
89
+ #endif
@@ -0,0 +1,62 @@
1
+ /// Copyright 2013-2018 Daniel Parker
2
+ // Distributed under the Boost license, Version 1.0.
3
+ // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4
+
5
+ // See https://github.com/danielaparker/jsoncons for latest version
6
+
7
+ #ifndef JSONCONS_SER_CONTEXT_HPP
8
+ #define JSONCONS_SER_CONTEXT_HPP
9
+
10
+ namespace jsoncons {
11
+
12
+ class ser_context
13
+ {
14
+ public:
15
+ virtual ~ser_context() noexcept = default;
16
+
17
+ virtual size_t line() const
18
+ {
19
+ return 0;
20
+ }
21
+
22
+ virtual size_t column() const
23
+ {
24
+ return 0;
25
+ }
26
+
27
+ virtual size_t position() const
28
+ {
29
+ return 0;
30
+ }
31
+
32
+ virtual size_t end_position() const
33
+ {
34
+ return 0;
35
+ }
36
+
37
+ #if !defined(JSONCONS_NO_DEPRECATED)
38
+ JSONCONS_DEPRECATED_MSG("Instead, use line()")
39
+ std::size_t line_number() const
40
+ {
41
+ return line();
42
+ }
43
+
44
+ JSONCONS_DEPRECATED_MSG("Instead, use column()")
45
+ std::size_t column_number() const
46
+ {
47
+ return column();
48
+ }
49
+ #endif
50
+ };
51
+
52
+ #if !defined(JSONCONS_NO_DEPRECATED)
53
+ JSONCONS_DEPRECATED_MSG("Instead, use ser_context") typedef ser_context null_ser_context;
54
+
55
+ JSONCONS_DEPRECATED_MSG("Instead, use ser_context") typedef ser_context parsing_context;
56
+ JSONCONS_DEPRECATED_MSG("Instead, use ser_context") typedef ser_context serializing_context;
57
+ JSONCONS_DEPRECATED_MSG("Instead, use ser_context") typedef ser_context null_parsing_context;
58
+ JSONCONS_DEPRECATED_MSG("Instead, use ser_context") typedef ser_context null_serializing_context;
59
+ #endif
60
+
61
+ }
62
+ #endif
@@ -0,0 +1,289 @@
1
+ // Copyright 2018 Daniel Parker
2
+ // Distributed under the Boost license, Version 1.0.
3
+ // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4
+
5
+ // See https://github.com/danielaparker/jsoncons for latest version
6
+
7
+ #ifndef JSONCONS_SINK_HPP
8
+ #define JSONCONS_SINK_HPP
9
+
10
+ #include <stdexcept>
11
+ #include <string>
12
+ #include <vector>
13
+ #include <ostream>
14
+ #include <cmath>
15
+ #include <exception>
16
+ #include <memory> // std::addressof
17
+ #include <cstring> // std::memcpy
18
+ #include <jsoncons/config/jsoncons_config.hpp>
19
+ #include <jsoncons/traits_extension.hpp>
20
+
21
+ namespace jsoncons {
22
+
23
+ // stream_sink
24
+
25
+ template <class CharT>
26
+ class stream_sink
27
+ {
28
+ public:
29
+ using value_type = CharT;
30
+ using container_type = std::basic_ostream<CharT>;
31
+
32
+ private:
33
+ static constexpr size_t default_buffer_length = 16384;
34
+
35
+ std::basic_ostream<CharT>* stream_ptr_;
36
+ std::vector<CharT> buffer_;
37
+ CharT * begin_buffer_;
38
+ const CharT* end_buffer_;
39
+ CharT* p_;
40
+
41
+ // Noncopyable
42
+ stream_sink(const stream_sink&) = delete;
43
+ stream_sink& operator=(const stream_sink&) = delete;
44
+
45
+ public:
46
+ stream_sink(stream_sink&&) = default;
47
+
48
+ stream_sink(std::basic_ostream<CharT>& os)
49
+ : stream_ptr_(std::addressof(os)), buffer_(default_buffer_length), begin_buffer_(buffer_.data()), end_buffer_(begin_buffer_+buffer_.size()), p_(begin_buffer_)
50
+ {
51
+ }
52
+ stream_sink(std::basic_ostream<CharT>& os, std::size_t buflen)
53
+ : stream_ptr_(std::addressof(os)), buffer_(buflen), begin_buffer_(buffer_.data()), end_buffer_(begin_buffer_+buffer_.size()), p_(begin_buffer_)
54
+ {
55
+ }
56
+ ~stream_sink() noexcept
57
+ {
58
+ stream_ptr_->write(begin_buffer_, buffer_length());
59
+ stream_ptr_->flush();
60
+ }
61
+
62
+ // Movable
63
+ stream_sink& operator=(stream_sink&&) = default;
64
+
65
+ void flush()
66
+ {
67
+ stream_ptr_->write(begin_buffer_, buffer_length());
68
+ stream_ptr_->flush();
69
+ p_ = buffer_.data();
70
+ }
71
+
72
+ void append(const CharT* s, std::size_t length)
73
+ {
74
+ std::size_t diff = end_buffer_ - p_;
75
+ if (diff >= length)
76
+ {
77
+ std::memcpy(p_, s, length*sizeof(CharT));
78
+ p_ += length;
79
+ }
80
+ else
81
+ {
82
+ stream_ptr_->write(begin_buffer_, buffer_length());
83
+ stream_ptr_->write(s,length);
84
+ p_ = begin_buffer_;
85
+ }
86
+ }
87
+
88
+ void push_back(CharT ch)
89
+ {
90
+ if (p_ < end_buffer_)
91
+ {
92
+ *p_++ = ch;
93
+ }
94
+ else
95
+ {
96
+ stream_ptr_->write(begin_buffer_, buffer_length());
97
+ p_ = begin_buffer_;
98
+ push_back(ch);
99
+ }
100
+ }
101
+ private:
102
+
103
+ std::size_t buffer_length() const
104
+ {
105
+ return p_ - begin_buffer_;
106
+ }
107
+ };
108
+
109
+ // binary_stream_sink
110
+
111
+ class binary_stream_sink
112
+ {
113
+ public:
114
+ typedef uint8_t value_type;
115
+ using container_type = std::basic_ostream<char>;
116
+ private:
117
+ static constexpr size_t default_buffer_length = 16384;
118
+
119
+ std::basic_ostream<char>* stream_ptr_;
120
+ std::vector<uint8_t> buffer_;
121
+ uint8_t * begin_buffer_;
122
+ const uint8_t* end_buffer_;
123
+ uint8_t* p_;
124
+
125
+ // Noncopyable
126
+ binary_stream_sink(const binary_stream_sink&) = delete;
127
+ binary_stream_sink& operator=(const binary_stream_sink&) = delete;
128
+
129
+ public:
130
+ binary_stream_sink(binary_stream_sink&&) = default;
131
+
132
+ binary_stream_sink(std::basic_ostream<char>& os)
133
+ : stream_ptr_(std::addressof(os)),
134
+ buffer_(default_buffer_length),
135
+ begin_buffer_(buffer_.data()),
136
+ end_buffer_(begin_buffer_+buffer_.size()),
137
+ p_(begin_buffer_)
138
+ {
139
+ }
140
+ binary_stream_sink(std::basic_ostream<char>& os, std::size_t buflen)
141
+ : stream_ptr_(std::addressof(os)),
142
+ buffer_(buflen),
143
+ begin_buffer_(buffer_.data()),
144
+ end_buffer_(begin_buffer_+buffer_.size()),
145
+ p_(begin_buffer_)
146
+ {
147
+ }
148
+ ~binary_stream_sink() noexcept
149
+ {
150
+ stream_ptr_->write((char*)begin_buffer_, buffer_length());
151
+ stream_ptr_->flush();
152
+ }
153
+
154
+ binary_stream_sink& operator=(binary_stream_sink&&) = default;
155
+
156
+ void flush()
157
+ {
158
+ stream_ptr_->write((char*)begin_buffer_, buffer_length());
159
+ p_ = buffer_.data();
160
+ }
161
+
162
+ void append(const uint8_t* s, std::size_t length)
163
+ {
164
+ std::size_t diff = end_buffer_ - p_;
165
+ if (diff >= length)
166
+ {
167
+ std::memcpy(p_, s, length*sizeof(uint8_t));
168
+ p_ += length;
169
+ }
170
+ else
171
+ {
172
+ stream_ptr_->write((char*)begin_buffer_, buffer_length());
173
+ stream_ptr_->write((const char*)s,length);
174
+ p_ = begin_buffer_;
175
+ }
176
+ }
177
+
178
+ void push_back(uint8_t ch)
179
+ {
180
+ if (p_ < end_buffer_)
181
+ {
182
+ *p_++ = ch;
183
+ }
184
+ else
185
+ {
186
+ stream_ptr_->write((char*)begin_buffer_, buffer_length());
187
+ p_ = begin_buffer_;
188
+ push_back(ch);
189
+ }
190
+ }
191
+ private:
192
+
193
+ std::size_t buffer_length() const
194
+ {
195
+ return p_ - begin_buffer_;
196
+ }
197
+ };
198
+
199
+ // string_sink
200
+
201
+ template <class StringT>
202
+ class string_sink
203
+ {
204
+ public:
205
+ using value_type = typename StringT::value_type;
206
+ using container_type = StringT;
207
+ private:
208
+ container_type* buf_ptr;
209
+
210
+ // Noncopyable
211
+ string_sink(const string_sink&) = delete;
212
+ string_sink& operator=(const string_sink&) = delete;
213
+ public:
214
+ string_sink(string_sink&& val) noexcept
215
+ : buf_ptr(nullptr)
216
+ {
217
+ std::swap(buf_ptr,val.buf_ptr);
218
+ }
219
+
220
+ string_sink(container_type& buf)
221
+ : buf_ptr(std::addressof(buf))
222
+ {
223
+ }
224
+
225
+ string_sink& operator=(string_sink&& val) noexcept
226
+ {
227
+ // TODO: Shouldn't val.buf_ptr be nullified?
228
+ // Also see move constructor above.
229
+ std::swap(buf_ptr,val.buf_ptr);
230
+ return *this;
231
+ }
232
+
233
+ void flush()
234
+ {
235
+ }
236
+
237
+ void append(const value_type* s, std::size_t length)
238
+ {
239
+ buf_ptr->insert(buf_ptr->end(), s, s+length);
240
+ }
241
+
242
+ void push_back(value_type ch)
243
+ {
244
+ buf_ptr->push_back(ch);
245
+ }
246
+ };
247
+
248
+ // bytes_sink
249
+
250
+ template <class Container, class = void>
251
+ class bytes_sink
252
+ {
253
+ };
254
+
255
+ template <class Container>
256
+ class bytes_sink<Container,typename std::enable_if<traits_extension::is_back_insertable_byte_container<Container>::value>::type>
257
+ {
258
+ public:
259
+ using container_type = Container;
260
+ using value_type = typename Container::value_type;
261
+ private:
262
+ container_type* buf_ptr;
263
+
264
+ // Noncopyable
265
+ bytes_sink(const bytes_sink&) = delete;
266
+ bytes_sink& operator=(const bytes_sink&) = delete;
267
+ public:
268
+ bytes_sink(bytes_sink&&) = default;
269
+
270
+ bytes_sink(container_type& buf)
271
+ : buf_ptr(std::addressof(buf))
272
+ {
273
+ }
274
+
275
+ bytes_sink& operator=(bytes_sink&&) = default;
276
+
277
+ void flush()
278
+ {
279
+ }
280
+
281
+ void push_back(uint8_t ch)
282
+ {
283
+ buf_ptr->push_back(static_cast<value_type>(ch));
284
+ }
285
+ };
286
+
287
+ } // namespace jsoncons
288
+
289
+ #endif