Arcane  v4.1.2.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
JSONWriter.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2025 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* JSONWriter.cc (C) 2000-2025 */
9/* */
10/* Ecrivain au format JSON. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/common/JSONWriter.h"
16#include "arccore/base/String.h"
17#include "arccore/base/FatalErrorException.h"
18#include <limits>
19
20// Les deux lignes suivantes permettent d'utiliser des indices sur 64 bits
21// au lieu de 32 par défaut.
22#define RAPIDJSON_NO_SIZETYPEDEFINE
23namespace rapidjson { typedef ::std::size_t SizeType; }
24
25#define RAPIDJSON_HAS_STDSTRING 1
26#include "arccore/common/internal/json/rapidjson/writer.h"
27#include "arccore/common/internal/json/rapidjson/prettywriter.h"
29#include "arccore/common/internal/json/rapidjson/stringbuffer.h"
30
31#include <sstream>
32#include <iomanip>
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37namespace Arcane
38{
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
44{
45 public:
46
47 Impl(bool use_hex_float)
48 : m_writer(m_buffer)
49 , m_use_hex_float(use_hex_float)
50 {
51 m_writer.SetIndent(' ', 1);
52 m_writer.SetFormatOptions(rapidjson::kFormatSingleLineArray);
53 m_real_ostr.precision(std::numeric_limits<Real>::max_digits10);
54 }
55
56 public:
57
58 void writeKey(StringView key)
59 {
60 Span<const Byte> bytes = key.bytes();
61 Int64 size = bytes.size();
62 if (size == 0)
63 ARCCORE_FATAL("null size");
64 // TODO: regarder s'il faut toujours mettre 'true' et donc faire une copie.
65 m_writer.Key((const char*)bytes.data(), size, true);
66 }
67 void writeStringValue(StringView value)
68 {
69 Span<const Byte> bytes = value.bytes();
70 Int64 size = bytes.size();
71 if (size == 0) {
72 m_writer.Null();
73 return;
74 }
75 // TODO: regarder s'il faut toujours mettre 'true' et donc faire une copie.
76 m_writer.String((const char*)bytes.data(), size, true);
77 }
78 void writeStringValue(const std::string& value)
79 {
80 m_writer.String(value);
81 }
82 void write(StringView key, const char* v);
83 void write(StringView key, Real v)
84 {
85 writeKey(key);
86 if (m_use_hex_float) {
87 char buf[32];
88 sprintf(buf, "%a", v);
89 m_writer.String(buf);
90 }
91 else
92 m_writer.Double(v);
93 }
94 void write(StringView key, Span<const Real> view)
95 {
96 writeKey(key);
97 {
98 m_real_ostr.str(std::string());
99 // NOTE: avec le C++11, on peut utiliser std::hexfloat comme modificateur
100 // pour écrire directement en hexadécimal flottant. Cependant ne fonctionne
101 // qu'à partir de GCC 5.0 ou visual studio 2015.
102 // ostr << std::hexfloat;
103 char buf[32];
104 for (Int64 i = 0, n = view.size(); i < n; ++i) {
105 if (i != 0)
106 m_real_ostr << ' ';
107 if (m_use_hex_float) {
108 sprintf(buf, "%a", view[i]);
109 m_real_ostr << buf;
110 }
111 else
112 m_real_ostr << view[i];
113 }
114 writeStringValue(m_real_ostr.str());
115 }
116 }
117
118 public:
119
120 rapidjson::StringBuffer m_buffer;
121 rapidjson::PrettyWriter<rapidjson::StringBuffer> m_writer;
122 bool m_use_hex_float;
123 std::ostringstream m_real_ostr;
124};
125
126/*---------------------------------------------------------------------------*/
127/*---------------------------------------------------------------------------*/
128
129JSONWriter::
130JSONWriter(FormatFlags format_flags)
131: m_p(nullptr)
132{
133 bool use_hex_float = (int)format_flags & (int)FormatFlags::HexFloat;
134 m_p = new Impl(use_hex_float);
135}
136
137JSONWriter::
138~JSONWriter()
139{
140 delete m_p;
141}
142
143void JSONWriter::
144beginObject()
145{
146 m_p->m_writer.StartObject();
147}
148void JSONWriter::
149endObject()
150{
151 m_p->m_writer.EndObject();
152}
153void JSONWriter::
154writeKey(StringView key)
155{
156 m_p->writeKey(key);
157}
158void JSONWriter::
159beginArray()
160{
161 m_p->m_writer.StartArray();
162}
163void JSONWriter::
164endArray()
165{
166 m_p->m_writer.EndArray();
167}
168
169void JSONWriter::
170writeValue(StringView str)
171{
172 m_p->writeStringValue(str);
173}
174void JSONWriter::
175write(StringView key, bool v)
176{
177 m_p->writeKey(key);
178 m_p->m_writer.Bool(v);
179}
180void JSONWriter::
181_writeInt64(StringView key, Int64 v)
182{
183 m_p->writeKey(key);
184 m_p->m_writer.Int64(v);
185}
186void JSONWriter::
187_writeUInt64(StringView key, UInt64 v)
188{
189 m_p->writeKey(key);
190 m_p->m_writer.Uint64(v);
191}
192void JSONWriter::
193write(StringView key, Real v)
194{
195 m_p->write(key, v);
196}
197void JSONWriter::
198write(StringView key, StringView str)
199{
200 m_p->writeKey(key);
201 m_p->writeStringValue(str);
202}
203void JSONWriter::
204write(StringView key, const char* v)
205{
206 m_p->writeKey(key);
207 m_p->writeStringValue(StringView(v));
208}
209void JSONWriter::
210write(StringView key, std::string_view v)
211{
212 m_p->writeKey(key);
213 m_p->writeStringValue(v);
214}
215void JSONWriter::
216writeIfNotNull(StringView key, const String& str)
217{
218 if (str.null())
219 return;
220 m_p->writeKey(key);
221 m_p->writeStringValue(str);
222}
223
224void JSONWriter::
225write(StringView key, Span<const Int32> view)
226{
227 m_p->writeKey(key);
228 m_p->m_writer.StartArray();
229 for (Int64 i = 0, n = view.size(); i < n; ++i)
230 m_p->m_writer.Int(view[i]);
231 m_p->m_writer.EndArray();
232}
233
234void JSONWriter::
235write(StringView key, Span<const Int64> view)
236{
237 m_p->writeKey(key);
238 m_p->m_writer.StartArray();
239 for (Int64 i = 0, n = view.size(); i < n; ++i)
240 m_p->m_writer.Int64(view[i]);
241 m_p->m_writer.EndArray();
242}
243
244void JSONWriter::
245write(StringView key, Span<const Real> view)
246{
247 m_p->write(key, view);
248}
249
250/*---------------------------------------------------------------------------*/
251/*---------------------------------------------------------------------------*/
252
253StringView JSONWriter::
254getBuffer() const
255{
256 const Byte* buf_chars = reinterpret_cast<const Byte*>(m_p->m_buffer.GetString());
257 Span<const Byte> bytes(buf_chars, m_p->m_buffer.GetSize());
258 return StringView(bytes);
259}
260
261/*---------------------------------------------------------------------------*/
262/*---------------------------------------------------------------------------*/
263
264} // namespace Arcane
265
266/*---------------------------------------------------------------------------*/
267/*---------------------------------------------------------------------------*/
#define ARCCORE_FATAL(...)
Macro envoyant une exception FatalErrorException.
Types et fonctions associés aux classes ArrayView et ConstArrayView.
constexpr __host__ __device__ pointer data() const noexcept
Pointeur sur le début de la vue.
Definition Span.h:537
constexpr __host__ __device__ SizeType size() const noexcept
Retourne la taille du tableau.
Definition Span.h:325
Vue d'un tableau d'éléments de type T.
Definition Span.h:633
Vue sur une chaîne de caractères UTF-8.
Definition StringView.h:47
constexpr Span< const Byte > bytes() const ARCCORE_NOEXCEPT
Retourne la conversion de l'instance dans l'encodage UTF-8.
Definition StringView.h:96
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
std::uint64_t UInt64
Type entier non signé sur 64 bits.
std::int64_t Int64
Type entier signé sur 64 bits.
double Real
Type représentant un réel.
unsigned char Byte
Type d'un octet.
Definition BaseTypes.h:43
main RapidJSON namespace
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition rapidjson.h:416