4#include "Stroika/Foundation/StroikaPreComp.h"
6#include "Stroika/Foundation/Characters/FloatConversion.h"
8#include "Stroika/Foundation/Streams/TextToBinary.h"
17using namespace Stroika::Foundation::Streams;
28 struct OptionValues_ {
29 OptionValues_ (
const Variant::JSON::Writer::Options& o)
30 : fFloatOptions{o.fFloatOptions.value_or (Characters::FloatConversion::ToStringOptions{})}
31 , fJSONPrettyPrint{o.fJSONPrettyPrint.value_or (true)}
32 , fSpacesPerIndent{o.fSpacesPerIndent.value_or (4)}
33 , fAllowNANInf{o.fAllowNANInf.value_or (true)}
34 , fLineTermination{o.fLineTermination.value_or (Characters::
kEOL<char>)}
36 if (not fJSONPrettyPrint) {
39 if (fSpacesPerIndent != 0) {
44 bool fJSONPrettyPrint;
45 unsigned int fSpacesPerIndent;
61 if (options.fSpacesPerIndent != 0) {
62 constexpr bool kSpeedTweak_ =
true;
63 if constexpr (kSpeedTweak_) {
64 for (
int i = 0; i < indentLevel; ++i) {
65 out.
Write (options.fIndentSpace);
69 out.
Write (options.fIndentSpace.Repeat (indentLevel));
86 out.
Write (
"false"sv);
92 (void)::swprintf (buf, Memory::NEltsOf (buf), L
"%lld", v);
98 (void)::swprintf (buf, Memory::NEltsOf (buf), L
"%llu", v);
105 if (isnan (v) or isinf (v)) {
106 Require (options.fAllowNANInf);
107 PrettyPrint_ (options, tmp, out);
113 template <
typename CHARITERATOR>
119 for (
auto i = start; i != end; ++i) {
152 (void)::swprintf (buf, Memory::NEltsOf (buf), L
"\\u%04x",
static_cast<char16_t> (c.
GetCharacterCode ()));
167 PrettyPrint_ (options, v.begin (), v.end (), out);
172 span<const char32_t> p = v.
GetData (&ignored);
173 PrettyPrint_ (options, p.data (), p.data () + p.size (), out);
175 void PrettyPrint_ (
const OptionValues_& options,
const vector<VariantValue>& v,
const OutputStream::Ptr<Character>& out,
int indentLevel)
178 if (options.fJSONPrettyPrint) {
179 out.
Write (options.fLineTermination);
181 for (
auto i = v.begin (); i != v.end (); ++i) {
182 Indent_ (options, out, indentLevel + 1);
183 PrettyPrint_ (options, *i, out, indentLevel + 1);
184 if (i + 1 != v.end ()) {
187 if (options.fJSONPrettyPrint) {
188 out.
Write (options.fLineTermination);
191 Indent_ (options, out, indentLevel);
197 if (options.fJSONPrettyPrint) {
198 out.
Write (options.fLineTermination);
200 for (
auto i = v.
begin (); i != v.
end ();) {
201 Indent_ (options, out, indentLevel + 1);
202 PrettyPrint_ (options, i->fKey, out, indentLevel + 1);
204 PrettyPrint_ (options, i->fValue, out, indentLevel + 1);
209 if (options.fJSONPrettyPrint) {
210 out.
Write (options.fLineTermination);
213 Indent_ (options, out, indentLevel);
218 switch (v.GetType ()) {
219 case VariantValue::eNull:
220 PrettyPrint_Null_ (options, out);
222 case VariantValue::eBoolean:
223 PrettyPrint_ (options, v.
As<
bool> (), out);
225 case VariantValue::eInteger:
226 PrettyPrint_ (options, v.
As<
long long int> (), out);
228 case VariantValue::eUnsignedInteger:
229 PrettyPrint_ (options, v.
As<
unsigned long long int> (), out);
231 case VariantValue::eFloat:
232 PrettyPrint_ (options, v.
As<
long double> (), out);
234 case VariantValue::eMap:
237 case VariantValue::eArray:
238 PrettyPrint_ (options, v.
As<vector<VariantValue>> (), out, indentLevel);
241 PrettyPrint_ (options, v.
As<
String> (), out);
251class Variant::JSON::Writer::Rep_ :
public Variant::Writer::_IRep {
253 OptionValues_ fOptions_;
254 Rep_ (
const Options& options)
258 Rep_ (
const OptionValues_& options)
262 virtual _SharedPtrIRep Clone ()
const override
264 return make_shared<Rep_> (fOptions_);
266 virtual optional<filesystem::path> GetDefaultFileSuffix ()
const override
273 PrettyPrint_ (fOptions_, v, textOut, 0);
274 if (fOptions_.fJSONPrettyPrint) {
275 textOut.
Write (fOptions_.fLineTermination);
280 PrettyPrint_ (fOptions_, v, out, 0);
284Variant::JSON::Writer::Writer (
const Options& options)
285 : inherited{make_shared<Rep_> (options)}
constexpr bool IsSurrogatePair() const
constexpr bool IsControl() const noexcept
constexpr char32_t GetCharacterCode() const noexcept
Return the char32_t UNICODE code-point associated with this character.
constexpr pair< char16_t, char16_t > GetSurrogatePair() const
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
nonvirtual span< const CHAR_T > GetData(Memory::StackBuffer< CHAR_T > *probablyIgnoredBuf) const
access a span of data located inside the StringBuilder. Return internal pointer, or pointer internal ...
nonvirtual void Append(span< const CHAR_T > s)
String is like std::u32string, except it is much easier to use, often much more space efficient,...
nonvirtual String Repeat(unsigned int count) const
static span< const CHAR_TYPE > GetData(const PeekSpanData &pds, Memory::StackBuffer< CHAR_TYPE, STACK_BUFFER_SZ > *possiblyUsedBuffer)
return the constant character data inside the string (rep) in the form of a span, possibly quickly an...
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
nonvirtual RETURNTYPE As() const
Logically halfway between std::array and std::vector; Smart 'direct memory array' - which when needed...
OutputStream<>::Ptr is Smart pointer to a stream-based sink of data.
nonvirtual void Write(span< ELEMENT_TYPE2, EXTENT_2 > elts) const
nonvirtual Iterator< T > begin() const
Support for ranged for, and STL syntax in general.
static constexpr default_sentinel_t end() noexcept
Support for ranged for, and STL syntax in general.
STRING_TYPE ToString(FLOAT_TYPE f, const ToStringOptions &options={})
static constexpr T kEOL[]
null-terminated String constant for current compiled platform - Windows (CRLF) or POSIX (NL) - macos ...