4#include "Stroika/Foundation/StroikaPreComp.h"
6#include "Stroika/Foundation/Characters/FloatConversion.h"
9#include "Stroika/Foundation/Streams/TextToBinary.h"
18using namespace Stroika::Foundation::Streams;
21using Memory::MakeSharedPtr;
30 struct OptionValues_ final {
31 OptionValues_ (
const Variant::JSON::Writer::Options& o)
32 : fFloatOptions{o.fFloatOptions.value_or (Characters::FloatConversion::ToStringOptions{})}
33 , fJSONPrettyPrint{o.fJSONPrettyPrint.value_or (true)}
34 , fSpacesPerIndent{o.fSpacesPerIndent.value_or (4)}
35 , fAllowNANInf{o.fAllowNANInf.value_or (true)}
36 , fLineTermination{o.fLineTermination.value_or (Characters::
kEOL<char>)}
38 if (not fJSONPrettyPrint) {
41 if (fSpacesPerIndent != 0) {
46 bool fJSONPrettyPrint;
47 unsigned int fSpacesPerIndent;
63 if (options.fSpacesPerIndent != 0) {
64 constexpr bool kSpeedTweak_ =
true;
65 if constexpr (kSpeedTweak_) {
66 for (
int i = 0; i < indentLevel; ++i) {
67 out.
Write (options.fIndentSpace);
71 out.
Write (options.fIndentSpace.Repeat (indentLevel));
88 out.
Write (
"false"sv);
94 (void)::swprintf (buf, std::size (buf), L
"%lld", v);
100 (void)::swprintf (buf, std::size (buf), L
"%llu", v);
107 if (isnan (v) or isinf (v)) {
108 Require (options.fAllowNANInf);
109 PrettyPrint_ (options, tmp, out);
115 template <
typename CHARITERATOR>
121 for (
auto i = start; i != end; ++i) {
154 (void)::swprintf (buf, std::size (buf), L
"\\u%04x",
static_cast<char16_t> (c.
GetCharacterCode ()));
169 PrettyPrint_ (options, v.begin (), v.end (), out);
174 span<const char32_t> p = v.
GetData (&ignored);
175 PrettyPrint_ (options, p.data (), p.data () + p.size (), out);
177 void PrettyPrint_ (
const OptionValues_& options,
const vector<VariantValue>& v,
const OutputStream::Ptr<Character>& out,
int indentLevel)
180 if (options.fJSONPrettyPrint) {
181 out.
Write (options.fLineTermination);
183 for (
auto i = v.begin (); i != v.end (); ++i) {
184 Indent_ (options, out, indentLevel + 1);
185 PrettyPrint_ (options, *i, out, indentLevel + 1);
186 if (i + 1 != v.end ()) {
189 if (options.fJSONPrettyPrint) {
190 out.
Write (options.fLineTermination);
193 Indent_ (options, out, indentLevel);
199 if (options.fJSONPrettyPrint) {
200 out.
Write (options.fLineTermination);
202 for (
auto i = v.
begin (); i != v.
end ();) {
203 Indent_ (options, out, indentLevel + 1);
204 PrettyPrint_ (options, i->fKey, out, indentLevel + 1);
206 PrettyPrint_ (options, i->fValue, out, indentLevel + 1);
211 if (options.fJSONPrettyPrint) {
212 out.
Write (options.fLineTermination);
215 Indent_ (options, out, indentLevel);
220 switch (v.GetType ()) {
221 case VariantValue::eNull:
222 PrettyPrint_Null_ (options, out);
224 case VariantValue::eBoolean:
225 PrettyPrint_ (options, v.
As<
bool> (), out);
227 case VariantValue::eInteger:
228 PrettyPrint_ (options, v.
As<
long long int> (), out);
230 case VariantValue::eUnsignedInteger:
231 PrettyPrint_ (options, v.
As<
unsigned long long int> (), out);
233 case VariantValue::eFloat:
234 PrettyPrint_ (options, v.
As<
long double> (), out);
236 case VariantValue::eMap:
239 case VariantValue::eArray:
240 PrettyPrint_ (options, v.
As<vector<VariantValue>> (), out, indentLevel);
243 PrettyPrint_ (options, v.
As<
String> (), out);
255 OptionValues_ fOptions_;
256 Rep_ (
const Options& options)
260 Rep_ (
const OptionValues_& options)
264 virtual _SharedPtrIRep Clone ()
const override
266 return MakeSharedPtr<Rep_> (fOptions_);
268 virtual optional<filesystem::path> GetDefaultFileSuffix ()
const override
275 PrettyPrint_ (fOptions_, v, textOut, 0);
276 if (fOptions_.fJSONPrettyPrint) {
277 textOut.
Write (fOptions_.fLineTermination);
282 PrettyPrint_ (fOptions_, v, out, 0);
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...
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 ...
Ptr New(const Streams::OutputStream::Ptr< byte > &src, const Characters::CodeCvt<> &char2OutputConverter)