BDE 4.39.x 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 options
122/// for reading a JSON document. See the @ref bdljsn_readoptions-attributes for information on the class attributes.
123///
124/// \note Note that the class invariants are identically the
125/// 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) attribute
147 /// 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.
177 ///
178 /// \pre The behavior is undefined unless `0 < value`.
179 ReadOptions& setMaxNestedDepth(int value);
180
181 // ACCESSORS
182
183 /// Return the `allowTrailingText` attribute of this object.
184 bool allowTrailingText() const;
185
186 /// Return the `maxNestedDepth` attribute of this object.
187 int maxNestedDepth() const;
188
189 // Aspects
190
191 /// Format this object to the specified output `stream` at the optionally
192 /// specified indentation `level` and return a reference to the modifiable
193 /// `stream`. If `level` is specified, optionally specify
194 /// `spacesPerLevel`, the number of spaces per indentation level for this
195 /// and all of its nested objects. Each line is indented by the absolute
196 /// value of `level * spacesPerLevel`. If `level` is negative, suppress
197 /// indentation of the first line. If `spacesPerLevel` is negative,
198 /// suppress line breaks and format the entire output on one line. If
199 /// `stream` is initially invalid, this operation has no effect.
200 ///
201 /// \note Note that a trailing newline is provided in multiline mode only. Also note that
202 /// the format is not fully specified, and can change without notice.
203 bsl::ostream& print(bsl::ostream& stream,
204 int level = 0,
205 int spacesPerLevel = 4) const;
206};
207
208// FREE OPERATORS
209
210/// Return `true` if the specified `lhs` and `rhs` attribute objects have the
211/// same value, and `false` otherwise. Two attribute objects have the same
212/// value if each respective attribute has the same value.
213inline
214bool operator==(const ReadOptions& lhs, const ReadOptions& rhs);
215
216/// Return `true` if the specified `lhs` and `rhs` attribute objects do not
217/// have the same value, and `false` otherwise. Two attribute objects do not
218/// have the same value if one or more respective attributes do not have the
219/// same value.
220inline
221bool operator!=(const ReadOptions& lhs, const ReadOptions& rhs);
222
223/// Format the specified `rhs` to the specified output `stream` in a single
224/// line format and return a non-`const` reference to `stream`.
225inline
226bsl::ostream& operator<<(bsl::ostream& stream, const ReadOptions& rhs);
227
228// ============================================================================
229// INLINE DEFINITIONS
230// ============================================================================
231
232 // -----------------
233 // class ReadOptions
234 // -----------------
235// CREATORS
236inline
238: d_allowTrailingText(original.d_allowTrailingText)
239, d_maxNestedDepth (original.d_maxNestedDepth)
240{
241}
242
243inline
245{
246 BSLS_ASSERT(0 < d_maxNestedDepth);
247}
248
249// MANIPULATORS
250inline
252{
253 d_allowTrailingText = rhs.d_allowTrailingText;
254 d_maxNestedDepth = rhs.d_maxNestedDepth;
255
256 return *this;
257}
258
259inline
261{
262 d_allowTrailingText = value;
263 return *this;
264}
265
266inline
268{
269 BSLS_ASSERT(0 < value);
270
271 d_maxNestedDepth = value;
272 return *this;
273}
274
275// ACCESSORS
276inline
278{
279 return d_allowTrailingText;
280}
281
282inline
284{
285 return d_maxNestedDepth;
286}
287
288} // close package namespace
289
290// FREE OPERATORS
291inline
293 const bdljsn::ReadOptions& rhs)
294{
295 return lhs.maxNestedDepth() == rhs.maxNestedDepth()
296 && lhs.allowTrailingText() == rhs.allowTrailingText();
297}
298
299inline
301 const bdljsn::ReadOptions& rhs)
302{
303 return lhs.maxNestedDepth() != rhs.maxNestedDepth()
304 || lhs.allowTrailingText() != rhs.allowTrailingText();
305}
306
307inline
308bsl::ostream& bdljsn::operator<<(bsl::ostream& stream,
309 const bdljsn::ReadOptions& rhs)
310{
311 return rhs.print(stream, 0, -1);
312}
313
314
315#endif
316
317// ----------------------------------------------------------------------------
318// Copyright 2022 Bloomberg Finance L.P.
319//
320// Licensed under the Apache License, Version 2.0 (the "License");
321// you may not use this file except in compliance with the License.
322// You may obtain a copy of the License at
323//
324// http://www.apache.org/licenses/LICENSE-2.0
325//
326// Unless required by applicable law or agreed to in writing, software
327// distributed under the License is distributed on an "AS IS" BASIS,
328// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
329// See the License for the specific language governing permissions and
330// limitations under the License.
331// ----------------------------- END-OF-FILE ----------------------------------
332
333/** @} */
334/** @} */
335/** @} */
Definition bdljsn_readoptions.h:128
ReadOptions & operator=(const ReadOptions &rhs)
Definition bdljsn_readoptions.h:251
~ReadOptions()
Destroy this object.
Definition bdljsn_readoptions.h:244
ReadOptions & reset()
int maxNestedDepth() const
Return the maxNestedDepth attribute of this object.
Definition bdljsn_readoptions.h:283
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:267
bool allowTrailingText() const
Return the allowTrailingText attribute of this object.
Definition bdljsn_readoptions.h:277
bsl::ostream & print(bsl::ostream &stream, int level=0, int spacesPerLevel=4) const
ReadOptions & setAllowTrailingText(bool value)
Definition bdljsn_readoptions.h:260
#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 bdljsn_error.h:142
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)
ALLOCATOR const STRING_VIEW_LIKE_TYPE & rhs
Definition bslstl_string.h:3918
ALLOCATOR & lhs
Definition bslstl_string.h:3917