4#include "Stroika/Foundation/StroikaPreComp.h"
6#include "Stroika/Foundation/Characters/FloatConversion.h"
8#include "Stroika/Foundation/Containers/SortedMapping.h"
10#include "Stroika/Foundation/Streams/TextToBinary.h"
19using namespace Stroika::Foundation::Streams;
23using Memory::MakeSharedPtr;
32 struct OptionValues_ final : Variant::JSON::Writer::Options {
33 OptionValues_ (
const Variant::JSON::Writer::Options& o)
34 : Variant::JSON::Writer::Options{o}
36 if (not fPrettyPrint) {
39 if (fSpacesPerIndent != 0) {
56 if (options.fSpacesPerIndent != 0) {
57 constexpr bool kSpeedTweak_ =
true;
58 if constexpr (kSpeedTweak_) {
59 for (
int i = 0; i < indentLevel; ++i) {
60 out.
Write (options.fIndentSpace);
64 out.
Write (options.fIndentSpace.Repeat (indentLevel));
81 out.
Write (
"false"sv);
87 (void)::swprintf (buf, std::size (buf), L
"%lld", v);
93 (void)::swprintf (buf, std::size (buf), L
"%llu", v);
100 if (isnan (v) or isinf (v)) {
101 Require (options.fAllowNANInf);
102 PrettyPrint_ (options, tmp, out);
108 template <
typename CHARITERATOR>
114 for (
auto i = start; i != end; ++i) {
147 (void)::swprintf (buf, std::size (buf), L
"\\u%04x",
static_cast<char16_t> (c.
GetCharacterCode ()));
162 PrettyPrint_ (options, v.begin (), v.end (), out);
167 span<const char32_t> p = v.
GetData (&ignored);
168 PrettyPrint_ (options, p.data (), p.data () + p.size (), out);
170 void PrettyPrint_ (
const OptionValues_& options,
const vector<VariantValue>& v,
const OutputStream::Ptr<Character>& out,
int indentLevel)
173 if (options.fPrettyPrint) {
174 out.
Write (options.fLineTermination);
176 for (
auto i = v.begin (); i != v.end (); ++i) {
177 Indent_ (options, out, indentLevel + 1);
178 PrettyPrint_ (options, *i, out, indentLevel + 1);
179 if (i + 1 != v.end ()) {
182 if (options.fPrettyPrint) {
183 out.
Write (options.fLineTermination);
186 Indent_ (options, out, indentLevel);
192 if (options.fPrettyPrint) {
193 out.
Write (options.fLineTermination);
196 if (options.fCanonicalize) {
199 for (
auto i = vv.begin (); i != vv.end ();) {
200 Indent_ (options, out, indentLevel + 1);
201 PrettyPrint_ (options, i->fKey, out, indentLevel + 1);
203 PrettyPrint_ (options, i->fValue, out, indentLevel + 1);
208 if (options.fPrettyPrint) {
209 out.
Write (options.fLineTermination);
212 Indent_ (options, out, indentLevel);
217 switch (v.GetType ()) {
218 case VariantValue::eNull:
219 PrettyPrint_Null_ (options, out);
221 case VariantValue::eBoolean:
222 PrettyPrint_ (options, v.
As<
bool> (), out);
224 case VariantValue::eInteger:
225 PrettyPrint_ (options, v.
As<
long long int> (), out);
227 case VariantValue::eUnsignedInteger:
228 PrettyPrint_ (options, v.
As<
unsigned long long int> (), out);
230 case VariantValue::eFloat:
231 PrettyPrint_ (options, v.
As<
long double> (), out);
233 case VariantValue::eMap:
236 case VariantValue::eArray:
237 PrettyPrint_ (options, v.
As<vector<VariantValue>> (), out, indentLevel);
240 PrettyPrint_ (options, v.
As<
String> (), out);
252 OptionValues_ fOptions_;
253 Rep_ (
const Options& options)
257 Rep_ (
const OptionValues_& options)
261 virtual optional<filesystem::path> GetDefaultFileSuffix ()
const override
268 PrettyPrint_ (fOptions_, v, textOut, 0);
269 if (fOptions_.fPrettyPrint) {
270 textOut.
Write (fOptions_.fLineTermination);
275 PrettyPrint_ (fOptions_, v, out, 0);
279#if qCompilerAndStdLib_default_member_initializer_needed_within_enclosing_class_Buggy
280Variant::JSON::Writer::Writer ()
286Variant::JSON::Writer::Writer (
const Options& options)
auto MakeSharedPtr(ARGS_TYPE &&... args) -> shared_ptr< T >
same as make_shared, but if type T has block allocation, then use block allocation for the 'shared pa...
conditional_t< qStroika_Foundation_Memory_PreferBlockAllocation and andTrueCheck, BlockAllocationUseHelper< T >, Common::Empty > UseBlockAllocationIfAppropriate
Use this to enable block allocation for a particular class. Beware of subclassing.
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...
A Mapping uniquely associates two elements: a key and a value (use Assocation to allow multiple value...
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
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={})
Ptr New(const Streams::OutputStream::Ptr< byte > &src, const Characters::CodeCvt<> &char2OutputConverter)