BDE 4.14.0 Production release
Loading...
Searching...
No Matches
baljsn_datumencoderoptions.h
Go to the documentation of this file.
1/// @file baljsn_datumencoderoptions.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// baljsn_datumencoderoptions.h -*-C++-*-
8#ifndef INCLUDED_BALJSN_DATUMENCODEROPTIONS
9#define INCLUDED_BALJSN_DATUMENCODEROPTIONS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup baljsn_datumencoderoptions baljsn_datumencoderoptions
15/// @brief Provide an attribute class for specifying Datum<->JSON options.
16/// @addtogroup bal
17/// @{
18/// @addtogroup baljsn
19/// @{
20/// @addtogroup baljsn_datumencoderoptions
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#baljsn_datumencoderoptions-purpose"> Purpose</a>
25/// * <a href="#baljsn_datumencoderoptions-classes"> Classes </a>
26/// * <a href="#baljsn_datumencoderoptions-description"> Description </a>
27/// * <a href="#baljsn_datumencoderoptions-attributes"> Attributes </a>
28/// * <a href="#baljsn_datumencoderoptions-usage"> Usage </a>
29/// * <a href="#baljsn_datumencoderoptions-example-1-creating-and-populating-an-options-object"> Example 1: Creating and Populating an Options Object </a>
30///
31/// # Purpose {#baljsn_datumencoderoptions-purpose}
32/// Provide an attribute class for specifying Datum<->JSON options.
33///
34/// # Classes {#baljsn_datumencoderoptions-classes}
35///
36/// - baljsn::DatumEncoderOptions: options for JSON encoding `Datum` objects
37///
38/// @see baljsn_datumutil
39///
40/// # Description {#baljsn_datumencoderoptions-description}
41/// This component provides a single, simply constrained
42/// (value-semantic) attribute class, `baljsn::DatumEncoderOptions`, that is
43/// used to specify options for encoding `Datum` objects in the JSON format (see
44/// `baljsn::DatumUtil`).
45///
46/// ## Attributes {#baljsn_datumencoderoptions-attributes}
47///
48///
49/// @code
50/// Name Type Default Simple Constraints
51/// ------------------ ----------- ------- ------------------
52/// strictTypes bool false none
53/// encodingStyle EncodingStyle e_COMPACT none
54/// encodeQuotedDecimal64
55/// bool true none
56/// initialIndentLevel int 0 >= 0
57/// spacesPerLevel int 0 >= 0
58/// @endcode
59/// * `strictTypes`: whether type-checking is performed to make sure encoded
60/// types conform to the strict set of types that JSON can represent (and can
61/// thus be decoded back to the same types of `Datum` values) ([`string`,
62/// `double`, `bool`, `null`, `array`, `map`]).
63/// * `encodingStyle`: encoding style used to encode the JSON data.
64/// * `encodeQuotedDecimal64`: option specifying a way to encode `Decimal64`
65/// values. If the `encodeQuotedDecimal64`
66/// attribute in the `DatumEncoderOptions` is `true`
67/// (the default), the `Decimal64` values will be
68/// encoded as strings, otherwise they will be
69/// encoded as numbers. Encoding a Decimal64 as a
70/// JSON numbers will frequently result it being
71/// later decoded as binary floating point number,
72/// and in the process losing digits of precision
73/// that were the point of using the Decimal64 type
74/// in the first place. Care should be taken when
75/// setting this option to `false` (though it may be
76/// useful when communicating with endpoints that
77/// are known to correctly handle high precision
78/// JSON numbers).
79/// * `initialIndentLevel`: Initial indent level for the topmost element.
80/// * `spacesPerLevel`: spaces per additional indent level.
81///
82/// ## Usage {#baljsn_datumencoderoptions-usage}
83///
84///
85/// This section illustrates intended use of this component.
86///
87/// ### Example 1: Creating and Populating an Options Object {#baljsn_datumencoderoptions-example-1-creating-and-populating-an-options-object}
88///
89///
90/// This component is designed to be used at a higher level to set the options
91/// for encoding `Datum` objects in the JSON format. This example shows how to
92/// create and populate an options object.
93///
94/// First, we default-construct a `baljsn::DatumEncoderOptions` object:
95/// @code
96/// const bool STRICT_TYPES = true;
97/// const int INITIAL_INDENT_LEVEL = 1;
98/// const int SPACES_PER_LEVEL = 4;
99/// const bool ENCODE_QUOTED_DECIMAL64 = false;
100///
101/// baljsn::DatumEncoderOptions options;
102/// assert(false == options.strictTypes());
103/// assert(0 == options.initialIndentLevel());
104/// assert(0 == options.spacesPerLevel());
105/// assert(baljsn::EncodingStyle::e_COMPACT == options.encodingStyle());
106/// assert(true == options.encodeQuotedDecimal64());
107/// @endcode
108/// Next, we populate that object to check strict types and encode in a pretty
109/// format using a pre-defined initial indent level and spaces per level:
110/// @code
111/// options.setStrictTypes(STRICT_TYPES);
112/// assert(true == options.strictTypes());
113///
114/// options.setEncodingStyle(baljsn::EncodingStyle::e_PRETTY);
115/// assert(baljsn::EncodingStyle::e_PRETTY == options.encodingStyle());
116///
117/// options.setEncodeQuotedDecimal64(ENCODE_QUOTED_DECIMAL64);
118/// ASSERT(ENCODE_QUOTED_DECIMAL64 == options.encodeQuotedDecimal64());
119///
120/// options.setInitialIndentLevel(INITIAL_INDENT_LEVEL);
121/// assert(INITIAL_INDENT_LEVEL == options.initialIndentLevel());
122///
123/// options.setSpacesPerLevel(SPACES_PER_LEVEL);
124/// assert(SPACES_PER_LEVEL == options.spacesPerLevel());
125/// @endcode
126/// @}
127/** @} */
128/** @} */
129
130/** @addtogroup bal
131 * @{
132 */
133/** @addtogroup baljsn
134 * @{
135 */
136/** @addtogroup baljsn_datumencoderoptions
137 * @{
138 */
139
140#include <balscm_version.h>
141
142#include <baljsn_encodingstyle.h>
143
144#include <bslalg_typetraits.h>
145
146#include <bsl_limits.h>
147#include <bsl_iosfwd.h>
148
149#include <bsls_assert.h>
150#include <bsls_objectbuffer.h>
151#include <bsls_review.h>
152
153
154
155namespace baljsn { class DatumEncoderOptions; }
156namespace baljsn {
157
158 // =========================
159 // class DatumEncoderOptions
160 // =========================
161
162/// This simply constrained (value-semantic) attribute class specifies
163/// options for encoding `Datum` objects in the JSON format. See the
164/// @ref baljsn_datumencoderoptions-attributes section for information on the class attributes. Note that
165/// the class invariants are identically the constraints on the individual
166/// attributes.
167///
168/// See @ref baljsn_datumencoderoptions
170
171 // INSTANCE DATA
172
173 // whether strict type validation is performed
174 bool d_strictTypes;
175
176 // option specifying a way to encode `Decimal64` values. If the option
177 // value is `true` then the `Decimal64` value is encoded quoted
178 // { "dec": "1.2e-5" }, and unquoted { "dec": 1.2e-5 } otherwise.
179 bool d_encodeQuotedDecimal64;
180
181 // initial indentation level for the topmost element
182 int d_initialIndentLevel;
183
184 // spaces per additional level of indentation
185 int d_spacesPerLevel;
186
187 // encoding style used to encode values
188 baljsn::EncodingStyle::Value d_encodingStyle;
189
190 public:
191 // CONSTANTS
193
195
197
199
202
203 public:
204 // CREATORS
205
206 /// Create an object of type `DatumEncoderOptions` having the default
207 /// value.
209
210 /// Create an object of type `DatumEncoderOptions` having the value of
211 /// the specified `original` object.
213
214 /// Destroy this object.
216
217 // MANIPULATORS
218
219 /// Assign to this object the value of the specified `rhs` object.
221
222 /// Reset this object to the default value (i.e., its value upon
223 /// default construction).
224 void reset();
225
226 /// Set the "StrictTypes" attribute of this object to the specified
227 /// `value`.
228 void setStrictTypes(bool value);
229
230 /// Set the "InitialIndentLevel" attribute of this object to the
231 /// specified `value`. The behavior is undefined unless `0 <= value`.
232 void setInitialIndentLevel(int value);
233
234 /// Set the "SpacesPerLevel" attribute of this object to the specified
235 /// `value`. The behavior is undefined unless `0 <= value`.
236 void setSpacesPerLevel(int value);
237
238 /// Set the "EncodingStyle" attribute of this object to the specified
239 /// `value`.
241
242 /// Set the "EncodeQuotedDecimal64" attribute of this object to the
243 /// specified `value`.
244 void setEncodeQuotedDecimal64(bool value);
245
246 // ACCESSORS
247
248 /// Return the "StrictTypes" attribute of this object.
249 bool strictTypes() const;
250
251 /// Return the "InitialIndentLevel" attribute of this object.
252 int initialIndentLevel() const;
253
254 /// Return the "SpacesPerLevel" attribute of this object.
255 int spacesPerLevel() const;
256
257 /// Return the "EncodingStyle" attribute of this object.
259
260 /// Return the value of the "EncodeQuotedDecimal64" attribute of this
261 /// object.
262 bool encodeQuotedDecimal64() const;
263
264 // Aspects
265
266 /// Format this object to the specified output `stream` at the
267 /// optionally specified indentation `level` and return a reference to
268 /// the modifiable `stream`. If `level` is specified, optionally
269 /// specify `spacesPerLevel`, the number of spaces per indentation level
270 /// for this and all of its nested objects. Each line is indented by
271 /// the absolute value of `level * spacesPerLevel`. If `level` is
272 /// negative, suppress indentation of the first line. If
273 /// `spacesPerLevel` is negative, suppress line breaks and format the
274 /// entire output on one line. If `stream` is initially invalid, this
275 /// operation has no effect. Note that a trailing newline is provided
276 /// in multiline mode only.
277 bsl::ostream& print(bsl::ostream& stream,
278 int level = 0,
279 int spacesPerLevel = 4) const;
280};
281
282// FREE OPERATORS
283
284/// Return `true` if the specified `lhs` and `rhs` attribute objects have
285/// the same value, and `false` otherwise. Two attribute objects have the
286/// same value if each respective attribute has the same value.
287inline
288bool operator==(const DatumEncoderOptions& lhs,
289 const DatumEncoderOptions& rhs);
290
291/// Return `true` if the specified `lhs` and `rhs` attribute objects do not
292/// have the same value, and `false` otherwise. Two attribute objects do
293/// not have the same value if one or more respective attributes differ in
294/// values.
295inline
296bool operator!=(const DatumEncoderOptions& lhs,
297 const DatumEncoderOptions& rhs);
298
299/// Format the specified `rhs` to the specified output `stream` and return a
300/// reference to the modifiable `stream`.
301inline
302bsl::ostream& operator<<(bsl::ostream& stream, const DatumEncoderOptions& rhs);
303
304} // close package namespace
305
306// ============================================================================
307// INLINE FUNCTION DEFINITIONS
308// ============================================================================
309
310namespace baljsn {
311
312 // -------------------------
313 // class DatumEncoderOptions
314 // -------------------------
315
316inline
318{
319 d_encodingStyle = value;
320}
321
322inline
324{
325 BSLS_ASSERT(0 <= value);
326
327 d_initialIndentLevel = value;
328}
329
330inline
332{
333 BSLS_ASSERT(0 <= value);
334
335 d_spacesPerLevel = value;
336}
337
338inline
340{
341 d_strictTypes = value;
342}
343
344inline
346{
347 d_encodeQuotedDecimal64 = value;
348}
349
350// ACCESSORS
351inline
353{
354 return static_cast<baljsn::EncodingStyle::Value>(d_encodingStyle);
355}
356
357inline
359{
360 return d_initialIndentLevel;
361}
362
363inline
365{
366 return d_spacesPerLevel;
367}
368
369inline
371{
372 return d_strictTypes;
373}
374
375inline
377{
378 return d_encodeQuotedDecimal64;
379}
380
381} // close package namespace
382
383// ============================================================================
384// INLINE DEFINITIONS
385// ============================================================================
386
387inline
390{
391 return lhs.strictTypes() == rhs.strictTypes()
392 && lhs.initialIndentLevel() == rhs.initialIndentLevel()
393 && lhs.spacesPerLevel() == rhs.spacesPerLevel()
394 && lhs.encodingStyle() == rhs.encodingStyle()
396}
397
398inline
401{
402 return lhs.strictTypes() != rhs.strictTypes()
403 || lhs.initialIndentLevel() != rhs.initialIndentLevel()
404 || lhs.spacesPerLevel() != rhs.spacesPerLevel()
405 || lhs.encodingStyle() != rhs.encodingStyle()
407}
408
409inline
410bsl::ostream& baljsn::operator<<(bsl::ostream& stream,
412{
413 return rhs.print(stream, 0, -1);
414}
415
416
417#endif
418
419// ----------------------------------------------------------------------------
420// Copyright 2019 Bloomberg Finance L.P.
421//
422// Licensed under the Apache License, Version 2.0 (the "License");
423// you may not use this file except in compliance with the License.
424// You may obtain a copy of the License at
425//
426// http://www.apache.org/licenses/LICENSE-2.0
427//
428// Unless required by applicable law or agreed to in writing, software
429// distributed under the License is distributed on an "AS IS" BASIS,
430// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
431// See the License for the specific language governing permissions and
432// limitations under the License.
433// ----------------------------- END-OF-FILE ----------------------------------
434
435/** @} */
436/** @} */
437/** @} */
Definition baljsn_datumencoderoptions.h:169
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
DatumEncoderOptions(const DatumEncoderOptions &original)
void setEncodingStyle(baljsn::EncodingStyle::Value value)
Definition baljsn_datumencoderoptions.h:317
bool strictTypes() const
Return the "StrictTypes" attribute of this object.
Definition baljsn_datumencoderoptions.h:370
DatumEncoderOptions & operator=(const DatumEncoderOptions &rhs)
Assign to this object the value of the specified rhs object.
void setEncodeQuotedDecimal64(bool value)
Definition baljsn_datumencoderoptions.h:345
void setSpacesPerLevel(int value)
Definition baljsn_datumencoderoptions.h:331
int initialIndentLevel() const
Return the "InitialIndentLevel" attribute of this object.
Definition baljsn_datumencoderoptions.h:358
void setStrictTypes(bool value)
Definition baljsn_datumencoderoptions.h:339
~DatumEncoderOptions()
Destroy this object.
static const bool s_DEFAULT_INITIALIZER_ENCODE_QUOTED_DECIMAL64
Definition baljsn_datumencoderoptions.h:194
baljsn::EncodingStyle::Value encodingStyle() const
Return the "EncodingStyle" attribute of this object.
Definition baljsn_datumencoderoptions.h:352
void setInitialIndentLevel(int value)
Definition baljsn_datumencoderoptions.h:323
static const bool s_DEFAULT_INITIALIZER_STRICT_TYPES
Definition baljsn_datumencoderoptions.h:192
int spacesPerLevel() const
Return the "SpacesPerLevel" attribute of this object.
Definition baljsn_datumencoderoptions.h:364
static const int s_DEFAULT_INITIALIZER_INITIAL_INDENT_LEVEL
Definition baljsn_datumencoderoptions.h:196
bool encodeQuotedDecimal64() const
Definition baljsn_datumencoderoptions.h:376
static const int s_DEFAULT_INITIALIZER_SPACES_PER_LEVEL
Definition baljsn_datumencoderoptions.h:198
static const baljsn::EncodingStyle::Value s_DEFAULT_INITIALIZER_ENCODING_STYLE
Definition baljsn_datumencoderoptions.h:201
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition baljsn_datumdecoderoptions.h:113
bool operator==(const DatumDecoderOptions &lhs, const DatumDecoderOptions &rhs)
bsl::ostream & operator<<(bsl::ostream &stream, const DatumDecoderOptions &rhs)
bool operator!=(const DatumDecoderOptions &lhs, const DatumDecoderOptions &rhs)
Value
Definition baljsn_encodingstyle.h:78