CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
json_dictionary_converter.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/details/pointer_collection_helpers.h>
8#include <system/details/patterns.h>
9#include <system/default.h>
10#include <system/constraints.h>
11#include <system/action.h>
12#include <cstdint>
13
14#include "system/text/json/serialization/json_resumable_converter_of_t.h"
15#include "system/text/json/writer/utf8_json_writer.h"
16#include "system/text/json/serialization/json_serializer_options.h"
17#include "system/text/json/serialization/write_stack.h"
18#include "system/text/json/serialization/converter_strategy.h"
19#include "system/text/json/serialization/json_converter.h"
20#include "system/text/json/reader/utf8_json_reader.h"
21#include "system/text/json/serialization/read_stack.h"
22#include "system/text/json/serialization/read_stack_frame.h"
23#include "system/text/json/json_token_type.h"
24#include "system/text/json/throw_helper.h"
25#include "system/text/json/json_helpers.h"
26#include "system/text/json/serialization/stack_frame_object_state.h"
27#include "system/text/json/serialization/json_serializer_helpers.h"
28#include "system/text/json/serialization/metadata_property_name.h"
29#include "system/text/json/serialization/polymorphic_serialization_state.h"
30#include "system/text/json/serialization/stack_frame_property_state.h"
31#include "system/text/json/serialization/metadata/polymorphic_type_resolver.h"
32#include "system/text/json/serialization/metadata/json_type_info.h"
33#include "system/text/json/serialization/write_stack_frame.h"
34
35namespace System
36{
37namespace Text
38{
39namespace Json
40{
41namespace Serialization
42{
43namespace Metadata
44{
45class JsonPropertyInfoBase;
46} // namespace Metadata
47} // namespace Serialization
48} // namespace Json
49} // namespace Text
50} // namespace System
51
52namespace System {
53
54namespace Text {
55
56namespace Json {
57
58namespace Serialization {
59
60template<typename TDictionary>
61class JsonDictionaryConverter<TDictionary> : public JsonResumableConverter<TDictionary>
62{
65
66 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
67 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
68
69 template<typename...> friend class JsonDictionaryConverter;
70
71public:
72
74 {
75 return true;
76 }
77
78 virtual bool OnWriteResume(System::SharedPtr<Utf8JsonWriter> writer, TDictionary dictionary, System::SharedPtr<JsonSerializerOptions> options, WriteStack& state) = 0;
79 void SetTemplateWeakPtr(uint32_t argument) override
80 {
81 switch (argument)
82 {
83 case 0:
84 BaseType::SetTemplateWeakPtr(0);
85 break;
86
87 }
88 }
89
90protected:
91
93 {
94 }
95
96private:
97
98 System::Text::Json::ConverterStrategy GetDefaultConverterStrategy() override
99 {
101 }
102
103};
104
105template<typename TDictionary, typename TKey, typename TValue>
107{
110
111 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
112 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
113
114 template<typename...> friend class JsonDictionaryConverter;
115
116public:
117
119 {
120 return System::ObjectExt::GetType<TValue>();
121 }
122
124 {
125 return System::ObjectExt::GetType<TKey>();
126 }
127
128 bool OnTryRead(Utf8JsonReader& reader, const System::TypeInfo& typeToConvert, System::SharedPtr<JsonSerializerOptions> options, ReadStack& state, TDictionary& value) override
129 {
130 ASPOSE_UNUSED(typeToConvert);
132 System::SharedPtr<Metadata::JsonTypeInfoBase> keyTypeInfo = jsonTypeInfo->get_KeyTypeInfo();
133 System::SharedPtr<Metadata::JsonTypeInfoBase> elementTypeInfo = jsonTypeInfo->get_ElementTypeInfo();
134
135 static auto ReadDictionaryKey = [](System::SharedPtr<JsonConverter<TKey>> keyConverter, Utf8JsonReader& reader, ReadStack& state, System::SharedPtr<JsonSerializerOptions> options) -> auto
136 {
137 TKey key;
138 String unescapedPropertyNameAsString = reader.GetString();
139 state.Current.JsonPropertyNameAsString = unescapedPropertyNameAsString;
140 // Copy key name for JSON Path support in case of error.
141
142 // Special case string to avoid calling GetString twice and save one allocation.
143 if (keyConverter->get_IsInternalConverter() && keyConverter->get_Type() == System::ObjectExt::GetType<String>())
144 {
145 key = System::ExplicitCast<TKey>(System::ExplicitCast<System::Object>(unescapedPropertyNameAsString));
146 }
147 else
148 {
149 key = keyConverter->ReadAsPropertyNameCore(System::Ref(reader), keyConverter->get_Type(), options);
150 }
151
152 return key;
153 };
154
156 {
157 // Fast path that avoids maintaining state variables and dealing with preserved references.
158
160 {
162 }
163
164 CreateCollection(System::Ref(reader), System::Ref(state));
165
166 jsonTypeInfo->InvokeOnDeserializing(state.Current.ReturnValue);
167
168 System::ObjectExt::CoalesceAssign(_keyConverter, [&]{return GetConverter<TKey>(keyTypeInfo);});
169 System::ObjectExt::CoalesceAssign(_valueConverter, [&]{return GetConverter<TValue>(elementTypeInfo);});
170
171 if (_valueConverter->get_CanUseDirectReadOrWrite() && state.Current.NumberHandling == nullptr)
172 {
173 // Process all elements.
174 while (true)
175 {
176 // Read the key name.
178
180 {
181 break;
182 }
183
184 // Read method would have thrown if otherwise.
185
186 state.Current.JsonPropertyInfo = keyTypeInfo->get_PropertyInfoForTypeInfo();
187 TKey key = ReadDictionaryKey(_keyConverter, System::Ref(reader), System::Ref(state), options);
188
189 // Read the value and add.
191 state.Current.JsonPropertyInfo = elementTypeInfo->get_PropertyInfoForTypeInfo();
192 TValue element = _valueConverter->Read(System::Ref(reader), get_ElementType(), options);
193 Add(key, element, options, System::Ref(state));
194 }
195 }
196 else
197 {
198 // Process all elements.
199 while (true)
200 {
201 // Read the key name.
203
205 {
206 break;
207 }
208
209 // Read method would have thrown if otherwise.
210
211 state.Current.JsonPropertyInfo = keyTypeInfo->get_PropertyInfoForTypeInfo();
212 TKey key = ReadDictionaryKey(_keyConverter, System::Ref(reader), System::Ref(state), options);
213
215
216 // Get the value from the converter and add it.
217 state.Current.JsonPropertyInfo = elementTypeInfo->get_PropertyInfoForTypeInfo();
218 TValue element;
219 _valueConverter->TryRead(System::Ref(reader), get_ElementType(), options, System::Ref(state), element, System::Discard<bool>());
220 Add(key, element, options, System::Ref(state));
221 }
222 }
223 }
224 else
225 {
226 // Slower path that supports continuation and reading metadata.
228 {
230 {
232 }
233
235 }
236
237 // Handle the metadata properties.
239 {
241 {
242 value = System::Default<TDictionary>();
243 return false;
244 }
245
247 }
248
249 // Dispatch to any polymorphic converters: should always be entered regardless of ObjectState progress
250 System::SharedPtr<JsonConverterBase> polymorphicConverter;
251 if (((state.Current.MetadataPropertyNames & System::Text::Json::MetadataPropertyName::Type)!= static_cast<MetadataPropertyName>(0) || System::Is(state.PolymorphicResolvedType, !(_== nullptr))) && state.Current.PolymorphicSerializationState != System::Text::Json::PolymorphicSerializationState::PolymorphicReEntryStarted && System::Is<JsonConverterBase>(this->ResolvePolymorphicConverter(jsonTypeInfo, System::Ref(state)), polymorphicConverter))
252 {
253
254 System::SharedPtr<Object> objectResult;
255 bool success = polymorphicConverter->OnTryReadAsObject(System::Ref(reader), polymorphicConverter->get_Type(), options, System::Ref(state), objectResult);
256 value = System::ExplicitCast<TDictionary>(objectResult);
257 state.ExitPolymorphicConverter(success);
258 return success;
259 }
260
261 // Create the dictionary.
263 {
264 if (state.Current.CanContainMetadata)
265 {
267 }
268
269 CreateCollection(System::Ref(reader), System::Ref(state));
270
271 jsonTypeInfo->InvokeOnDeserializing(state.Current.ReturnValue);
272
274 }
275
276 // Process all elements.
277 System::ObjectExt::CoalesceAssign(_keyConverter, [&]{return GetConverter<TKey>(keyTypeInfo);});
278 System::ObjectExt::CoalesceAssign(_valueConverter, [&]{return GetConverter<TValue>(elementTypeInfo);});
279 while (true)
280 {
282 {
283 // Read the key name.
284 if (!reader.Read())
285 {
286 value = System::Default<TDictionary>();
287 return false;
288 }
289
291 }
292
293 // Determine the property.
294 TKey key;
296 {
298 {
299 break;
300 }
301
302 // Read method would have thrown if otherwise.
303
305
306 if (state.Current.CanContainMetadata)
307 {
309 if (System::Text::Json::Serialization::Details::IsMetadataPropertyName(propertyName, state.Current.get_BaseJsonTypeInfo()->get_PolymorphicTypeResolver()))
310 {
312 }
313 }
314
315 state.Current.JsonPropertyInfo = keyTypeInfo->get_PropertyInfoForTypeInfo();
316 key = ReadDictionaryKey(_keyConverter, System::Ref(reader), System::Ref(state), options);
317 }
318 else
319 {
320 // DictionaryKey is assigned before all return false cases, null value is unreachable
321 key = System::ExplicitCast<TKey>(state.Current.DictionaryKey);
322 }
323
325 {
326 if (!JsonHelpers::TryAdvanceWithOptionalReadAhead(reader, _valueConverter->get_RequiresReadAhead()))
327 {
328 state.Current.DictionaryKey = System::ExplicitCast<System::Object>(key);
329 value = System::Default<TDictionary>();
330 return false;
331 }
332
334 }
335
337 {
338 // Get the value from the converter and add it.
339 state.Current.JsonPropertyInfo = elementTypeInfo->get_PropertyInfoForTypeInfo();
340 TValue element2;
341 bool success = _valueConverter->TryRead(System::Ref(reader), System::ObjectExt::GetType<TValue>(), options, System::Ref(state), element2, System::Discard<bool>());
342 if (!success)
343 {
344 state.Current.DictionaryKey = System::ExplicitCast<System::Object>(key);
345 value = System::Default<TDictionary>();
346 return false;
347 }
348
349 Add(key, element2, options, System::Ref(state));
350 state.Current.EndElement();
351 }
352 }
353 }
354
355 ConvertCollection(System::Ref(state), options);
357 jsonTypeInfo->InvokeOnDeserialized(result);
358 value = System::ExplicitCast<TDictionary>(result);
359
360 return true;
361 }
362
363 bool OnTryWrite(System::SharedPtr<Utf8JsonWriter> writer, TDictionary dictionary, System::SharedPtr<JsonSerializerOptions> options, WriteStack& state) override
364 {
365 if (System::IsNull(dictionary))
366 {
367 writer->WriteNullValue();
368 return true;
369 }
370
372
373 if (!state.Current.ProcessedStartToken)
374 {
375 state.Current.ProcessedStartToken = true;
376
377 jsonTypeInfo->InvokeOnSerializing(System::ExplicitCast<System::Object>(dictionary));
378
379 writer->WriteStartObject();
380
381 if (state.get_CurrentContainsMetadata() && this->get_CanHaveMetadata())
382 {
384 }
385
386 state.Current.JsonPropertyInfo = jsonTypeInfo->get_ElementTypeInfo()->get_PropertyInfoForTypeInfo();
387 }
388
389 bool success = this->OnWriteResume(writer, dictionary, options, System::Ref(state));
390 if (success)
391 {
392 if (!state.Current.ProcessedEndToken)
393 {
394 state.Current.ProcessedEndToken = true;
395 writer->WriteEndObject();
396 }
397
398 jsonTypeInfo->InvokeOnSerialized(System::ExplicitCast<System::Object>(dictionary));
399 }
400
401 return success;
402 }
403
404 void SetTemplateWeakPtr(uint32_t argument) override
405 {
406 switch (argument)
407 {
408 case 0:
409 BaseType::SetTemplateWeakPtr(0);
410 break;
411
412 case 1:
413 System::Details::CollectionHelpers::SetWeakPointer(0, _keyConverter);
414 break;
415
416 case 2:
417 System::Details::CollectionHelpers::SetWeakPointer(0, _valueConverter);
418 break;
419
420 }
421 }
422
423protected:
424
427
428 virtual void Add(TKey key, const TValue& value, System::SharedPtr<JsonSerializerOptions> options, ReadStack& state) = 0;
430 {
431 ASPOSE_UNUSED(state);
432 ASPOSE_UNUSED(options);
433 }
434
435 virtual void CreateCollection(Utf8JsonReader& reader, ReadStack& state)
436 {
437 if (System::SafeInvoke(state.get_ParentProperty(), [&](auto&& expr){return expr->TryGetPrePopulatedValue(System::Ref(state));}) == true)
438 {
439 return;
440 }
441
443
444 if (typeInfo->get_HasCreateObject() == false)
445 {
447 }
448
449 state.Current.ReturnValue = typeInfo->CreateObjectInstance();
450
451 }
452
453 template<typename T>
455 {
457 }
458
460 {
461 }
462
463 #ifdef ASPOSE_GET_SHARED_MEMBERS
464 void GetSharedMembers(System::Object::shared_members_type& result) const override
465 {
467
468 result.Add("System::Text::Json::Serialization::JsonDictionaryConverter::_keyConverter", this->_keyConverter);
469 result.Add("System::Text::Json::Serialization::JsonDictionaryConverter::_valueConverter", this->_valueConverter);
470 }
471 #endif
472
473};
474
475} // namespace Serialization
476} // namespace Json
477} // namespace Text
478} // namespace System
static auto CoalesceAssign(T0 &value, T1 func) -> T0 &
Implementation of '??=' operator translation.
Definition: object_ext.h:748
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
static bool TryAdvanceWithOptionalReadAhead(Utf8JsonReader &reader, bool requiresReadAhead)
static void ReadWithVerify(Utf8JsonReader &reader)
static ReadOnlySpan< uint8_t > GetUnescapedSpan(Utf8JsonReader &reader)
System::Text::Json::PolymorphicSerializationState PolymorphicSerializationState
Definition: read_stack_frame.h:65
String JsonPropertyNameAsString
Definition: read_stack_frame.h:55
bool CanContainMetadata
Definition: read_stack_frame.h:62
Nullable< Serialization::JsonNumberHandling > NumberHandling
Definition: read_stack_frame.h:73
System::SharedPtr< System::Text::Json::Serialization::Metadata::JsonTypeInfoBase > get_BaseJsonTypeInfo()
System::SharedPtr< Object > ReturnValue
Definition: read_stack_frame.h:59
MetadataPropertyName MetadataPropertyNames
Definition: read_stack_frame.h:64
StackFramePropertyState PropertyState
Definition: read_stack_frame.h:52
StackFrameObjectState ObjectState
Definition: read_stack_frame.h:61
System::SharedPtr< Object > DictionaryKey
Definition: read_stack_frame.h:56
System::SharedPtr< System::Text::Json::Serialization::Metadata::JsonPropertyInfoBase > JsonPropertyInfo
Definition: read_stack_frame.h:51
System::SharedPtr< System::Text::Json::Serialization::Metadata::JsonTypeInfoBase > JsonTypeInfo
Definition: read_stack_frame.h:60
Definition: read_stack.h:35
bool SupportContinuation
Definition: read_stack.h:50
void ExitPolymorphicConverter(bool success)
ReadStackFrame Current
Definition: read_stack.h:44
System::SharedPtr< Serialization::Metadata::JsonPropertyInfoBase > get_ParentProperty()
TypeInfo PolymorphicResolvedType
Definition: read_stack.h:52
Definition: json_converter_of_t.h:80
virtual void ConvertCollection(ReadStack &state, System::SharedPtr< JsonSerializerOptions > options)
Definition: json_dictionary_converter.h:429
System::SharedPtr< JsonConverter< TKey > > _keyConverter
Definition: json_dictionary_converter.h:425
System::TypeInfo get_ElementType() override
Definition: json_dictionary_converter.h:118
static System::SharedPtr< JsonConverter< T > > GetConverter(System::SharedPtr< Metadata::JsonTypeInfoBase > typeInfo)
Definition: json_dictionary_converter.h:454
virtual ~JsonDictionaryConverter()
Definition: json_dictionary_converter.h:459
virtual void CreateCollection(Utf8JsonReader &reader, ReadStack &state)
Definition: json_dictionary_converter.h:435
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: json_dictionary_converter.h:404
bool OnTryRead(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options, ReadStack &state, TDictionary &value) override
Definition: json_dictionary_converter.h:128
System::SharedPtr< JsonConverter< TValue > > _valueConverter
Definition: json_dictionary_converter.h:426
bool OnTryWrite(System::SharedPtr< Utf8JsonWriter > writer, TDictionary dictionary, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state) override
Definition: json_dictionary_converter.h:363
virtual void Add(TKey key, const TValue &value, System::SharedPtr< JsonSerializerOptions > options, ReadStack &state)=0
System::TypeInfo get_KeyType() override
Definition: json_dictionary_converter.h:123
friend class JsonDictionaryConverter
Definition: json_dictionary_converter.h:114
bool get_SupportsCreateObjectDelegate() override
Definition: json_dictionary_converter.h:73
virtual ~JsonDictionaryConverter()
Definition: json_dictionary_converter.h:92
virtual bool OnWriteResume(System::SharedPtr< Utf8JsonWriter > writer, TDictionary dictionary, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state)=0
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: json_dictionary_converter.h:79
friend class JsonDictionaryConverter
Definition: json_dictionary_converter.h:69
Definition: json_resumable_converter_of_t.h:42
static void ThrowJsonException_DeserializeUnableToConvertValue(const TypeInfo &propertyType)
static void ThrowNotSupportedException_DeserializeNoConstructor(System::SharedPtr< Serialization::Metadata::JsonTypeInfoBase > typeInfo, Utf8JsonReader &reader, ReadStack &state)
static void ThrowUnexpectedMetadataException(ReadOnlySpan< uint8_t > propertyName, Utf8JsonReader &reader, ReadStack &state)
Definition: utf8_json_reader.h:78
bool ProcessedStartToken
Definition: write_stack_frame.h:58
System::SharedPtr< System::Text::Json::Serialization::Metadata::JsonPropertyInfoBase > JsonPropertyInfo
Definition: write_stack_frame.h:54
bool ProcessedEndToken
Definition: write_stack_frame.h:59
System::SharedPtr< System::Text::Json::Serialization::Metadata::JsonTypeInfoBase > JsonTypeInfo
Definition: write_stack_frame.h:56
Definition: write_stack.h:34
WriteStackFrame Current
Definition: write_stack.h:45
Represents a particular type and provides information about it.
Definition: type_info.h:109
@ Text
Defines color adjustment information for text.
void ValidateMetadataForObjectConverter(ReadStack &state)
MetadataPropertyName WriteMetadataForObject(System::SharedPtr< JsonConverterBase > jsonConverter, WriteStack &state, System::SharedPtr< Utf8JsonWriter > writer)
bool IsMetadataPropertyName(ReadOnlySpan< uint8_t > propertyName, System::SharedPtr< Metadata::PolymorphicTypeResolver > resolver)
bool TryReadMetadata(System::SharedPtr< JsonConverterBase > converter, System::SharedPtr< Metadata::JsonTypeInfoBase > jsonTypeInfo, Utf8JsonReader &reader, ReadStack &state)
MetadataPropertyName
Definition: metadata_property_name.h:13
ConverterStrategy
Definition: converter_strategy.h:13
Definition: db_command.h:9
SmartPtr< X > MakeSharedPtr(X *p)
Converts raw pointer to smart pointer.
Definition: smart_ptr.h:1640
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
static auto SafeInvoke(T0 &&expr, T1 &&func)
Implementation of '?.' operator translation.
Definition: object_ext.h:1434
std::enable_if_t< Details::CastType< Source, Result >::None, Result > ExplicitCast(const Source &value)
Casts the source type to the result type using explicit cast. Used when the source and the result typ...
Definition: object_ext.h:960