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