BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdljsn_jsonutil.h
Go to the documentation of this file.
1/// @file bdljsn_jsonutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdljsn_jsonutil.h -*-C++-*-
8#ifndef INCLUDED_BDLJSN_JSONUTIL
9#define INCLUDED_BDLJSN_JSONUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdljsn_jsonutil bdljsn_jsonutil
15/// @brief Provide common non-primitive operations on `Json` objects.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdljsn
19/// @{
20/// @addtogroup bdljsn_jsonutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdljsn_jsonutil-purpose"> Purpose</a>
25/// * <a href="#bdljsn_jsonutil-classes"> Classes </a>
26/// * <a href="#bdljsn_jsonutil-description"> Description </a>
27/// * <a href="#bdljsn_jsonutil-configuring-the-output-format"> Configuring the Output Format </a>
28/// * <a href="#bdljsn_jsonutil-handling-of-duplicate-keys"> Handling of Duplicate Keys </a>
29/// * <a href="#bdljsn_jsonutil-allowing-trailing-text"> Allowing Trailing Text </a>
30/// * <a href="#bdljsn_jsonutil-usage"> Usage </a>
31/// * <a href="#bdljsn_jsonutil-example-1-reading-and-writing-json-data"> Example 1: Reading and Writing JSON Data </a>
32/// * <a href="#bdljsn_jsonutil-example-2-the-effect-of-options-on-write"> Example 2: The Effect of options on write </a>
33/// * <a href="#bdljsn_jsonutil-sortmembers"> sortMembers </a>
34/// * <a href="#bdljsn_jsonutil-style-and-style-related-options"> style And style-related options </a>
35///
36/// # Purpose {#bdljsn_jsonutil-purpose}
37/// Provide common non-primitive operations on `Json` objects.
38///
39/// # Classes {#bdljsn_jsonutil-classes}
40///
41/// - bdljsn::JsonUtil: namespace for non-primitive operations on `Json` objects
42///
43/// # Description {#bdljsn_jsonutil-description}
44/// This component provides a namespace `bdljsn::JsonUtil`
45/// containing utility functions that operate on `Json` objects.
46///
47/// The following methods are provided by `JsonUtil`:
48/// * `read` populate a `Json` object from a JSON text document.
49/// * `write` populate a JSON text document from a `Json` object.
50///
51/// ## Configuring the Output Format {#bdljsn_jsonutil-configuring-the-output-format}
52///
53///
54/// There are a number of options to configure the output format produced by
55/// `write`:
56/// * `sortMembers`: sort the members of any Object elements in the output JSON
57/// (default: `false`)
58/// * `style`: the style of the resulting output
59/// - `e_COMPACT` (default): render with no added white space
60/// - `e_ONELINE`: render a human readable single-line format (e.g., for
61/// logging)
62/// - `e_PRETTY`: render a multi-line human readable format
63/// * `spacesPerLevel`: for the `e_PRETTY` style, the number of spaces added
64/// for each additional nesting level (default 4)
65/// * `initialIndentationLevel`: for the `e_PRETTY` style, the number of sets
66/// of `spacesPerLevel` spaces added to every line of the output, including
67/// the first and last lines (default 0)
68///
69/// The example below shows the various write styles:
70/// @code
71/// Compact:
72/// {"a":1,"b":[]}
73///
74/// One-line:
75/// {"a": 1, "b": []}
76///
77/// Pretty:
78/// {
79/// "a": 1,
80/// "b": []
81/// }
82/// @endcode
83/// For more information, see the @ref bdljsn_writeoptions and @ref bdljsn_writestyle
84/// components.
85///
86/// ## Handling of Duplicate Keys {#bdljsn_jsonutil-handling-of-duplicate-keys}
87///
88///
89/// `bdljsn::JsonObject` represents a JSON Object having unique keys. If an
90/// Object with duplicate keys is found in a JSON document, `read` will preserve
91/// the value associated with the FIRST instance of that key.
92///
93/// Per the JSON RFC (https://www.rfc-editor.org/rfc/rfc8259#section-4):
94/// @code
95/// "The names within an object SHOULD be unique."
96/// @endcode
97/// That is, the expectation is that a JSON document should have unique keys,
98/// and JSON documents with duplicate keys are not an interoperable
99/// representation. JSON parsing implementations vary on how duplicate keys are
100/// handled (though many represent the object in-memory with unique keys). Note
101/// that preserving the value of the first key is consistent with the behavior
102/// of the existing `baljsn::DatumUtil` component.
103///
104/// ## Allowing Trailing Text {#bdljsn_jsonutil-allowing-trailing-text}
105///
106///
107/// By default, `bdljsn::JsonUtil::read` will report an error for input where a
108/// valid JSON document is followed by additional text unless the trailing text
109/// consists solely of white space characters. This behavior is configured by
110/// the `bdljsn::ReadOptions` attribute, "allowTrailingText" (which defaults to
111/// `false`).
112///
113/// If "allowTrailingText" is `true`, then `bdljsn::JsonUtil::read` will return
114/// success where a valid JSON document is followed by additional text as long
115/// as that text is separated from the valid JSON by a delimiter character
116/// (i.e., either the JSON text ends in a delimiter, or the text that follows
117/// starts with a delimiter). Here, delimiters are white-space characters,
118/// `[`,`]`,`{`,`}`,`,`, or `"`. Per RFC 8259, white space characters are
119/// Space (0x20), Horizontal tab (0x09), New Line (0x0A), and Carriage Return
120/// (0x0D).
121///
122/// The table below shows some examples:
123/// @code
124/// * "ATT" = "allowTrailingText"
125/// * Document is only valid where the result is SUCCESS
126///
127/// +-----------+------------------------+-------------+-----------+
128/// | Input | ATT = false (default) | ATT = true | Document |
129/// +===========+========================+=============+===========+
130/// | '[]' | SUCCESS | SUCCESS | [] |
131/// | '[] ' | SUCCESS | SUCCESS | [] |
132/// | '[],' | ERROR | SUCCESS | [] |
133/// | '[]a' | ERROR | SUCCESS | [] |
134/// | 'false ' | SUCCESS | SUCCESS | false |
135/// | 'false,' | ERROR | SUCCESS | false |
136/// | 'falsea' | ERROR | ERROR | |
137/// | '"a"x' | ERROR | SUCCESS | "a" |
138/// +-----------+------------------------+-------------+-----------+
139/// @endcode
140///
141/// ## Usage {#bdljsn_jsonutil-usage}
142///
143///
144/// This section illustrates the intended use of this component.
145///
146/// ### Example 1: Reading and Writing JSON Data {#bdljsn_jsonutil-example-1-reading-and-writing-json-data}
147///
148///
149/// This component provides methods for reading and writing JSON data to/from
150/// `Json` objects.
151///
152/// First, we define the JSON data we plan to read:
153/// @code
154/// const char *INPUT_JSON = R"JSON({
155/// "a boolean": true,
156/// "a date": "1970-01-01",
157/// "a number": 2.1,
158/// "an integer": 10,
159/// "array of values": [
160/// -1,
161/// 0,
162/// 2.718281828459045,
163/// 3.1415926535979,
164/// "abc",
165/// true
166/// ],
167/// "event": {
168/// "date": "1969-07-16",
169/// "description": "Apollo 11 Moon Landing",
170/// "passengers": [
171/// "Neil Armstrong",
172/// "Buzz Aldrin"
173/// ],
174/// "success": true
175/// }
176/// }})JSON";
177/// @endcode
178/// Next, we read the JSON data into a `Json` object:
179/// @code
180/// bdljsn::Json result;
181/// bdljsn::Error error;
182///
183/// int rc = bdljsn::JsonUtil::read(&result, &error, INPUT_JSON);
184///
185/// assert(0 == rc);
186///
187/// if (0 != rc) {
188/// bsl::cout << "Error message: \"" << error.message() << "\""
189/// << bsl::endl;
190/// }
191/// @endcode
192/// Then, we check the values of a few selected fields:
193/// @code
194/// assert(result.type() == JsonType::e_OBJECT);
195/// assert(result["array of values"][2].theNumber().asDouble()
196/// == 2.718281828459045);
197/// assert(result["event"]["date"].theString() == "1969-07-16");
198/// assert(result["event"]["passengers"][1].theString() == "Buzz Aldrin");
199/// @endcode
200/// Finally, we'll `write` the `result` back into another string and make sure
201/// we got the same value back, by using the correct `WriteOptions` to match
202/// the input format:
203/// @code
204/// bsl::string resultString;
205///
206/// // Set the WriteOptions to match the initial style:
207/// WriteOptions writeOptions;
208/// writeOptions.setStyle(bdljsn::WriteStyle::e_PRETTY);
209/// writeOptions.setInitialIndentLevel(0);
210/// writeOptions.setSpacesPerLevel(2);
211/// writeOptions.setSortMembers(true);
212///
213/// bdljsn::JsonUtil::write(&resultString, result, writeOptions);
214///
215/// assert(resultString == INPUT_JSON);
216/// @endcode
217///
218/// ### Example 2: The Effect of options on write {#bdljsn_jsonutil-example-2-the-effect-of-options-on-write}
219///
220///
221/// By populating a `WriteOptions` object and passing it to `write`, the format
222/// of the resulting JSON can be controlled.
223///
224/// First, let's populate a `Json` object named `json` from an input string
225/// using `read`, and create an empty `options` (see `bdljsn::WriteOptions`):
226/// @code
227/// const bsl::string JSON = R"JSON(
228/// {
229/// "a" : 1,
230/// "b" : []
231/// }
232/// )JSON";
233///
234/// bdljsn::Json json;
235/// bdljsn::WriteOptions options;
236///
237/// int rc = bdljsn::JsonUtil::read(&json, JSON);
238///
239/// assert(0 == rc);
240/// @endcode
241/// There are 4 options, which can be broken down into 2 unrelated sets.
242///
243/// The first set consists of the `sortMembers` option, which controls whether
244/// members of objects are printed in lexicographical order.
245///
246/// The second set consists of the `style`, `initialIndentLevel`, and
247/// `spacesPerLevel` options - `style` controls the format used to render a
248/// `Json`, and, if `bdljsn::WriteStyle::e_PRETTY == options.style()`, the
249/// `spacesPerLevel` and `initialIndentLevel` options are used to control the
250/// indentation of the output. For any other value of `options.style()`, the
251/// `spacesPerLevel` and `initialIndentLevel` options have no effect.
252///
253/// #### sortMembers {#bdljsn_jsonutil-sortmembers}
254///
255///
256/// If `sortMembers` is true, then the members of an object output by `write`
257/// will be in sorted order. Otherwise, the elements are written in an
258/// (implementation defined) order (that may change).
259///
260/// The `sortMembers` option defaults to `false` for performance reasons, but
261/// applications that rely on stable output text should set `sortMembers` to
262/// `true` (e.g., in a test where the resulting JSON text is compared for
263/// equality) .
264///
265/// Here, we set `sortMembers` to `true`, and verify the resulting JSON text
266/// matches the expected text:
267/// @code
268/// options.setSortMembers(true);
269/// bsl::string output;
270///
271/// rc = bdljsn::JsonUtil::write(&output, json, options);
272///
273/// assert(0 == rc);
274/// assert(R"JSON({"a":1,"b":[]})JSON" == output);
275/// @endcode
276/// Had we not specified `setSortMembers(true)`, the order of the "a" and "b"
277/// members in the `output` string would be unpredictable.
278///
279/// #### style And style-related options {#bdljsn_jsonutil-style-and-style-related-options}
280///
281///
282/// There are 3 options for `style` (see `bdljsn::WriteStyle`):
283/// * bdljsn::WriteStyle::e_COMPACT
284/// * bdljsn::WriteStyle::e_ONELINE
285/// * bdljsn::WriteStyle::e_PRETTY
286///
287/// Next, we write `json` using the style `e_COMPACT` (the default), a single
288/// line presentation with no added spaces after `:` and `,` elements.
289/// @code
290/// rc = bdljsn::JsonUtil::write(&output, json, options);
291///
292/// assert(0 == rc);
293///
294/// // Using 'e_COMPACT' style:
295/// assert(R"JSON({"a":1,"b":[]})JSON" == output);
296/// @endcode
297/// Next, we write `json` using the `e_ONELINE` style, another single line
298/// format, which adds single ` ` characters after `:` and `,` elements for
299/// readability.
300/// @code
301/// options.setStyle(bdljsn::WriteStyle::e_ONELINE);
302/// rc = bdljsn::JsonUtil::write(&output, json, options);
303///
304/// assert(0 == rc);
305///
306/// // Using 'e_ONELINE' style:
307/// assert(R"JSON({"a": 1, "b": []})JSON" == output);
308/// @endcode
309/// Next, we write `json` using the `e_PRETTY` style, a multiline format where
310/// newlines are introduced after each (non-terminal) `{`, `[`, `,`, `]`, and
311/// `}` character. Furthermore, the indentation of JSON rendered in the
312/// `e_PRETTY` style is controlled by the other 2 attributes, `spacesPerLevel`
313/// and `initialIndentLevel`.
314///
315/// `e_PRETTY` styling does not add a newline to the end of the output.
316///
317/// `spacesPerLevel` controls the number of spaces added for each successive
318/// indentation level - e.g., if `spacesPerLevel` is 2, then each nesting level
319/// of the rendered JSON is indented 2 spaces.
320///
321/// `initialIndentLevel` controls how much the entire JSON output is indented.
322/// It defaults to 0 - if it's a positive value, then the entire JSON is
323/// indented by `initialIndentLevel * spacesPerLevel` spaces.
324/// @code
325/// options.setStyle(bdljsn::WriteStyle::e_PRETTY);
326/// options.setSpacesPerLevel(4); // the default
327/// options.setInitialIndentLevel(0); // the default
328///
329/// rc = bdljsn::JsonUtil::write(&output, json, options);
330///
331/// assert(0 == rc);
332///
333/// // Using 'e_PRETTY' style:
334/// assert(R"JSON({
335/// "a": 1,
336/// "b": []
337/// })JSON" == output);
338/// @endcode
339/// Finally, if we set `initialIndentLevel` to 1, then an extra set of 4 spaces
340/// is prepended to each line, where 4 is the value of `spacesPerLevel`:
341/// @code
342/// options.setInitialIndentLevel(1);
343///
344/// rc = bdljsn::JsonUtil::write(&output, json, options);
345///
346/// assert(0 == rc);
347///
348/// // Using 'e_PRETTY' style (with 'initialIndentLevel' as 1):
349/// assert(R"JSON({
350/// "a": 1,
351/// "b": []
352/// })JSON" == output);
353/// @endcode
354/// @}
355/** @} */
356/** @} */
357
358/** @addtogroup bdl
359 * @{
360 */
361/** @addtogroup bdljsn
362 * @{
363 */
364/** @addtogroup bdljsn_jsonutil
365 * @{
366 */
367
368#include <bdlscm_version.h>
369
370#include <bdljsn_error.h>
371#include <bdljsn_json.h>
372#include <bdljsn_readoptions.h>
373#include <bdljsn_writeoptions.h>
374
376
377#include <bslmf_movableref.h>
378
379#include <bsls_libraryfeatures.h>
380
381#include <bsl_cstdint.h>
382#include <bsl_iosfwd.h>
383#include <bsl_sstream.h>
384#include <bsl_string.h>
385#include <bsl_string_view.h>
386
387
388namespace bdljsn {
389
390 // ===============
391 // struct JsonUtil
392 // ===============
393
394/// This `struct` provides a namespace for utility functions that provide
395/// `read` and `write` operations to/from `Json` objects.
396struct JsonUtil {
397
398 // TYPES
400
401 // CLASS METHODS
402
403 static int read(Json *result,
404 bsl::istream& input);
405 static int read(Json *result,
406 bsl::istream& input,
407 const ReadOptions& options);
408 static int read(Json *result,
409 bsl::streambuf *input);
410 static int read(Json *result,
411 bsl::streambuf *input,
412 const ReadOptions& options);
413 static int read(Json *result,
414 const bsl::string_view& input);
415 static int read(Json *result,
416 const bsl::string_view& input,
417 const ReadOptions& options);
418 static int read(Json *result,
419 Error *errorDescription,
420 bsl::istream& input);
421 static int read(Json *result,
422 Error *errorDescription,
423 bsl::istream& input,
424 const ReadOptions& options);
425 static int read(Json *result,
426 Error *errorDescription,
427 bsl::streambuf *input);
428 static int read(Json *result,
429 Error *errorDescription,
430 bsl::streambuf *input,
431 const ReadOptions& options);
432 static int read(Json *result,
433 Error *errorDescription,
434 const bsl::string_view& input);
435 /// Load to the specified `result` a value-semantic representation of
436 /// the JSON text in the specified `input`. Optionally specify an
437 /// `errorDescription` that, if an error occurs, is loaded with a
438 /// description of the error. Optionally specify `options` which allow
439 /// altering the maximum nesting depth. Return 0 on success, and a
440 /// non-zero value if `input` does not consist of valid JSON text or an
441 /// error occurs when reading from `input`. If
442 /// `options.allowTrailingText()` is `false` (the default), then an
443 /// error will be reported if a valid JSON text is followed by any text
444 /// that does not consist solely of white-space characters. If
445 /// `options.allowTrailingText()` is `true`, then this function will
446 /// return success where a valid JSON document is followed by additional
447 /// text as long as that text is separated from the valid JSON by a
448 /// delimiter character (i.e., either the JSON text ends in a delimiter,
449 /// or the text that follows starts with a delimiter). Here, delimiters
450 /// are white-space characters, `[`,`]`,`{`,`}`,`,`, or `"`.
451 static int read(Json *result,
452 Error *errorDescription,
453 const bsl::string_view& input,
454 const ReadOptions& options);
455
456 /// Print, to the specified `stream`, a description of the specified
457 /// `error`, containing the line and column in the specified `input`
458 /// where the `error` occurred. Return a reference to the modifiable
459 /// `stream`. If `error.location()` does not refer to a valid location
460 /// in `input` an unspecified error description will be written to
461 /// `stream`. Note that the caller should ensure `input` refers to the
462 /// same input position as when `input` was supplied to `read` (or
463 /// whatever operation created `error`).
464 static bsl::ostream& printError(bsl::ostream& stream,
465 bsl::istream& input,
466 const Error& error);
467 static bsl::ostream& printError(bsl::ostream& stream,
468 bsl::streambuf *input,
469 const Error& error);
470 static bsl::ostream& printError(bsl::ostream& stream,
471 const bsl::string_view& input,
472 const Error& error);
473
474 static int write(bsl::ostream& output,
475 const Json& json);
476 static int write(bsl::ostream& output,
477 const Json& json,
478 const WriteOptions& options);
479 static int write(bsl::streambuf *output,
480 const Json& json);
481 static int write(bsl::streambuf *output,
482 const Json& json,
483 const WriteOptions& options);
484 static int write(bsl::string *output,
485 const Json& json);
486 static int write(bsl::string *output,
487 const Json& json,
488 const WriteOptions& options);
489#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
490 static int write(std::pmr::string *output,
491 const Json& json);
492 static int write(std::pmr::string *output,
493 const Json& json,
494 const WriteOptions& options);
495#endif
496 static int write(std::string *output,
497 const Json& json);
498 /// Write to the specified `output` a JSON text representation of the
499 /// specified `json` document, using the optionally specified `options`
500 /// for formatting the resulting text. Return 0 on success, and a
501 /// non-zero value otherwise. Note that this operation will report an
502 /// error only if there is an error writing to `output`.
503 static int write(std::string *output,
504 const Json& json,
505 const WriteOptions& options);
506};
507
508// ============================================================================
509// INLINE DEFINITIONS
510// ============================================================================
511
512 // --------------
513 // class JsonUtil
514 // --------------
515// CLASS METHODS
516inline
518 bsl::istream& input,
519 const ReadOptions& options)
520{
521 Error tmp;
522 return read(result, &tmp, input, options);
523}
524
525inline
527 bsl::istream& input)
528{
529 ReadOptions options;
530 return read(result, input, options);
531}
532
533inline
535 bsl::streambuf *input,
536 const ReadOptions& options)
537{
538 Error tmp;
539 return read(result, &tmp, input, options);
540}
541
542inline
544 bsl::streambuf *input)
545{
546 ReadOptions options;
547 return read(result, input, options);
548}
549
550inline
552 const bsl::string_view& input,
553 const ReadOptions& options)
554{
555 Error tmp;
556 return read(result, &tmp, input, options);
557}
558
559inline
561 const bsl::string_view& input)
562{
563 ReadOptions options;
564 return read(result, input, options);
565}
566
567inline
569 Error *errorDescription,
570 bsl::istream& input,
571 const ReadOptions& options)
572{
573
574 return read(result, errorDescription, input.rdbuf(), options);
575}
576
577inline
579 Error *errorDescription,
580 bsl::istream& input)
581{
582 ReadOptions options;
583
584 return read(result, errorDescription, input, options);
585}
586
587inline
589 Error *errorDescription,
590 bsl::streambuf *input)
591{
592 ReadOptions options;
593
594 return read(result, errorDescription, input, options);
595}
596
597inline
599 Error *errorDescription,
600 const bsl::string_view& input,
601 const ReadOptions& options)
602{
603 bdlsb::FixedMemInStreamBuf inputBuf(input.data(), input.size());
604 return read(result, errorDescription, &inputBuf, options);
605}
606
607inline
609 Error *errorDescription,
610 const bsl::string_view& input)
611{
612 ReadOptions options;
613 return read(result, errorDescription, input, options);
614}
615
616inline
617bsl::ostream& JsonUtil::printError(bsl::ostream& stream,
618 bsl::istream& input,
619 const Error& error)
620{
621 return printError(stream, input.rdbuf(), error);
622}
623
624inline
625bsl::ostream& JsonUtil::printError(bsl::ostream& stream,
626 const bsl::string_view& input,
627 const Error& error)
628{
629 bdlsb::FixedMemInStreamBuf inputBuf(input.data(), input.size());
630 return printError(stream, &inputBuf, error);
631}
632
633inline
634int JsonUtil::write(bsl::streambuf *output,
635 const Json& json,
636 const WriteOptions& options)
637{
638 bsl::ostream outputStream(output);
639 return write(outputStream, json, options);
640}
641
642inline
643int JsonUtil::write(bsl::streambuf *output,
644 const Json& json)
645{
646 WriteOptions options;
647 return write(output, json, options);
648}
649
650inline
652 const Json& json,
653 const WriteOptions& options)
654{
655 bsl::ostringstream stream(output->get_allocator());
656
657 int rc = write(stream, json, options);
658 if (0 == rc) {
659#ifdef BSLS_PLATFORM_CMP_SUN
660 const bsl::string &tmpOutput = stream.str();
661#else
662 bsl::string tmpOutput = stream.str(output->get_allocator());
663#endif
664 *output = bslmf::MovableRefUtil::move(tmpOutput);
665 }
666 return rc;
667}
668
669inline
671 const Json& json)
672{
673 WriteOptions options;
674
675 return write(output, json, options);
676}
677
678#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
679inline
680int JsonUtil::write(std::pmr::string *output,
681 const Json& json,
682 const WriteOptions& options)
683{
684#if defined (BSLS_LIBRARYFEATURES_HAS_CPP20_BASELINE_LIBRARY)
685 typedef std::basic_ostringstream<char,
686 std::char_traits<char>,
687 std::pmr::polymorphic_allocator<char> >
688 PmrOstringStream;
689
690 PmrOstringStream stream(std::ios_base::out, output->get_allocator());
691 int rc = write(stream, json, options);
692 if (0 == rc) {
693 std::pmr::string tmpOutput = stream.str(output->get_allocator());
694 *output = bslmf::MovableRefUtil::move(tmpOutput);
695 }
696 return rc;
697#else
698 std::ostringstream stream;
699 int rc = write(stream, json, options);
700 if (0 == rc) {
701 const bsl::string &tmpOutput = stream.str();
702 *output = bslmf::MovableRefUtil::move(tmpOutput);
703 }
704 return rc;
705#endif
706}
707#endif // defined(BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING)
708
709#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
710inline
711int JsonUtil::write(std::pmr::string *output,
712 const Json& json)
713{
714 WriteOptions options;
715
716 return write(output, json, options);
717}
718#endif // defined(BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING)
719
720inline
721int JsonUtil::write(std::string *output,
722 const Json& json,
723 const WriteOptions& options)
724{
725 std::ostringstream stream;
726
727 int rc = write(stream, json, options);
728 if (0 == rc) {
729 std::string tmpOutput(stream.str());
730 *output = bslmf::MovableRefUtil::move(tmpOutput);
731 }
732 return rc;
733}
734
735inline
736int JsonUtil::write(std::string *output,
737 const Json& json)
738{
739 WriteOptions options;
740
741 return write(output, json, options);
742}
743
744inline
745int JsonUtil::write(bsl::ostream& output, const Json& json)
746{
747 WriteOptions options;
748 return write(output, json, options);
749}
750
751} // close package namespace
752
753
754#endif // INCLUDED_BDLJSN_JSONUTIL
755
756// ----------------------------------------------------------------------------
757// Copyright 2022 Bloomberg Finance L.P.
758//
759// Licensed under the Apache License, Version 2.0 (the "License");
760// you may not use this file except in compliance with the License.
761// You may obtain a copy of the License at
762//
763// http://www.apache.org/licenses/LICENSE-2.0
764//
765// Unless required by applicable law or agreed to in writing, software
766// distributed under the License is distributed on an "AS IS" BASIS,
767// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
768// See the License for the specific language governing permissions and
769// limitations under the License.
770// ----------------------------- END-OF-FILE ----------------------------------
771
772/** @} */
773/** @} */
774/** @} */
Definition bdljsn_error.h:157
Definition bdljsn_json.h:1110
Definition bdljsn_readoptions.h:128
Definition bdljsn_writeoptions.h:134
Definition bdlsb_fixedmeminstreambuf.h:187
Definition bslstl_ostringstream.h:175
void str(const StringType &value)
Definition bslstl_ostringstream.h:581
Definition bslstl_stringview.h:441
BSLS_KEYWORD_CONSTEXPR const_pointer data() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:1760
BSLS_KEYWORD_CONSTEXPR size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the length of this view.
Definition bslstl_stringview.h:1676
Definition bslstl_string.h:1281
allocator_type get_allocator() const BSLS_KEYWORD_NOEXCEPT
Return the allocator used by this string to supply memory.
Definition bslstl_string.h:6723
Definition bslstl_pair.h:1210
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdljsn_error.h:143
Definition bdljsn_jsonutil.h:396
static int write(bsl::ostream &output, const Json &json, const WriteOptions &options)
static int write(bsl::ostream &output, const Json &json)
Definition bdljsn_jsonutil.h:745
static bsl::ostream & printError(bsl::ostream &stream, bsl::istream &input, const Error &error)
Definition bdljsn_jsonutil.h:617
static bsl::ostream & printError(bsl::ostream &stream, bsl::streambuf *input, const Error &error)
bsl::pair< bsl::uint64_t, bsl::uint64_t > LineAndColumnNumber
Definition bdljsn_jsonutil.h:399
static int read(Json *result, bsl::istream &input)
Definition bdljsn_jsonutil.h:526
static int read(Json *result, Error *errorDescription, bsl::streambuf *input, const ReadOptions &options)
static MovableRef< t_TYPE > move(t_TYPE &reference) BSLS_KEYWORD_NOEXCEPT
Definition bslmf_movableref.h:1060