BDE 4.39.x 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.
165///
166/// \note Note that the class invariants are identically the constraints on the individual
167/// attributes.
168///
169/// See @ref baljsn_datumencoderoptions
171
172 // INSTANCE DATA
173
174 // whether strict type validation is performed
175 bool d_strictTypes;
176
177 // option specifying a way to encode `Decimal64` values. If the option
178 // value is `true` then the `Decimal64` value is encoded quoted
179 // { "dec": "1.2e-5" }, and unquoted { "dec": 1.2e-5 } otherwise.
180 bool d_encodeQuotedDecimal64;
181
182 // initial indentation level for the topmost element
183 int d_initialIndentLevel;
184
185 // spaces per additional level of indentation
186 int d_spacesPerLevel;
187
188 // encoding style used to encode values
189 baljsn::EncodingStyle::Value d_encodingStyle;
190
191 public:
192 // CONSTANTS
194
196
198
200
203
204 public:
205 // CREATORS
206
207 /// Create an object of type `DatumEncoderOptions` having the default
208 /// value.
210
211 /// Create an object of type `DatumEncoderOptions` having the value of
212 /// the specified `original` object.
214
215 /// Destroy this object.
217
218 // MANIPULATORS
219
220 /// Assign to this object the value of the specified `rhs` object.
222
223 /// Reset this object to the default value (i.e., its value upon
224 /// default construction).
225 void reset();
226
227 /// Set the "StrictTypes" attribute of this object to the specified
228 /// `value`.
229 void setStrictTypes(bool value);
230
231 /// Set the "InitialIndentLevel" attribute of this object to the specified `value`.
232 ///
233 /// \pre The behavior is undefined unless `0 <= value`.
234 void setInitialIndentLevel(int value);
235
236 /// Set the "SpacesPerLevel" attribute of this object to the specified `value`.
237 ///
238 /// \pre The behavior is undefined unless `0 <= value`.
239 void setSpacesPerLevel(int value);
240
241 /// Set the "EncodingStyle" attribute of this object to the specified
242 /// `value`.
244
245 /// Set the "EncodeQuotedDecimal64" attribute of this object to the
246 /// specified `value`.
247 void setEncodeQuotedDecimal64(bool value);
248
249 // ACCESSORS
250
251 /// Return the "StrictTypes" attribute of this object.
252 bool strictTypes() const;
253
254 /// Return the "InitialIndentLevel" attribute of this object.
255 int initialIndentLevel() const;
256
257 /// Return the "SpacesPerLevel" attribute of this object.
258 int spacesPerLevel() const;
259
260 /// Return the "EncodingStyle" attribute of this object.
262
263 /// Return the value of the "EncodeQuotedDecimal64" attribute of this
264 /// object.
265 bool encodeQuotedDecimal64() const;
266
267 // Aspects
268
269 /// Format this object to the specified output `stream` at the
270 /// optionally specified indentation `level` and return a reference to
271 /// the modifiable `stream`. If `level` is specified, optionally
272 /// specify `spacesPerLevel`, the number of spaces per indentation level
273 /// for this and all of its nested objects. Each line is indented by
274 /// the absolute value of `level * spacesPerLevel`. If `level` is
275 /// negative, suppress indentation of the first line. If
276 /// `spacesPerLevel` is negative, suppress line breaks and format the
277 /// entire output on one line. If `stream` is initially invalid, this operation has no effect.
278 ///
279 /// \note Note that a trailing newline is provided
280 /// in multiline mode only.
281 bsl::ostream& print(bsl::ostream& stream,
282 int level = 0,
283 int spacesPerLevel = 4) const;
284};
285
286// FREE OPERATORS
287
288/// Return `true` if the specified `lhs` and `rhs` attribute objects have
289/// the same value, and `false` otherwise. Two attribute objects have the
290/// same value if each respective attribute has the same value.
291inline
292bool operator==(const DatumEncoderOptions& lhs,
293 const DatumEncoderOptions& rhs);
294
295/// Return `true` if the specified `lhs` and `rhs` attribute objects do not
296/// have the same value, and `false` otherwise. Two attribute objects do
297/// not have the same value if one or more respective attributes differ in
298/// values.
299inline
300bool operator!=(const DatumEncoderOptions& lhs,
301 const DatumEncoderOptions& rhs);
302
303/// Format the specified `rhs` to the specified output `stream` and return a
304/// reference to the modifiable `stream`.
305inline
306bsl::ostream& operator<<(bsl::ostream& stream, const DatumEncoderOptions& rhs);
307
308} // close package namespace
309
310// ============================================================================
311// INLINE FUNCTION DEFINITIONS
312// ============================================================================
313
314namespace baljsn {
315
316 // -------------------------
317 // class DatumEncoderOptions
318 // -------------------------
319
320inline
322{
323 d_encodingStyle = value;
324}
325
326inline
328{
329 BSLS_ASSERT(0 <= value);
330
331 d_initialIndentLevel = value;
332}
333
334inline
336{
337 BSLS_ASSERT(0 <= value);
338
339 d_spacesPerLevel = value;
340}
341
342inline
344{
345 d_strictTypes = value;
346}
347
348inline
350{
351 d_encodeQuotedDecimal64 = value;
352}
353
354// ACCESSORS
355inline
357{
358 return static_cast<baljsn::EncodingStyle::Value>(d_encodingStyle);
359}
360
361inline
363{
364 return d_initialIndentLevel;
365}
366
367inline
369{
370 return d_spacesPerLevel;
371}
372
373inline
375{
376 return d_strictTypes;
377}
378
379inline
381{
382 return d_encodeQuotedDecimal64;
383}
384
385} // close package namespace
386
387// ============================================================================
388// INLINE DEFINITIONS
389// ============================================================================
390
391inline
394{
395 return lhs.strictTypes() == rhs.strictTypes()
396 && lhs.initialIndentLevel() == rhs.initialIndentLevel()
397 && lhs.spacesPerLevel() == rhs.spacesPerLevel()
398 && lhs.encodingStyle() == rhs.encodingStyle()
399 && lhs.encodeQuotedDecimal64() == rhs.encodeQuotedDecimal64();
400}
401
402inline
405{
406 return lhs.strictTypes() != rhs.strictTypes()
407 || lhs.initialIndentLevel() != rhs.initialIndentLevel()
408 || lhs.spacesPerLevel() != rhs.spacesPerLevel()
409 || lhs.encodingStyle() != rhs.encodingStyle()
410 || lhs.encodeQuotedDecimal64() != rhs.encodeQuotedDecimal64();
411}
412
413inline
414bsl::ostream& baljsn::operator<<(bsl::ostream& stream,
416{
417 return rhs.print(stream, 0, -1);
418}
419
420
421#endif
422
423// ----------------------------------------------------------------------------
424// Copyright 2019 Bloomberg Finance L.P.
425//
426// Licensed under the Apache License, Version 2.0 (the "License");
427// you may not use this file except in compliance with the License.
428// You may obtain a copy of the License at
429//
430// http://www.apache.org/licenses/LICENSE-2.0
431//
432// Unless required by applicable law or agreed to in writing, software
433// distributed under the License is distributed on an "AS IS" BASIS,
434// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
435// See the License for the specific language governing permissions and
436// limitations under the License.
437// ----------------------------- END-OF-FILE ----------------------------------
438
439/** @} */
440/** @} */
441/** @} */
Definition baljsn_datumencoderoptions.h:170
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:321
bool strictTypes() const
Return the "StrictTypes" attribute of this object.
Definition baljsn_datumencoderoptions.h:374
DatumEncoderOptions & operator=(const DatumEncoderOptions &rhs)
Assign to this object the value of the specified rhs object.
void setEncodeQuotedDecimal64(bool value)
Definition baljsn_datumencoderoptions.h:349
void setSpacesPerLevel(int value)
Definition baljsn_datumencoderoptions.h:335
int initialIndentLevel() const
Return the "InitialIndentLevel" attribute of this object.
Definition baljsn_datumencoderoptions.h:362
void setStrictTypes(bool value)
Definition baljsn_datumencoderoptions.h:343
~DatumEncoderOptions()
Destroy this object.
static const bool s_DEFAULT_INITIALIZER_ENCODE_QUOTED_DECIMAL64
Definition baljsn_datumencoderoptions.h:195
baljsn::EncodingStyle::Value encodingStyle() const
Return the "EncodingStyle" attribute of this object.
Definition baljsn_datumencoderoptions.h:356
void setInitialIndentLevel(int value)
Definition baljsn_datumencoderoptions.h:327
static const bool s_DEFAULT_INITIALIZER_STRICT_TYPES
Definition baljsn_datumencoderoptions.h:193
int spacesPerLevel() const
Return the "SpacesPerLevel" attribute of this object.
Definition baljsn_datumencoderoptions.h:368
static const int s_DEFAULT_INITIALIZER_INITIAL_INDENT_LEVEL
Definition baljsn_datumencoderoptions.h:197
bool encodeQuotedDecimal64() const
Definition baljsn_datumencoderoptions.h:380
static const int s_DEFAULT_INITIALIZER_SPACES_PER_LEVEL
Definition baljsn_datumencoderoptions.h:199
static const baljsn::EncodingStyle::Value s_DEFAULT_INITIALIZER_ENCODING_STYLE
Definition baljsn_datumencoderoptions.h:202
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition baljsn_convertfromjsonoptions.h:112
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)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917
Value
Definition baljsn_encodingstyle.h:80