CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
casting_converter.h
1#pragma once
2
3#include <system/type_info.h>
4#include <system/scope_guard.h>
5#include <system/object_ext.h>
6#include <system/exceptions.h>
7#include <system/details/pointer_collection_helpers.h>
8#include <cstdint>
9
10#include "system/text/json/serialization/json_converter.h"
11#include "system/text/json/serialization/json_converter_of_t.h"
12#include "system/text/json/serialization/converter_strategy.h"
13#include "system/text/json/reader/utf8_json_reader.h"
14#include "system/text/json/serialization/json_serializer_options.h"
15#include "system/text/json/serialization/json_serializer_helpers.h"
16#include "system/text/json/writer/utf8_json_writer.h"
17#include "system/text/json/serialization/read_stack.h"
18#include "system/text/json/serialization/write_stack.h"
19#include "system/text/json/serialization/json_number_handling.h"
20#include "system/text/json/serialization/json_value_type.h"
21
22namespace System
23{
24namespace Text
25{
26namespace Json
27{
28namespace Serialization
29{
30template <typename> class JsonConverter;
31} // namespace Serialization
32} // namespace Json
33} // namespace Text
34} // namespace System
35
36namespace System {
37
38namespace Text {
39
40namespace Json {
41
42namespace Serialization {
43
44namespace Converters {
45
46template<typename T>
47class CastingConverter final : public JsonConverter<T>
48{
51
52 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
53 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
54
55 template<typename FT0> friend class CastingConverter;
56
57public:
58
60 {
61 return _sourceConverter->get_KeyType();
62 }
63
65 {
66 return _sourceConverter->get_ElementType();
67 }
68
70 {
71 return _sourceConverter->get_NullableElementConverter();
72 }
73
74 bool get_HandleNull() override
75 {
76 return pr_HandleNull;
77 }
78
80 {
81 return _sourceConverter->get_SupportsCreateObjectDelegate();
82 }
83
85 {
86 return _sourceConverter;
87 }
88
89 CastingConverter(System::SharedPtr<JsonConverterBase> sourceConverter) : pr_HandleNull(false)
90 {
91 System::Details::ThisProtector __local_self_ref(this);
92
93 _sourceConverter = sourceConverter;
94 this->init_IsInternalConverter(sourceConverter->get_IsInternalConverter());
95 this->init_IsInternalConverterForNumberType(sourceConverter->get_IsInternalConverterForNumberType());
96 this->init_ConverterStrategy(sourceConverter->get_ConverterStrategy());
97 this->init_CanBePolymorphic(sourceConverter->get_CanBePolymorphic());
98
99 // Ensure HandleNull values reflect the exact configuration of the source converter
100 this->init_HandleNullOnRead(sourceConverter->get_HandleNullOnRead());
101 this->init_HandleNullOnWrite(sourceConverter->get_HandleNullOnWrite());
102 pr_HandleNull = sourceConverter->get_HandleNullOnWrite();
103 }
104
105 T Read(Utf8JsonReader& reader, const System::TypeInfo& typeToConvert, System::SharedPtr<JsonSerializerOptions> options) override
106 {
108 {
109 // A Nullable<T> converter (e.g. the one created for DateTime? or TimeSpan?) must consume
110 // null JSON itself; the underlying T converter would otherwise reject the null token.
111 return System::Default<T>();
112 }
113
114 return System::Text::Json::Serialization::Details::UnboxOnRead<T>(_sourceConverter->ReadAsObject(System::Ref(reader), typeToConvert, options));
115 }
116
118 {
119 _sourceConverter->WriteAsObject(writer, System::ExplicitCast<System::Object>(value), options);
120 }
121
122 bool OnTryRead(Utf8JsonReader& reader, const System::TypeInfo& typeToConvert, System::SharedPtr<JsonSerializerOptions> options, ReadStack& state, T& value) override
123 {
125 {
126 value = System::Default<T>();
127 return true;
128 }
129
130 System::SharedPtr<Object> sourceValue;
131 bool result = _sourceConverter->OnTryReadAsObject(System::Ref(reader), typeToConvert, options, System::Ref(state), sourceValue);
132 value = System::Text::Json::Serialization::Details::UnboxOnRead<T>(sourceValue);
133 return result;
134 }
135
137 {
138 return _sourceConverter->OnTryWriteAsObject(writer, System::ExplicitCast<System::Object>(value), options, System::Ref(state));
139 }
140
142 {
143 if (_sourceConverter->get_Type() == System::ObjectExt::GetType<T>())
144 {
145 return System::Text::Json::Serialization::Details::UnboxOnRead<T>(_sourceConverter->ReadAsPropertyNameAsObject(System::Ref(reader), typeToConvert, options));
146 }
147 else
148 {
149 // The source converter's Type doesn't match T. Using the source converter's
150 // ReadAsPropertyName could fail or behave incorrectly: the source's fallback
151 // would look up a converter for its type (e.g. object), not T. Use our base
152 // class logic instead, which correctly uses the fallback for type T.
154 }
155 }
156
158 {
159 if (_sourceConverter->get_Type() == System::ObjectExt::GetType<T>())
160 {
161 return System::Text::Json::Serialization::Details::UnboxOnRead<T>(_sourceConverter->ReadAsPropertyNameCoreAsObject(System::Ref(reader), typeToConvert, options));
162 }
163 else
164 {
165 // The source converter's Type doesn't match T. Using the source converter's
166 // ReadAsPropertyNameCore could fail or behave incorrectly: the source's fallback
167 // would look up a converter for its type (e.g. object), not T. Use our base
168 // class logic instead, which correctly uses the fallback for type T.
170 }
171 }
172
174 {
175 if (_sourceConverter->get_Type() == System::ObjectExt::GetType<T>())
176 {
177 _sourceConverter->WriteAsPropertyNameAsObject(writer, System::ExplicitCast<System::Object>(value), options);
178 }
179 else
180 {
181 // The source converter's Type doesn't match T. Using the source converter's
182 // WriteAsPropertyName could cause infinite recursion: the source's fallback
183 // would look up a converter for its type (e.g. object), which might delegate
184 // back to us via the runtime type lookup. Use our base class logic instead,
185 // which correctly uses the fallback for type T.
187 }
188 }
189
190 void WriteAsPropertyNameCore(System::SharedPtr<Utf8JsonWriter> writer, T value, System::SharedPtr<JsonSerializerOptions> options, bool isWritingExtensionDataProperty) override
191 {
192 if (_sourceConverter->get_Type() == System::ObjectExt::GetType<T>())
193 {
194 _sourceConverter->WriteAsPropertyNameCoreAsObject(writer, System::ExplicitCast<System::Object>(value), options, isWritingExtensionDataProperty);
195 }
196 else
197 {
198 // The source converter's Type doesn't match T. Using the source converter's
199 // WriteAsPropertyNameCore could cause infinite recursion: the source's fallback
200 // would look up a converter for its type (e.g. object), which might delegate
201 // back to us via the runtime type lookup. Use our base class logic instead,
202 // which correctly uses the fallback for type T.
203 System::Text::Json::Serialization::JsonConverter<T>::WriteAsPropertyNameCore(writer, value, options, isWritingExtensionDataProperty);
204 }
205 }
206
208 {
210 {
211 return System::Default<T>();
212 }
213
214 return System::Text::Json::Serialization::Details::UnboxOnRead<T>(_sourceConverter->ReadNumberWithCustomHandlingAsObject(System::Ref(reader), handling, options));
215 }
216
218 {
219 _sourceConverter->WriteNumberWithCustomHandlingAsObject(writer, System::ExplicitCast<System::Object>(value), handling);
220 }
221
223 {
224 return _sourceConverter->GetSupportedJsonValueTypes(numberHandling);
225 }
226
227 void SetTemplateWeakPtr(uint32_t argument) override
228 {
229 switch (argument)
230 {
231 case 0:
233 break;
234
235 }
236 }
237
238protected:
239
240 #ifdef ASPOSE_GET_SHARED_MEMBERS
241 void GetSharedMembers(System::Object::shared_members_type& result) const override
242 {
244
245 result.Add("System::Text::Json::Serialization::Converters::CastingConverter::_sourceConverter", this->_sourceConverter);
246 }
247 #endif
248
249private:
250
252 bool pr_HandleNull;
253
254};
255
256} // namespace Converters
257} // namespace Serialization
258} // namespace Json
259} // namespace Text
260} // namespace System
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Definition: read_stack.h:35
T ReadAsPropertyNameCore(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options) override
Definition: casting_converter.h:157
void WriteAsPropertyName(System::SharedPtr< Utf8JsonWriter > writer, T value, System::SharedPtr< JsonSerializerOptions > options) override
Definition: casting_converter.h:173
CastingConverter(System::SharedPtr< JsonConverterBase > sourceConverter)
Definition: casting_converter.h:89
void WriteAsPropertyNameCore(System::SharedPtr< Utf8JsonWriter > writer, T value, System::SharedPtr< JsonSerializerOptions > options, bool isWritingExtensionDataProperty) override
Definition: casting_converter.h:190
bool OnTryRead(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options, ReadStack &state, T &value) override
Definition: casting_converter.h:122
System::TypeInfo get_KeyType() override
Definition: casting_converter.h:59
bool get_SupportsCreateObjectDelegate() override
Definition: casting_converter.h:79
T ReadAsPropertyName(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options) override
Definition: casting_converter.h:141
bool get_HandleNull() override
Definition: casting_converter.h:74
System::SharedPtr< JsonConverterBase > get_NullableElementConverter() override
Definition: casting_converter.h:69
JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling numberHandling) override
Definition: casting_converter.h:222
System::SharedPtr< JsonConverterBase > get_SourceConverterForCastingConverter() override
Definition: casting_converter.h:84
void WriteNumberWithCustomHandling(System::SharedPtr< Utf8JsonWriter > writer, T value, JsonNumberHandling handling) override
Definition: casting_converter.h:217
void Write(System::SharedPtr< Utf8JsonWriter > writer, T value, System::SharedPtr< JsonSerializerOptions > options) override
Definition: casting_converter.h:117
T Read(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options) override
Definition: casting_converter.h:105
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: casting_converter.h:227
T ReadNumberWithCustomHandling(Utf8JsonReader &reader, JsonNumberHandling handling, System::SharedPtr< JsonSerializerOptions > options) override
Definition: casting_converter.h:207
System::TypeInfo get_ElementType() override
Definition: casting_converter.h:64
bool OnTryWrite(System::SharedPtr< Utf8JsonWriter > writer, T value, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state) override
Definition: casting_converter.h:136
void init_ConverterStrategy(System::Text::Json::ConverterStrategy value)
Definition: json_converter_of_t.h:80
virtual void WriteAsPropertyNameCore(System::SharedPtr< Utf8JsonWriter > writer, T value, System::SharedPtr< JsonSerializerOptions > options, bool isWritingExtensionDataProperty)
Definition: json_converter_of_t.h:570
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: json_converter_of_t.h:151
virtual T ReadAsPropertyNameCore(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options)
Definition: json_converter_of_t.h:557
virtual T ReadAsPropertyName(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options)
Definition: json_converter_of_t.h:127
virtual void WriteAsPropertyName(System::SharedPtr< Utf8JsonWriter > writer, T value, System::SharedPtr< JsonSerializerOptions > options)
Definition: json_converter_of_t.h:139
Definition: utf8_json_reader.h:78
Definition: write_stack.h:34
Represents a particular type and provides information about it.
Definition: type_info.h:109
@ Text
Defines color adjustment information for text.
JsonValueType
Definition: json_value_type.h:15
JsonNumberHandling
Definition: json_number_handling.h:16
Definition: db_command.h:9
decltype(Ref(std::declval< T & >())) Ref(const std::reference_wrapper< T > &wrapper)
Wrapper to make sure Ref(std::ref(DynamicWeakPtr)) works.
Definition: dynamic_weak_ptr.h:207
A template predicate that determines if its template argument T in Nullable or its subclass.
Definition: nullable.h:23