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 {
33 OptionValues_ (
const Variant::JSON::Writer::Options& o)
34 : fFloatOptions{o.fFloatOptions.value_or (Characters::FloatConversion::ToStringOptions{})}
35 , fPrettyPrint{o.fPrettyPrint.value_or (true)}
36 , fCanonicalize{o.fCanonicalize.value_or (false)}
37 , fSpacesPerIndent{o.fSpacesPerIndent.value_or (4)}
38 , fAllowNANInf{o.fAllowNANInf.value_or (true)}
39 , fLineTermination{o.fLineTermination.value_or (Characters::
kEOL<char>)}
41 if (not fPrettyPrint) {
44 if (fSpacesPerIndent != 0) {
51 unsigned int fSpacesPerIndent;
67 if (options.fSpacesPerIndent != 0) {
68 constexpr bool kSpeedTweak_ =
true;
69 if constexpr (kSpeedTweak_) {
70 for (
int i = 0; i < indentLevel; ++i) {
71 out.
Write (options.fIndentSpace);
75 out.
Write (options.fIndentSpace.Repeat (indentLevel));
92 out.
Write (
"false"sv);
98 (void)::swprintf (buf, std::size (buf), L
"%lld", v);
104 (void)::swprintf (buf, std::size (buf), L
"%llu", v);
111 if (isnan (v) or isinf (v)) {
112 Require (options.fAllowNANInf);
113 PrettyPrint_ (options, tmp, out);
119 template <
typename CHARITERATOR>
125 for (
auto i = start; i != end; ++i) {
158 (void)::swprintf (buf, std::size (buf), L
"\\u%04x",
static_cast<char16_t> (c.
GetCharacterCode ()));
173 PrettyPrint_ (options, v.begin (), v.end (), out);
178 span<const char32_t> p = v.
GetData (&ignored);
179 PrettyPrint_ (options, p.data (), p.data () + p.size (), out);
181 void PrettyPrint_ (
const OptionValues_& options,
const vector<VariantValue>& v,
const OutputStream::Ptr<Character>& out,
int indentLevel)
184 if (options.fPrettyPrint) {
185 out.
Write (options.fLineTermination);
187 for (
auto i = v.begin (); i != v.end (); ++i) {
188 Indent_ (options, out, indentLevel + 1);
189 PrettyPrint_ (options, *i, out, indentLevel + 1);
190 if (i + 1 != v.end ()) {
193 if (options.fPrettyPrint) {
194 out.
Write (options.fLineTermination);
197 Indent_ (options, out, indentLevel);
203 if (options.fPrettyPrint) {
204 out.
Write (options.fLineTermination);
207 if (options.fCanonicalize) {
210 for (
auto i = vv.begin (); i != vv.end ();) {
211 Indent_ (options, out, indentLevel + 1);
212 PrettyPrint_ (options, i->fKey, out, indentLevel + 1);
214 PrettyPrint_ (options, i->fValue, out, indentLevel + 1);
219 if (options.fPrettyPrint) {
220 out.
Write (options.fLineTermination);
223 Indent_ (options, out, indentLevel);
228 switch (v.GetType ()) {
229 case VariantValue::eNull:
230 PrettyPrint_Null_ (options, out);
232 case VariantValue::eBoolean:
233 PrettyPrint_ (options, v.
As<
bool> (), out);
235 case VariantValue::eInteger:
236 PrettyPrint_ (options, v.
As<
long long int> (), out);
238 case VariantValue::eUnsignedInteger:
239 PrettyPrint_ (options, v.
As<
unsigned long long int> (), out);
241 case VariantValue::eFloat:
242 PrettyPrint_ (options, v.
As<
long double> (), out);
244 case VariantValue::eMap:
247 case VariantValue::eArray:
248 PrettyPrint_ (options, v.
As<vector<VariantValue>> (), out, indentLevel);
251 PrettyPrint_ (options, v.
As<
String> (), out);
263 OptionValues_ fOptions_;
264 Rep_ (
const Options& options)
268 Rep_ (
const OptionValues_& options)
272 virtual _SharedPtrIRep Clone ()
const override
274 return MakeSharedPtr<Rep_> (fOptions_);
276 virtual optional<filesystem::path> GetDefaultFileSuffix ()
const override
283 PrettyPrint_ (fOptions_, v, textOut, 0);
284 if (fOptions_.fPrettyPrint) {
285 textOut.
Write (fOptions_.fLineTermination);
290 PrettyPrint_ (fOptions_, v, out, 0);
294Variant::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={})
static constexpr T kEOL[]
null-terminated String constant for current compiled platform - Windows (CRLF) or POSIX (NL) - macos ...
Ptr New(const Streams::OutputStream::Ptr< byte > &src, const Characters::CodeCvt<> &char2OutputConverter)