CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
json_value_of_t.h
1#pragma once
2
3#include <system/version.h>
4#include <system/uri.h>
5#include <system/type_info.h>
6#include <system/type_code.h>
7#include <system/timespan.h>
8#include <system/scope_guard.h>
9#include <system/object_ext.h>
10#include <system/nullable.h>
11#include <system/guid.h>
12#include <system/diagnostics/debug.h>
13#include <system/details/pointer_collection_helpers.h>
14#include <system/details/patterns.h>
15#include <system/default.h>
16#include <system/date_time_offset.h>
17#include <system/date_time.h>
18#include <cstdint>
19
20#include "system/text/json/document/json_value_kind.h"
21#include "system/text/json/throw_helper.h"
22#include "system/text/json/nodes/json_node_options.h"
23#include "system/text/json/nodes/json_value.h"
24#include "system/text/json/document/json_element.h"
25
26namespace System
27{
28namespace Text
29{
30namespace Json
31{
32class JsonElement;
33namespace Nodes
34{
35template <typename> class JsonValueCustomized;
36class JsonValueOfElement;
37template <typename> class JsonValuePrimitive;
38} // namespace Nodes
39} // namespace Json
40} // namespace Text
41} // namespace System
42
43namespace System {
44
45namespace Text {
46
47namespace Json {
48
49namespace Nodes {
50
51template<typename TValue>
53{
55 typedef JsonValueBase BaseType;
56
57 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
58 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
59
60 friend class JsonValueOfElement;
61 template<typename FT0> friend class JsonValue;
62 template<typename FT0> friend class JsonValueCustomized;
63 template<typename FT0> friend class JsonValuePrimitive;
64
65private:
66
67 class DebugView final : public System::Object
68 {
69 typedef DebugView ThisType;
71
72 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
73 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
74
75 public:
76
78
79 String get_Json()
80 {
81 return _node->ToJsonString();
82 }
83
84 String get_Path()
85 {
86 return System::ExplicitCast<JsonNode>(_node)->GetPath();
87 }
88
89 TValue get_Value()
90 {
91 return _node->Value;
92 }
93
94 DebugView(System::SharedPtr<JsonValue<TValue>> node)
95 {
96 _node = node;
97 }
98
99 protected:
100
101 #ifdef ASPOSE_GET_SHARED_MEMBERS
102 void GetSharedMembers(System::Object::shared_members_type& result) const override
103 {
104 Object::GetSharedMembers(result);
105
106 result.Add("System::Text::Json::Nodes::JsonValueBase::DebugView::_node", this->_node);
107 }
108 #endif
109
110 };
111
112public:
113
114 TValue Value;
115
117 {
118 return s_valueKind.get_HasValue();
119 }
120
121 template<typename T>
122 T GetValue() /*override*/
123 {
124 // If no conversion is needed, just return the raw value.
125 T returnValue;
126 if (System::Is<T>(Value, returnValue))
127 {
128 return returnValue;
129 }
130
131 // Currently we do not support other conversions.
132 // Generics (and also boxing) do not support standard cast operators say from 'long' to 'int',
133 // so attempting to cast here would throw InvalidCastException.
134 System::Text::Json::ThrowHelper::ThrowInvalidOperationException_NodeUnableToConvert(System::ObjectExt::GetType<TValue>(), System::ObjectExt::GetType<T>());
135 return System::Default<T>();
136 }
137
138 template<typename T>
139 bool TryGetValue(T& value) /*override*/
140 {
141 // If no conversion is needed, just return the raw value.
142 T returnValue;
143 if (System::Is<T>(Value, returnValue))
144 {
145 value = returnValue;
146 return true;
147 }
148
149 // Currently we do not support other conversions.
150 // Generics (and also boxing) do not support standard cast operators say from 'long' to 'int',
151 // so attempting to cast here would throw InvalidCastException.
152 value = System::Default<T>();
153 return false;
154 }
155
156 void SetTemplateWeakPtr(uint32_t argument) override
157 {
158 switch (argument)
159 {
160 case 0:
161 System::Details::CollectionHelpers::SetWeakPointer(Value);
162 break;
163
164 }
165 }
166
167protected:
168
169 JsonValue(TValue value, Nullable<JsonNodeOptions> options) : JsonValueBase(options), Value(TValue())
170 {
171 System::Details::ThisProtector __local_self_ref(this);
172
173 Value = value;
174 }
175
176 virtual ~JsonValue()
177 {
178 }
179
180 #ifdef ASPOSE_GET_SHARED_MEMBERS
181 void GetSharedMembers(System::Object::shared_members_type& result) const override
182 {
183 JsonValueBase::GetSharedMembers(result);
184
185 result.Add("System::Text::Json::Nodes::JsonValueBase::Value", this->Value);
186 }
187 #endif
188
189private:
190
191 static Nullable<JsonValueKind> s_valueKind;
192
193 static JsonValueKind DetermineValueKind(TValue value)
194 {
195 CODEPORTING_DEBUG_ASSERT2(!(s_valueKind == nullptr), u"Should only be invoked for types that are supported primitives.");
196
197 return DetermineValueKindImpl(value, std::is_same<TValue, bool>());
198 }
199
200 static JsonValueKind DetermineValueKindImpl(TValue value, std::true_type)
201 {
202 // Boolean requires special handling since kind varies by value.
204 }
205
206 static JsonValueKind DetermineValueKindImpl(TValue, std::false_type)
207 {
208 return s_valueKind.get_Value();
209 }
210
211 static Nullable<JsonValueKind> DetermineValueKindForType(const TypeInfo& type)
212 {
213 if (type.get_IsEnum())
214 {
215 return nullptr;
216 // Can vary depending on converter configuration and value.
217 }
218
219 TypeInfo underlyingType;
220 if (System::Is<TypeInfo>(System::NullableUtils::GetUnderlyingType(type), underlyingType))
221 {
222 // Because JsonNode excludes null values, we can identify with the value kind of the underlying type.
223 return DetermineValueKindForType(underlyingType);
224 }
225
226 if (type.Equals<DateTime>() || type.Equals<DateTimeOffset>() || type.Equals<TimeSpan>() || type.Equals<Guid>() || type.Equals<Uri>() || type.Equals<Version>())
227 {
229 }
230
231 if (type.Equals<bool>())
232 {
234 }
235
236 if (type.Equals<int8_t>() || type.Equals<uint8_t>() ||
237 type.Equals<int16_t>() || type.Equals<uint16_t>() ||
238 type.Equals<int32_t>() || type.Equals<uint32_t>() ||
239 type.Equals<int64_t>() || type.Equals<uint64_t>() ||
240 type.Equals<float>() || type.Equals<double>() ||
241 type.Equals<Decimal>())
242 {
244 }
245
246 if (type.Equals<String>() || type.Equals<char16_t>())
247 {
249 }
250
251 return nullptr;
252 }
253
254 static struct __StaticConstructor__ { __StaticConstructor__(); } s_constructor__;
255
256};
257
258template<typename TValue>
259Nullable<JsonValueKind> JsonValue<TValue>::s_valueKind;
260
261} // namespace Nodes
262} // namespace Json
263} // namespace Text
264} // namespace System
Forward declaration.
Definition: nullable.h:75
T get_Value() const
Returns a copy of the value represented by the current object.
Definition: nullable.h:144
bool get_HasValue() const
Determines whether the current object represents any value.
Definition: nullable.h:162
static const System::TypeInfo & GetUnderlyingType(const System::TypeInfo &nullableType)
Returns the underlying type argument of the specified nullable type.
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
Definition: json_node.h:54
Definition: json_value.h:60
Definition: json_value_of_t_customized.h:49
Definition: json_value_of_t.h:53
virtual ~JsonValue()
Definition: json_value_of_t.h:176
static bool get_TypeIsSupportedPrimitive()
Definition: json_value_of_t.h:116
T GetValue()
Definition: json_value_of_t.h:122
TValue Value
Definition: json_value_of_t.h:114
bool TryGetValue(T &value)
Definition: json_value_of_t.h:139
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: json_value_of_t.h:156
JsonValue(TValue value, Nullable< JsonNodeOptions > options)
Definition: json_value_of_t.h:169
Definition: json_value_of_element.h:47
Definition: json_value_of_t_primitive.h:47
static void ThrowInvalidOperationException_NodeUnableToConvert(const TypeInfo &sourceType, const TypeInfo &destinationType)
@ Text
Defines color adjustment information for text.
@ TypeInfo
Specifies that the member is a type.
JsonValueKind
Definition: json_value_kind.h:12
Definition: db_command.h:9