CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
json_value_of_json_primitive.h
1#pragma once
2
3#include <system/type_info.h>
4#include <system/span.h>
5#include <system/object_ext.h>
6#include <system/nullable.h>
7#include <system/guid.h>
8#include <system/diagnostics/debug.h>
9#include <system/default.h>
10#include <system/decimal.h>
11#include <system/date_time_offset.h>
12#include <system/date_time.h>
13#include <system/array.h>
14#include <system/buffers/text/utf8_parser.h>
15#include <functional>
16#include <cstdint>
17
18#include "system/text/json/nodes/json_node_options.h"
19#include "system/text/json/nodes/json_value.h"
20#include "system/text/json/throw_helper.h"
21#include "system/text/json/document/json_value_kind.h"
22#include "system/text/json/document/json_element.h"
23#include "system/text/json/writer/json_writer_helper.h"
24#include "system/text/json/reader/json_reader_helper.h"
25#include "system/text/json/json_constants.h"
26
27namespace System
28{
29namespace Text
30{
31namespace Json
32{
33class JsonSerializerOptions;
34namespace Nodes
35{
36class JsonNode;
37} // namespace Nodes
38class Utf8JsonReader;
39class Utf8JsonWriter;
40} // namespace Json
41} // namespace Text
42} // namespace System
43
44namespace System {
45
46namespace Text {
47
48namespace Json {
49
50namespace Nodes {
51
53{
55
56public:
57
59
60public:
62};
63
65{
67 typedef JsonValueBase BaseType;
68
69 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
70 RTTI_INFO_DECL();
71
72public:
73
75
78 template<typename T>
79 T GetValue() /*override*/
80 {
81 T value;
82 if (!TryGetValue<T>(value))
83 {
85 }
86
87 return value;
88 }
89
90 template<typename T>
91 bool TryGetValue(T& value) /*override*/
92 {
93 const System::TypeInfo& typeInfo = System::ObjectExt::GetType<T>();
94 if (typeInfo.Equals<JsonElement>() || typeInfo.Equals<Nullable<JsonElement>>() || typeInfo.Equals<Object>())
95 {
96 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(System::Text::Json::JsonWriterHelper::WriteString<JsonElement>(_value.get_Span(), static_cast<JsonWriterHelper::WriteCallback<JsonElement>>([](ReadOnlySpan<uint8_t> serialized) -> System::Text::Json::JsonElement
97 {
98 return System::Text::Json::JsonElement::Parse(serialized);
99 }))));
100 return true;
101 }
102
103 if (typeInfo.Equals<String>())
104 {
106
107 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result));
108 return true;
109 }
110
111 bool success;
112
113 if (typeInfo.Equals<DateTime>() || typeInfo.Equals<Nullable<DateTime>>())
114 {
115 DateTime result;
116 success = System::Text::Json::JsonReaderHelper::TryGetValue(_value.get_Span(), false, result);
117 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result));
118 return success;
119 }
120
121 if (typeInfo.Equals<DateTimeOffset>() || typeInfo.Equals<Nullable<DateTimeOffset>>())
122 {
123 DateTimeOffset result2;
124 success = System::Text::Json::JsonReaderHelper::TryGetValue(_value.get_Span(), false, result2);
125 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result2));
126 return success;
127 }
128
129 if (typeInfo.Equals<Guid>() || typeInfo.Equals<Nullable<Guid>>())
130 {
131 Guid result3;
132 success = System::Text::Json::JsonReaderHelper::TryGetValue(_value.get_Span(), false, result3);
133 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result3));
134 return success;
135 }
136
137 if (typeInfo.Equals<char16_t>() || typeInfo.Equals<Nullable<char16_t>>())
138 {
140
141 if (result.get_Length() == 1)
142 {
143 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result[0]));
144 return true;
145 }
146 }
147
148 value = System::Default<T>();
149 return false;
150 }
151
152protected:
153
154 #ifdef ASPOSE_GET_SHARED_MEMBERS
155 void GetSharedMembers(System::Object::shared_members_type& result) const override;
156 #endif
157
158private:
159
161
162 JsonValueKind GetValueKindCore() override;
163
164};
165
167{
169 typedef JsonValueBase BaseType;
170
171 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
172 RTTI_INFO_DECL();
173
174public:
175
177
180 template<typename T>
181 T GetValue() /*override*/
182 {
183 T value;
184 if (!TryGetValue<T>(value))
185 {
187 }
188
189 return value;
190 }
191
192 template<typename T>
193 bool TryGetValue(T& value) /*override*/
194 {
195 const System::TypeInfo& typeInfo = System::ObjectExt::GetType<T>();
196 if (typeInfo.Equals<JsonElement>() || typeInfo.Equals<Nullable<JsonElement>>() || typeInfo.Equals<Object>())
197 {
199 return true;
200 }
201
202 if (typeInfo.Equals<bool>() || typeInfo.Equals<Nullable<bool>>())
203 {
204 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(_value));
205 return true;
206 }
207
208 value = System::Default<T>();
209 return false;
210 }
211
212private:
213
214 bool _value;
215
216 JsonValueKind get_ValueKind();
217
218 JsonValueKind GetValueKindCore() override;
219
220};
221
223{
225 typedef JsonValueBase BaseType;
226
227 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
228 RTTI_INFO_DECL();
229
230public:
231
233
235 template<typename T>
236 T GetValue() /*override*/
237 {
238 T value;
239 if (!TryGetValue<T>(value))
240 {
242 }
243
244 return value;
245 }
246
247 template<typename T>
248 bool TryGetValue(T& value) /*override*/
249 {
250 const System::TypeInfo& typeInfo = System::ObjectExt::GetType<T>();
251 if (typeInfo.Equals<JsonElement>() || typeInfo.Equals<Nullable<JsonElement>>() || typeInfo.Equals<Object>())
252 {
253 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(System::Text::Json::JsonElement::Parse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value))));
254 return true;
255 }
256
257 bool success;
258
259 if (typeInfo.Equals<int32_t>() || typeInfo.Equals<Nullable<int32_t>>())
260 {
261 int32_t result, consumed;
262 success = System::Buffers::Text::Utf8Parser::TryParse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value), result, consumed) && consumed == _value->get_Length();
263
264 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result));
265 return success;
266 }
267
268 if (typeInfo.Equals<int64_t>() || typeInfo.Equals<Nullable<int64_t>>())
269 {
270 int64_t result2; int32_t consumed2;
271 success = System::Buffers::Text::Utf8Parser::TryParse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value), result2, consumed2) && consumed2 == _value->get_Length();
272
273 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result2));
274 return success;
275 }
276
277 if (typeInfo.Equals<double>() || typeInfo.Equals<Nullable<double>>())
278 {
279 double result3; int32_t consumed3;
280 success = System::Buffers::Text::Utf8Parser::TryParse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value), result3, consumed3) && consumed3 == _value->get_Length();
281
282 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result3));
283 return success;
284 }
285
286 if (typeInfo.Equals<int16_t>() || typeInfo.Equals<Nullable<int16_t>>())
287 {
288 int16_t result4; int32_t consumed4;
289 success = System::Buffers::Text::Utf8Parser::TryParse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value), result4, consumed4) && consumed4 == _value->get_Length();
290
291 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result4));
292 return success;
293 }
294
295 if (typeInfo.Equals<Decimal>() || typeInfo.Equals<Nullable<Decimal>>())
296 {
297 Decimal result5; int32_t consumed5;
298 success = System::Buffers::Text::Utf8Parser::TryParse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value), result5, consumed5) && consumed5 == _value->get_Length();
299
300 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result5));
301 return success;
302 }
303
304 if (typeInfo.Equals<uint8_t>() || typeInfo.Equals<Nullable<uint8_t>>())
305 {
306 uint8_t result6; int32_t consumed6;
307 success = System::Buffers::Text::Utf8Parser::TryParse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value), result6, consumed6) && consumed6 == _value->get_Length();
308
309 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result6));
310 return success;
311 }
312
313 if (typeInfo.Equals<float>() || typeInfo.Equals<Nullable<float>>())
314 {
315 float result7; int32_t consumed7;
316 success = System::Buffers::Text::Utf8Parser::TryParse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value), result7, consumed7) && consumed7 == _value->get_Length();
317
318 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result7));
319 return success;
320 }
321
322 if (typeInfo.Equals<uint32_t>() || typeInfo.Equals<Nullable<uint32_t>>())
323 {
324 uint32_t result8; int32_t consumed8;
325 success = System::Buffers::Text::Utf8Parser::TryParse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value), result8, consumed8) && consumed8 == _value->get_Length();
326
327 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result8));
328 return success;
329 }
330
331 if (typeInfo.Equals<uint16_t>() || typeInfo.Equals<Nullable<uint16_t>>())
332 {
333 uint16_t result9; int32_t consumed9;
334 success = System::Buffers::Text::Utf8Parser::TryParse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value), result9, consumed9) && consumed9 == _value->get_Length();
335
336 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result9));
337 return success;
338 }
339
340 if (typeInfo.Equals<uint64_t>() || typeInfo.Equals<Nullable<uint64_t>>())
341 {
342 uint64_t result10; int32_t consumed10;
343 success = System::Buffers::Text::Utf8Parser::TryParse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value), result10, consumed10) && consumed10 == _value->get_Length();
344
345 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result10));
346 return success;
347 }
348
349 if (typeInfo.Equals<int8_t>() || typeInfo.Equals<Nullable<int8_t>>())
350 {
351 int8_t result11; int32_t consumed11;
352 success = System::Buffers::Text::Utf8Parser::TryParse(ReadOnlySpan<uint8_t>::to_ReadOnlySpan(_value), result11, consumed11) && consumed11 == _value->get_Length();
353
354 value = System::ExplicitCast<T>(System::ExplicitCast<System::Object>(result11));
355 return success;
356 }
357
358 value = System::Default<T>();
359 return false;
360 }
361
363
364protected:
365
366 #ifdef ASPOSE_GET_SHARED_MEMBERS
367 void GetSharedMembers(System::Object::shared_members_type& result) const override;
368 #endif
369
370private:
371
373
374 JsonValueKind GetValueKindCore() override;
375
376};
377
378} // namespace Nodes
379} // namespace Json
380} // namespace Text
381} // namespace System
Class that represents an array data structure. Objects of this class should only be allocated using S...
Definition: array.h:296
static bool TryParse(const ReadOnlySpan< uint8_t > &source, bool &value, int32_t &bytesConsumed)
Represents a specific date and time value on the time continuum. This type should be allocated on sta...
Definition: date_time.h:50
Contains the date and time of day relative to Coordinated Universal Time. Objects of this class shoul...
Definition: date_time_offset.h:16
Represents a decimal number. This type should be allocated on stack and passed to functions by value ...
Definition: decimal.h:107
Represents a Globally Unique IDentifier This type should be allocated on stack and passed to function...
Definition: guid.h:34
Forward declaration.
Definition: nullable.h:75
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:51
ReadOnlySpan< T > get_Span() const
Gets a read-only span that represents the current memory.
Definition: memory.h:243
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
String class used across the library. Is a substitute for C# System.String when translating code....
Definition: string.h:126
int get_Length() const
Gets string length.
static ReadOnlySpan< uint8_t > get_FalseValue()
static ReadOnlySpan< uint8_t > get_TrueValue()
Definition: json_element.h:59
static JsonElement Parse(ReadOnlySpan< uint8_t > utf8Json, JsonDocumentOptions options=System::Default< System::Text::Json::JsonDocumentOptions >())
static bool TryGetValue(ReadOnlySpan< uint8_t > segment, bool isEscaped, DateTime &value)
static String TranscodeHelper(ReadOnlySpan< uint8_t > utf8Unescaped)
T(*)(System::ReadOnlySpan< uint8_t >) WriteCallback
Definition: json_writer_helper.h:48
Definition: json_node.h:54
Definition: json_node_options.h:14
Definition: json_value.h:60
Definition: json_value_of_json_primitive.h:167
void WriteTo(System::SharedPtr< Utf8JsonWriter > writer, System::SharedPtr< JsonSerializerOptions > options=nullptr) override
System::SharedPtr< JsonNode > DeepCloneCore() override
T GetValue()
Definition: json_value_of_json_primitive.h:181
JsonValueOfJsonBool(bool value, Nullable< JsonNodeOptions > options)
bool TryGetValue(T &value)
Definition: json_value_of_json_primitive.h:193
Definition: json_value_of_json_primitive.h:223
JsonValueOfJsonNumber(System::SharedPtr< Array< uint8_t > > number, Nullable< JsonNodeOptions > options)
bool TryGetValue(T &value)
Definition: json_value_of_json_primitive.h:248
T GetValue()
Definition: json_value_of_json_primitive.h:236
void WriteTo(System::SharedPtr< Utf8JsonWriter > writer, System::SharedPtr< JsonSerializerOptions > options=nullptr) override
System::SharedPtr< JsonNode > DeepCloneCore() override
Definition: json_value_of_json_primitive.h:53
static System::SharedPtr< JsonValueBase > CreatePrimitiveValue(Utf8JsonReader &reader, JsonNodeOptions options)
Definition: json_value_of_json_primitive.h:65
bool TryGetValue(T &value)
Definition: json_value_of_json_primitive.h:91
JsonValueOfJsonString(ReadOnlyMemory< uint8_t > utf8String, Nullable< JsonNodeOptions > options)
void WriteTo(System::SharedPtr< Utf8JsonWriter > writer, System::SharedPtr< JsonSerializerOptions > options=nullptr) override
T GetValue()
Definition: json_value_of_json_primitive.h:79
System::SharedPtr< JsonNode > DeepCloneCore() override
static void ThrowInvalidOperationException_NodeUnableToConvertElement(JsonValueKind valueKind, const TypeInfo &destinationType)
Definition: utf8_json_reader.h:78
Represents a particular type and provides information about it.
Definition: type_info.h:109
bool Equals(const TypeInfo &other) const
Definition: type_info.h:244
@ Text
Defines color adjustment information for text.
JsonValueKind
Definition: json_value_kind.h:12
Definition: db_command.h:9