JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
encode.hpp
Go to the documentation of this file.
1/** \file jsonv/encode.hpp
2 * Classes and functions for encoding JSON values to various representations.
3 *
4 * Copyright (c) 2014 by Travis Gockel. All rights reserved.
5 *
6 * This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
7 * as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
8 * version.
9 *
10 * \author Travis Gockel (travis@gockelhut.com)
11**/
12#ifndef __JSONV_ENCODE_HPP_INCLUDED__
13#define __JSONV_ENCODE_HPP_INCLUDED__
14
15#include <jsonv/config.hpp>
16#include <jsonv/forward.hpp>
17#include <string_view>
18
19#include <cstdint>
20#include <iosfwd>
21
22namespace jsonv
23{
24
25/** An encoder is responsible for writing values to some form of output. **/
27{
28public:
29 virtual ~encoder() noexcept;
30
31 /** Encode some source value into this encoder. This is the only useful entry point to this class. **/
33
34protected:
35 /** Write the null value.
36 *
37 * \code
38 * null
39 * \endcode
40 **/
41 virtual void write_null() = 0;
42
43 /** Write the opening of an object value.
44 *
45 * \code
46 * {
47 * \endcode
48 **/
49 virtual void write_object_begin() = 0;
50
51 /** Write the closing of an object value.
52 *
53 * \code
54 * }
55 * \endcode
56 **/
57 virtual void write_object_end() = 0;
58
59 /** Write the key for an object, including the separator.
60 *
61 * \code
62 * "key":
63 * \endcode
64 **/
65 virtual void write_object_key(std::string_view key) = 0;
66
67 /** Write the delimiter between two entries in an object.
68 *
69 * \code
70 * ,
71 * \endcode
72 **/
73 virtual void write_object_delimiter() = 0;
74
75 /** Write the opening of an array value.
76 *
77 * \code
78 * [
79 * \endcode
80 **/
81 virtual void write_array_begin() = 0;
82
83 /** Write the closing of an array value.
84 *
85 * \code
86 * ]
87 * \endcode
88 **/
89 virtual void write_array_end() = 0;
90
91 /** Write the delimiter between two entries in an array.
92 *
93 * \code
94 * ,
95 * \endcode
96 **/
97 virtual void write_array_delimiter() = 0;
98
99 /** Write a string value.
100 *
101 * \param value is the string to write. It will \e hopefully be encoded as valid UTF-8. It is the implementation's
102 * choice of how to deal with malformed string values. Two common options are to replace malformed
103 * sequences with ?s or to simply output these encodings and let the receiver deal with them.
104 *
105 * \code
106 * "value"
107 * \endcode
108 **/
109 virtual void write_string(std::string_view value) = 0;
110
111 /** Write an integer value.
112 *
113 * \code
114 * 902
115 * \endcode
116 **/
117 virtual void write_integer(std::int64_t value) = 0;
118
119 /** Write a decimal value.
120 *
121 * \param value is the decimal to write. Keep in mind that standard JSON does not support special IEEE 754 values
122 * such as NaN and infinity. It is the implementation's choice of how to deal with such values. Two
123 * common options are to output \c null or to encode a string description of the special value.
124 *
125 * \code
126 * 4.9
127 * \endcode
128 **/
129 virtual void write_decimal(double value) = 0;
130
131 /** Write a boolean value.
132 *
133 * \code
134 * true
135 * \endcode
136 **/
137 virtual void write_boolean(bool value) = 0;
138};
139
140/** An encoder that outputs to an \c std::ostream. This implementation is used for \c operator<< on a \c value.
141**/
143 public encoder
144{
145public:
146 /** Create an instance which places text into \a output. **/
147 explicit ostream_encoder(std::ostream& output);
148
149 virtual ~ostream_encoder() noexcept;
150
151 /** If set to true (the default), then all non-ASCII characters in strings will be replaced with their numeric
152 * encodings. Since JSON allows for encoded text to be contained in a document, this is inefficient if you have
153 * many non-ASCII characters. If you know that your decoding side can properly handle UTF-8 encoding, then you
154 * should turn this on.
155 *
156 * \note
157 * This functionality cannot be used to passthrough malformed UTF-8 encoded strings. If a given string is invalid
158 * UTF-8, it will still get replaced with a numeric encoding.
159 **/
160 void ensure_ascii(bool value);
161
163 virtual void write_null() override;
164
165 virtual void write_object_begin() override;
166
167 virtual void write_object_end() override;
168
169 virtual void write_object_key(std::string_view key) override;
170
171 virtual void write_object_delimiter() override;
172
173 virtual void write_array_begin() override;
174
175 virtual void write_array_end() override;
176
177 virtual void write_array_delimiter() override;
178
179 virtual void write_string(std::string_view value) override;
180
181 virtual void write_integer(std::int64_t value) override;
182
183 /** When a special value is given, this will output \c null. **/
184 virtual void write_decimal(double value) override;
185
186 virtual void write_boolean(bool value) override;
187
189 std::ostream& output();
190
191private:
192 std::ostream& _output;
193 bool _ensure_ascii;
194};
195
196/** Like \c ostream_encoder, but pretty prints output to an \c std::ostream.
197 *
198 * \example "ostream_pretty_encoder to pretty-print JSON to std::cout"
199 * \code
200 * jsonv::ostream_pretty_encoder encoder(std::cout);
201 * encoder.encode(some_value);
202 * encoder.encode(another_value);
203 * \endcode
204**/
207{
208public:
209 /** Create an instance which places text into \a output. **/
210 explicit ostream_pretty_encoder(std::ostream& output, std::size_t indent_size = 2);
211
213
215 virtual void write_null() override;
216
217 virtual void write_object_begin() override;
218
219 virtual void write_object_end() override;
220
221 virtual void write_object_key(std::string_view key) override;
222
223 virtual void write_object_delimiter() override;
224
225 virtual void write_array_begin() override;
226
227 virtual void write_array_end() override;
228
229 virtual void write_array_delimiter() override;
230
231 virtual void write_string(std::string_view value) override;
232
233 virtual void write_integer(std::int64_t value) override;
234
235 virtual void write_decimal(double value) override;
236
237 virtual void write_boolean(bool value) override;
238
239private:
240 void write_prefix();
241
242 void write_eol();
243
244private:
245 std::size_t _indent;
246 std::size_t _indent_size;
247 bool _defer_indent;
248};
249
250}
251
252#endif/*__JSONV_ENCODE_HPP_INCLUDED__*/
An encoder is responsible for writing values to some form of output.
Definition encode.hpp:27
virtual void write_object_begin()=0
Write the opening of an object value.
virtual void write_decimal(double value)=0
Write a decimal value.
virtual void write_array_begin()=0
Write the opening of an array value.
virtual void write_array_delimiter()=0
Write the delimiter between two entries in an array.
virtual void write_string(std::string_view value)=0
Write a string value.
virtual void write_object_end()=0
Write the closing of an object value.
void encode(const jsonv::value &source)
Encode some source value into this encoder.
virtual void write_object_key(std::string_view key)=0
Write the key for an object, including the separator.
virtual void write_integer(std::int64_t value)=0
Write an integer value.
virtual void write_null()=0
Write the null value.
virtual void write_object_delimiter()=0
Write the delimiter between two entries in an object.
virtual void write_boolean(bool value)=0
Write a boolean value.
virtual void write_array_end()=0
Write the closing of an array value.
An adapter for enumeration types.
An encoder that outputs to an std::ostream.
Definition encode.hpp:144
ostream_encoder(std::ostream &output)
Create an instance which places text into output.
ostream_pretty_encoder(std::ostream &output, std::size_t indent_size=2)
Create an instance which places text into output.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition value.hpp:107
Copyright (c) 2014-2020 by Travis Gockel.
#define JSONV_PUBLIC
This function or class is part of the public API for JSON Voorhees.
Definition config.hpp:102
Copyright (c) 2012-2020 by Travis Gockel.
STL namespace.