14#include "arccore/common/JSONWriter.h"
16#include "arccore/base/String.h"
17#include "arccore/base/FatalErrorException.h"
22#define RAPIDJSON_NO_SIZETYPEDEFINE
28#define RAPIDJSON_HAS_STDSTRING 1
29#include "arccore/common/internal/json/rapidjson/writer.h"
30#include "arccore/common/internal/json/rapidjson/prettywriter.h"
32#include "arccore/common/internal/json/rapidjson/stringbuffer.h"
50 Impl(
bool use_hex_float)
52 , m_use_hex_float(use_hex_float)
54 m_writer.SetIndent(
' ', 1);
55 m_writer.SetFormatOptions(rapidjson::kFormatSingleLineArray);
56 m_real_ostr.precision(std::numeric_limits<Real>::max_digits10);
64 Int64 size = bytes.size();
68 m_writer.Key((
const char*)bytes.data(), size,
true);
73 Int64 size = bytes.size();
79 m_writer.String((
const char*)bytes.data(), size,
true);
81 void writeStringValue(
const std::string& value)
83 m_writer.String(value);
89 if (m_use_hex_float) {
91 sprintf(buf,
"%a", v);
101 m_real_ostr.str(std::string());
107 for (
Int64 i = 0, n = view.
size(); i < n; ++i) {
110 if (m_use_hex_float) {
111 sprintf(buf,
"%a", view[i]);
115 m_real_ostr << view[i];
117 writeStringValue(m_real_ostr.str());
123 rapidjson::StringBuffer m_buffer;
124 rapidjson::PrettyWriter<rapidjson::StringBuffer> m_writer;
125 bool m_use_hex_float;
126 std::ostringstream m_real_ostr;
133JSONWriter(FormatFlags format_flags)
136 bool use_hex_float = (int)format_flags & (int)FormatFlags::HexFloat;
137 m_p =
new Impl(use_hex_float);
149 m_p->m_writer.StartObject();
154 m_p->m_writer.EndObject();
157writeKey(StringView key)
164 m_p->m_writer.StartArray();
169 m_p->m_writer.EndArray();
173writeValue(StringView str)
175 m_p->writeStringValue(str);
178write(StringView key,
bool v)
181 m_p->m_writer.Bool(v);
184_writeInt64(StringView key,
Int64 v)
187 m_p->m_writer.Int64(v);
190_writeUInt64(StringView key,
UInt64 v)
193 m_p->m_writer.Uint64(v);
196write(StringView key,
Real v)
201write(StringView key, StringView str)
204 m_p->writeStringValue(str);
207write(StringView key,
const char* v)
210 m_p->writeStringValue(StringView(v));
213write(StringView key, std::string_view v)
216 m_p->writeStringValue(v);
219writeIfNotNull(StringView key,
const String& str)
224 m_p->writeStringValue(str);
228write(StringView key, Span<const Int32> view)
231 m_p->m_writer.StartArray();
232 for (
Int64 i = 0, n = view.size(); i < n; ++i)
233 m_p->m_writer.Int(view[i]);
234 m_p->m_writer.EndArray();
238write(StringView key, Span<const Int64> view)
241 m_p->m_writer.StartArray();
242 for (
Int64 i = 0, n = view.size(); i < n; ++i)
243 m_p->m_writer.Int64(view[i]);
244 m_p->m_writer.EndArray();
248write(StringView key, Span<const Real> view)
250 m_p->write(key, view);
256StringView JSONWriter::
259 const Byte* buf_chars =
reinterpret_cast<const Byte*
>(m_p->m_buffer.GetString());
260 Span<const Byte> bytes(buf_chars, m_p->m_buffer.GetSize());
261 return StringView(bytes);
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
Types and functions associated with the classes ArrayView and ConstArrayView.
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
View of an array of elements of type T.
View of a UTF-8 character string.
constexpr Span< const Byte > bytes() const ARCCORE_NOEXCEPT
Returns the conversion of the instance in UTF-8 encoding.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::uint64_t UInt64
Unsigned integer type of 64 bits.
std::int64_t Int64
Signed integer type of 64 bits.
double Real
Type representing a real number.
unsigned char Byte
Type of a byte.
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.).