BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdljsn_readoptions.h
Go to the documentation of this file.
1/// @file bdljsn_readoptions.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdljsn_readoptions.h -*-C++-*-
8#ifndef INCLUDED_BDLJSN_READOPTIONS
9#define INCLUDED_BDLJSN_READOPTIONS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdljsn_readoptions bdljsn_readoptions
15/// @brief Provide options for reading a JSON document.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdljsn
19/// @{
20/// @addtogroup bdljsn_readoptions
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdljsn_readoptions-purpose"> Purpose</a>
25/// * <a href="#bdljsn_readoptions-classes"> Classes </a>
26/// * <a href="#bdljsn_readoptions-description"> Description </a>
27/// * <a href="#bdljsn_readoptions-attributes"> Attributes </a>
28/// * <a href="#bdljsn_readoptions-usage"> Usage </a>
29/// * <a href="#bdljsn_readoptions-example-1-creating-and-populating-an-options-object"> Example 1: Creating and Populating an Options Object </a>
30///
31/// # Purpose {#bdljsn_readoptions-purpose}
32/// Provide options for reading a JSON document.
33///
34/// # Classes {#bdljsn_readoptions-classes}
35///
36/// - bdljsn::ReadOptions: options for reading a JSON document
37///
38/// @see bdljsn_jsonutil, bdljsn_json
39///
40/// # Description {#bdljsn_readoptions-description}
41/// This component provides a single, simply constrained
42/// (value-semantic) attribute class, `bdljsn::ReadOptions`, that is used to
43/// specify options for reading a JSON document (see @ref bdljsn_jsonutil ).
44///
45/// ## Attributes {#bdljsn_readoptions-attributes}
46///
47///
48/// @code
49/// Name Type Default Simple Constraints
50/// ------------------ ----------- ------- ------------------
51/// maxNestedDepth int 64 > 0
52/// allowTrailingText bool false
53/// @endcode
54/// * `maxNestedDepth`: the maximum depth to which JSON objects and arrays are
55/// allowed to be nested before the JSON decoder reports an error. For
56/// example, if `maxNestedDepth` is 8, and a JSON text has 9 consecutive open
57/// brackets (`[`) then decoding will return an error. This option can be
58/// used to prevent poorly formed (or malicious) JSON text from causing a
59/// stack overflow.
60/// * `allowTrailingText`: whether a read operation will report an error
61/// if any non-white space text follows a valid JSON document. By default
62/// this option is `false`, indicating the user expects the input to contain
63/// a single valid JSON document (without any subsequent text). When
64/// set to `true` a `read` operation will return success if there is text
65/// following a valid JSON document, assuming that text is separated by
66/// a delimeter. See @ref bdljsn_jsonutil for details.
67///
68/// ## Usage {#bdljsn_readoptions-usage}
69///
70///
71/// This section illustrates intended use of this component.
72///
73/// ### Example 1: Creating and Populating an Options Object {#bdljsn_readoptions-example-1-creating-and-populating-an-options-object}
74///
75///
76/// This component is designed to be used at a higher level to set the options
77/// for decoding `Datum` objects in the JSON format. This example shows how to
78/// create and populate an options object.
79///
80/// First, we default-construct a `bdljsn::ReadOptions` object:
81/// @code
82/// const int MAX_NESTED_DEPTH = 16;
83///
84/// bdljsn::ReadOptions options;
85/// assert(64 == options.maxNestedDepth());
86/// assert(false == options.allowTrailingText());
87/// @endcode
88/// Finally, we populate that object to limit the maximum nested depth using a
89/// pre-defined limit:
90/// @code
91/// options.setMaxNestedDepth(MAX_NESTED_DEPTH);
92/// assert(MAX_NESTED_DEPTH == options.maxNestedDepth());
93/// @endcode
94/// @}
95/** @} */
96/** @} */
97
98/** @addtogroup bdl
99 * @{
100 */
101/** @addtogroup bdljsn
102 * @{
103 */
104/** @addtogroup bdljsn_readoptions
105 * @{
106 */
107
108#include <bdlscm_version.h>
109
110#include <bsl_iosfwd.h>
111
112#include <bsls_assert.h>
113
114
115namespace bdljsn {
116
117 // =================
118 // class ReadOptions
119 // =================
120
121/// This simply constrained (value-semantic) attribute class specifies
122/// options for reading a JSON document. See the {Attributes} section under
123/// {DESCRIPTION} in the component-level documentation for information on
124/// the class attributes. Note that the class invariants are identically
125/// the constraints on the individual attributes.
126///
127/// See @ref bdljsn_readoptions
129
130 // INSTANCE DATA
131
132 // whether to permit text after a valid JSON document
133 bool d_allowTrailingText;
134
135 // maximum nesting level for JSON objects and arrays
136 int d_maxNestedDepth;
137
138 public:
139 // CONSTANTS
142
143 public:
144 // CREATORS
145
146 /// Create an object of type `ReadOptions` having the (default)
147 /// attribute values:
148 /// @code
149 /// setAllowTrailingText() == false
150 /// maxNestedDepth() == 64
151 /// @endcode
153
154 /// Create an object of type `ReadOptions` having the value of the
155 /// specified `original` object.
156 ReadOptions(const ReadOptions& original);
157
158 /// Destroy this object.
159 ~ReadOptions();
160
161 // MANIPULATORS
162
163 /// Assign to this object the value of the specified `rhs` object and
164 /// return a non-`const` reference to this object.
165 ReadOptions& operator=(const ReadOptions& rhs);
166
167 /// Reset this object to the default value (i.e., its value upon default
168 /// construction) and return a non-`const` reference to this object.
170
171 /// Set the `allowTrailingText` attribute of this object to th specified
172 /// `value` and return a non-`const` reference to this object.
174
175 /// Set the `maxNestedDepth` attribute of this object to the specified
176 /// `value` and return a non-`const` reference to this object. The
177 /// behavior is undefined unless `0 < value`.
178 ReadOptions& setMaxNestedDepth(int value);
179
180 // ACCESSORS
181
182 /// Return the `allowTrailingText` attribute of this object.
183 bool allowTrailingText() const;
184
185 /// Return the `maxNestedDepth` attribute of this object.
186 int maxNestedDepth() const;
187
188 // Aspects
189
190 /// Format this object to the specified output `stream` at the
191 /// optionally specified indentation `level` and return a reference to
192 /// the modifiable `stream`. If `level` is specified, optionally
193 /// specify `spacesPerLevel`, the number of spaces per indentation level
194 /// for this and all of its nested objects. Each line is indented by
195 /// the absolute value of `level * spacesPerLevel`. If `level` is
196 /// negative, suppress indentation of the first line. If
197 /// `spacesPerLevel` is negative, suppress line breaks and format the
198 /// entire output on one line. If `stream` is initially invalid, this
199 /// operation has no effect. Note that a trailing newline is provided
200 /// in multiline mode only. Also note that the format is not fully
201 /// specified, and can change without notice.
202 bsl::ostream& print(bsl::ostream& stream,
203 int level = 0,
204 int spacesPerLevel = 4) const;
205};
206
207// FREE OPERATORS
208
209/// Return `true` if the specified `lhs` and `rhs` attribute objects have
210/// the same value, and `false` otherwise. Two attribute objects have the
211/// same value if each respective attribute has the same value.
212inline
213bool operator==(const ReadOptions& lhs, const ReadOptions& rhs);
214
215/// Return `true` if the specified `lhs` and `rhs` attribute objects do not
216/// have the same value, and `false` otherwise. Two attribute objects do
217/// not have the same value if one or more respective attributes do not
218/// have the same value.
219inline
220bool operator!=(const ReadOptions& lhs, const ReadOptions& rhs);
221
222/// Format the specified `rhs` to the specified output `stream` in a single
223/// line format and return a non-`const` reference to `stream`.
224inline
225bsl::ostream& operator<<(bsl::ostream& stream, const ReadOptions& rhs);
226
227// ============================================================================
228// INLINE DEFINITIONS
229// ============================================================================
230
231 // -----------------
232 // class ReadOptions
233 // -----------------
234// CREATORS
235inline
237: d_allowTrailingText(original.d_allowTrailingText)
238, d_maxNestedDepth (original.d_maxNestedDepth)
239{
240}
241
242inline
244{
245 BSLS_ASSERT(0 < d_maxNestedDepth);
246}
247
248// MANIPULATORS
249inline
251{
252 d_allowTrailingText = rhs.d_allowTrailingText;
253 d_maxNestedDepth = rhs.d_maxNestedDepth;
254
255 return *this;
256}
257
258inline
260{
261 d_allowTrailingText = value;
262 return *this;
263}
264
265inline
267{
268 BSLS_ASSERT(0 < value);
269
270 d_maxNestedDepth = value;
271 return *this;
272}
273
274// ACCESSORS
275inline
277{
278 return d_allowTrailingText;
279}
280
281inline
283{
284 return d_maxNestedDepth;
285}
286
287} // close package namespace
288
289// FREE OPERATORS
290inline
292 const bdljsn::ReadOptions& rhs)
293{
294 return lhs.maxNestedDepth() == rhs.maxNestedDepth()
295 && lhs.allowTrailingText() == rhs.allowTrailingText();
296}
297
298inline
300 const bdljsn::ReadOptions& rhs)
301{
302 return lhs.maxNestedDepth() != rhs.maxNestedDepth()
303 || lhs.allowTrailingText() != rhs.allowTrailingText();
304}
305
306inline
307bsl::ostream& bdljsn::operator<<(bsl::ostream& stream,
308 const bdljsn::ReadOptions& rhs)
309{
310 return rhs.print(stream, 0, -1);
311}
312
313
314#endif
315
316// ----------------------------------------------------------------------------
317// Copyright 2022 Bloomberg Finance L.P.
318//
319// Licensed under the Apache License, Version 2.0 (the "License");
320// you may not use this file except in compliance with the License.
321// You may obtain a copy of the License at
322//
323// http://www.apache.org/licenses/LICENSE-2.0
324//
325// Unless required by applicable law or agreed to in writing, software
326// distributed under the License is distributed on an "AS IS" BASIS,
327// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
328// See the License for the specific language governing permissions and
329// limitations under the License.
330// ----------------------------- END-OF-FILE ----------------------------------
331
332/** @} */
333/** @} */
334/** @} */
Definition bdljsn_readoptions.h:128
ReadOptions & operator=(const ReadOptions &rhs)
Definition bdljsn_readoptions.h:250
~ReadOptions()
Destroy this object.
Definition bdljsn_readoptions.h:243
ReadOptions & reset()
int maxNestedDepth() const
Return the maxNestedDepth attribute of this object.
Definition bdljsn_readoptions.h:282
static const bool s_DEFAULT_INITIALIZER_ALLOW_TRAILING_TEXT
Definition bdljsn_readoptions.h:140
static const int s_DEFAULT_INITIALIZER_MAX_NESTED_DEPTH
Definition bdljsn_readoptions.h:141
ReadOptions & setMaxNestedDepth(int value)
Definition bdljsn_readoptions.h:266
bool allowTrailingText() const
Return the allowTrailingText attribute of this object.
Definition bdljsn_readoptions.h:276
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
ReadOptions & setAllowTrailingText(bool value)
Definition bdljsn_readoptions.h:259
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdljsn_error.h:143
bool operator!=(const Error &lhs, const Error &rhs)
bool operator==(const Error &lhs, const Error &rhs)
bsl::ostream & operator<<(bsl::ostream &stream, const Error &object)