Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
Variant/JSON/Writer.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_DataExchange_Variant_JSON_Writer_h_
5#define _Stroika_Foundation_DataExchange_Variant_JSON_Writer_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include "Stroika/Foundation/Characters/FloatConversion.h"
10#include "Stroika/Foundation/Characters/LineEndings.h"
11#include "Stroika/Foundation/Common/Common.h"
15
16/**
17 * \file
18 */
19
20namespace Stroika::Foundation::DataExchange::Variant::JSON {
21
22 /**
23 * \note Our definition of the JSON format comes from:
24 * http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
25 *
26 * There are several variations. Please also note that http://jsonlint.com/ disallows
27 * json top level values other than array or object, but that doesn't appear to follow
28 * the standard as specified in ECMA.
29 *
30 * This implementation allows any top-level value (as ECMA-404 calls for).
31 *
32 * \note Though ANY VariantValue can be mapped to a JSON object, the return trip
33 * will NOT necessarily be the same as the start object, because VariantValue supports more builtin types
34 * than JSON (e.g. Date). If you map a date to JSON, and back, you will get a String.
35 *
36 * \note Writing NAN/INF values (if fAllowNANInf set - default) - transforms those values to strings, but on roundtrip
37 * automatically transforms them back to the correct IEEE floating point type (if you coerce the VariantValue
38 * return from parsing to a floating point type).
39 *
40 * \par Example Usage
41 * \code
42 * VariantValue v{3};
43 * JSON::Writer{}.Write (v, IO::FileSystem::FileOutputStream ("fred.json"));
44 * \endcode
45 *
46 * \par Example Usage
47 * \code
48 * Mapping<String, VariantValue> m;
49 * m.Add ("max-files", 10);
50 * m.Add ("max-processes", 3);
51 * stringstream stringBuf;
52 * JSON::Writer{}.Write (VariantValue{m}, Streams::iostream::OutputStreamFromStdOStream (stringBuf));
53 * \endcode
54 *
55 * \par Example Usage
56 * \code
57 * VariantValue v{3}; // or any other variant value - like a Mapping<String,VariantValue>
58 * String x = JSON::Writer {}.WriteAsString (v);
59 * \endcode
60 *
61 */
62 class Writer : public Variant::Writer {
63 private:
65
66 private:
67 class Rep_;
68
69 public:
70 DISABLE_COMPILER_GCC_WARNING_START ("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
71 DISABLE_COMPILER_CLANG_WARNING_START ("clang diagnostic ignored \"-Wdeprecated-declarations\"");
72 /**
73 */
74 struct Options {
75 /**
76 * If false, more compact, and no leading/trailing spaces.
77 */
78 bool fPrettyPrint{true};
79
80 /**
81 * Off by default, since it is costly, and rarely needed. Principally used for regression tests
82 * where we want to compare we get the same results as last time.
83 */
84 bool fCanonicalize{false};
85
86 /**
87 */
89
90 /**
91 * Ignored where fPrettyPrint is false.
92 */
93 unsigned int fSpacesPerIndent{4};
94
95 /**
96 * \note Note - NAN values (if floating point) - are also illegal, and generate a 'Require' failure.
97 * From http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf:
98 * Numeric values that cannot be represented as sequences of digits (such as Infinity and NaN) are not
99 * permitted.
100 *
101 * And yet, there is a natural way to represent the numbers.
102 * We DEFAULT to allowing this. If not allowed, then presence of NANs and INF in input, is treated as an assertion error.
103 *
104 * \note - because these would be ILLEGAL JSON, - they are written as strings. So when written, they REMAIN LEGAL JSON. And when you
105 * re-read the JSON, it will be read as a string. But when you coerce that string to a floating point type, the 'INF' or whatever,
106 * will be coerced to the proper IEEE floating point type.
107 */
108 bool fAllowNANInf{true};
109
110 /**
111 */
112 String fLineTermination{Characters::kEOL<char>};
113 };
114 DISABLE_COMPILER_GCC_WARNING_END ("GCC diagnostic ignored \"-Wdeprecated-declarations\"");
115 DISABLE_COMPILER_CLANG_WARNING_END ("clang diagnostic ignored \"-Wdeprecated-declarations\"");
116
117 public:
118#if qCompilerAndStdLib_default_member_initializer_needed_within_enclosing_class_Buggy
119 Writer ();
120 Writer (const Options& options); ///< \brief Writing as options says
121#else
122 Writer (const Options& options = {}); ///< \brief Writing as options says
123#endif
124 };
125
126}
127
128/*
129 ********************************************************************************
130 ***************************** Implementation Details ***************************
131 ********************************************************************************
132 */
133
134#endif /*_Stroika_Foundation_DataExchange_Variant_JSON_Writer_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
Writer(const Options &options={})
Writing as options says.
abstract class specifying interface for writers VariantValue objects to serialized formats like JSON,...