CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
json_value_of_element.h
1#pragma once
2
3#include <system/type_info.h>
4#include <system/object_ext.h>
5#include <system/nullable.h>
6#include <system/guid.h>
7#include <system/diagnostics/debug.h>
8#include <system/details/pointer_collection_helpers.h>
9#include <system/details/patterns.h>
10#include <system/default.h>
11#include <system/decimal.h>
12#include <system/date_time_offset.h>
13#include <system/date_time.h>
14#include <cstdint>
15
16#include "system/text/json/document/json_element.h"
17#include "system/text/json/nodes/json_node_options.h"
18#include "system/text/json/nodes/json_value_of_t.h"
19#include "system/text/json/throw_helper.h"
20#include "system/text/json/document/json_value_kind.h"
21
22namespace System
23{
24namespace Text
25{
26namespace Json
27{
28class JsonSerializerOptions;
29namespace Nodes
30{
31class JsonNode;
32} // namespace Nodes
33class Utf8JsonWriter;
34} // namespace Json
35} // namespace Text
36} // namespace System
37
38namespace System {
39
40namespace Text {
41
42namespace Json {
43
44namespace Nodes {
45
46class JsonValueOfElement final : public JsonValue<JsonElement>
47{
50
51 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
52 RTTI_INFO_DECL();
53
54public:
55
57
59
62 template<typename TypeToConvert>
63 TypeToConvert GetValue() /*override*/
64 {
65 TypeToConvert value;
66 if (!TryGetValue<TypeToConvert>(value))
67 {
69 }
70
71 return value;
72 }
73
74 template<typename TypeToConvert>
75 bool TryGetValue(TypeToConvert& value) /*override*/
76 {
77 bool success;
78 const System::TypeInfo& typeInfo = System::ObjectExt::GetType<TypeToConvert>();
79
80 TypeToConvert element;
81 if (System::Is<TypeToConvert>(Value, element))
82 {
83 value = element;
84 return true;
85 }
86
87 switch (Value.get_ValueKind())
88 {
90 {
91 if (typeInfo.Equals<int32_t>() || typeInfo.Equals<Nullable<int32_t>>())
92 {
93 int32_t result;
94 success = Value.TryGetInt32(result);
95 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result));
96 return success;
97 }
98 if (typeInfo.Equals<int64_t>() || typeInfo.Equals<Nullable<int64_t>>())
99 {
100 int64_t result2;
101 success = Value.TryGetInt64(result2);
102 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result2));
103 return success;
104 }
105 if (typeInfo.Equals<double>() || typeInfo.Equals<Nullable<double>>())
106 {
107 double result3;
108 success = Value.TryGetDouble(result3);
109 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result3));
110 return success;
111 }
112 if (typeInfo.Equals<int16_t>() || typeInfo.Equals<Nullable<int16_t>>())
113 {
114 int16_t result4;
115 success = Value.TryGetInt16(result4);
116 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result4));
117 return success;
118 }
119 if (typeInfo.Equals<Decimal>() || typeInfo.Equals<Nullable<Decimal>>())
120 {
121 Decimal result5;
122 success = Value.TryGetDecimal(result5);
123 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result5));
124 return success;
125 }
126 if (typeInfo.Equals<uint8_t>() || typeInfo.Equals<Nullable<uint8_t>>())
127 {
128 uint8_t result6;
129 success = Value.TryGetByte(result6);
130 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result6));
131 return success;
132 }
133 if (typeInfo.Equals<float>() || typeInfo.Equals<Nullable<float>>())
134 {
135 float result7;
136 success = Value.TryGetSingle(result7);
137 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result7));
138 return success;
139 }
140 if (typeInfo.Equals<uint32_t>() || typeInfo.Equals<Nullable<uint32_t>>())
141 {
142 uint32_t result8;
143 success = Value.TryGetUInt32(result8);
144 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result8));
145 return success;
146 }
147 if (typeInfo.Equals<uint16_t>() || typeInfo.Equals<Nullable<uint16_t>>())
148 {
149 uint16_t result9;
150 success = Value.TryGetUInt16(result9);
151 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result9));
152 return success;
153 }
154 if (typeInfo.Equals<uint64_t>() || typeInfo.Equals<Nullable<uint64_t>>())
155 {
156 uint64_t result10;
157 success = Value.TryGetUInt64(result10);
158 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result10));
159 return success;
160 }
161 if (typeInfo.Equals<int8_t>() || typeInfo.Equals<Nullable<int8_t>>())
162 {
163 int8_t result11;
164 success = Value.TryGetSByte(result11);
165 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result11));
166 return success;
167 }
168 break;
169 }
171 {
172 if (typeInfo.Equals<String>())
173 {
174 String result = Value.GetString();
175 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result));
176 return true;
177 }
178 if (typeInfo.Equals<DateTime>() || typeInfo.Equals<Nullable<DateTime>>())
179 {
180 DateTime result12;
181 success = Value.TryGetDateTime(result12);
182 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result12));
183 return success;
184 }
185 if (typeInfo.Equals<DateTimeOffset>() || typeInfo.Equals<Nullable<DateTimeOffset>>())
186 {
187 DateTimeOffset result13;
188 success = Value.TryGetDateTimeOffset(result13);
189 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result13));
190 return success;
191 }
192 if (typeInfo.Equals<Guid>() || typeInfo.Equals<Nullable<Guid>>())
193 {
194 Guid result14;
195 success = Value.TryGetGuid(result14);
196 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result14));
197 return success;
198 }
199 if (typeInfo.Equals<char16_t>() || typeInfo.Equals<Nullable<char16_t>>())
200 {
201 String result = Value.GetString();
202 if (result.get_Length() == 1)
203 {
204 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(result[0]));
205 return true;
206 }
207 }
208 break;
209 }
212 if (typeInfo.Equals<bool>() || typeInfo.Equals<Nullable<bool>>())
213 {
214 value = System::ExplicitCast<TypeToConvert>(System::ExplicitCast<System::Object>(Value.GetBoolean()));
215 return true;
216 }
217 break;
218 default:
219 break;
220 }
221
222 value = System::Default<TypeToConvert>();
223 return false;
224 }
225
227 void SetTemplateWeakPtr(uint32_t argument) override;
228
229protected:
230
232
233private:
234
235 JsonValueKind GetValueKindCore() override;
236
237};
238
239} // namespace Nodes
240} // namespace Json
241} // namespace Text
242} // namespace System
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
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.
Definition: json_element.h:59
bool TryGetSingle(float &value)
bool TryGetInt32(int32_t &value)
bool TryGetInt64(int64_t &value)
bool TryGetUInt16(uint16_t &value)
bool TryGetUInt64(uint64_t &value)
bool TryGetDouble(double &value)
bool TryGetInt16(int16_t &value)
bool TryGetSByte(int8_t &value)
bool TryGetDateTime(DateTime &value)
bool TryGetByte(uint8_t &value)
bool TryGetDateTimeOffset(DateTimeOffset &value)
bool TryGetDecimal(Decimal &value)
bool TryGetUInt32(uint32_t &value)
Definition: json_node.h:54
Definition: json_value_of_t.h:53
JsonElement Value
Definition: json_value_of_t.h:114
Definition: json_value_of_element.h:47
bool DeepEqualsCore(System::SharedPtr< JsonNode > otherNode) override
bool TryGetValue(TypeToConvert &value)
Definition: json_value_of_element.h:75
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
System::SharedPtr< JsonNode > DeepCloneCore() override
JsonValueOfElement(JsonElement value, Nullable< JsonNodeOptions > options)
Nullable< JsonElement > get_UnderlyingElement() override
void WriteTo(System::SharedPtr< Utf8JsonWriter > writer, System::SharedPtr< JsonSerializerOptions > options=nullptr) override
TypeToConvert GetValue()
Definition: json_value_of_element.h:63
static void ThrowInvalidOperationException_NodeUnableToConvertElement(JsonValueKind valueKind, const TypeInfo &destinationType)
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