CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
i_dictionary_converter.h
1#pragma once
2
3#include <system/type_info.h>
4#include <system/object_ext.h>
5#include <system/func.h>
6#include <system/details/pointer_collection_helpers.h>
7#include <system/details/patterns.h>
8#include <system/constraints.h>
9#include <system/collections/ienumerator_ng.h>
10#include <system/collections/idictionary.h>
11#include <system/collections/dictionary.h>
12#include <functional>
13#include <cstdint>
14
15#include "system/text/json/serialization/converters/collection/json_dictionary_converter.h"
16#include "system/text/json/writer/utf8_json_writer.h"
17#include "system/text/json/serialization/json_serializer_options.h"
18#include "system/text/json/serialization/write_stack.h"
19#include "system/text/json/serialization/write_stack_frame.h"
20#include "system/text/json/serialization/json_converter.h"
21#include "system/text/json/serialization/stack_frame_property_state.h"
22#include "system/text/json/serialization/read_stack.h"
23#include "system/text/json/serialization/read_stack_frame.h"
24#include "system/text/json/throw_helper.h"
25#include "system/text/json/reader/utf8_json_reader.h"
26
27namespace System {
28
29namespace Text {
30
31namespace Json {
32
33namespace Serialization {
34
35namespace Converters {
36
37template<typename TDictionary>
38class IDictionaryConverter final : public JsonDictionaryConverter<TDictionary, String, System::SharedPtr<Object>>
39{
41
44
45 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
46 RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
47
48 template<typename FT0> friend class IDictionaryConverter;
49
50public:
51
52 bool get_CanPopulate() override
53 {
54 return true;
55 }
56
58 {
60 if (state.Current.CollectionEnumerator == nullptr)
61 {
62 enumerator = value->GetEnumerator();
63 state.Current.CollectionEnumerator = enumerator;
64 if (!enumerator->MoveNext())
65 {
66 return true;
67 }
68 }
69 else
70 {
71 enumerator = System::ExplicitCast<System::Collections::Generic::IEnumerator<System::Collections::Generic::KeyValuePair<System::String, System::SharedPtr<System::Object>>>>(state.Current.CollectionEnumerator);
72 }
73
75 System::ObjectExt::CoalesceAssign(JsonDictionaryConverter<TDictionary, String, System::SharedPtr<Object>>::_valueConverter, [&]{return JsonDictionaryConverter<TDictionary, String, System::SharedPtr<Object>>::template GetConverter<System::SharedPtr<System::Object>>(typeInfo->get_ElementTypeInfo());});
76
77 do
78 {
79 if (this->ShouldFlush(System::Ref(state), writer))
80 {
81 return false;
82 }
83
85 {
87 System::SharedPtr<Object> key = System::ExplicitCast<System::Object>(enumerator->get_Current().get_Key());
88 // Optimize for string since that's the hot path.
89 String keyString;
90 if (System::Is<String>(key, keyString))
91 {
92 System::ObjectExt::CoalesceAssign(JsonDictionaryConverter<TDictionary, String, System::SharedPtr<Object>>::_keyConverter, [&]{return JsonDictionaryConverter<TDictionary, String, System::SharedPtr<Object>>::template GetConverter<System::String>(typeInfo->get_KeyTypeInfo());});
93 JsonDictionaryConverter<TDictionary, String, System::SharedPtr<Object>>::_keyConverter->WriteAsPropertyNameCore(writer, keyString, options, state.Current.IsWritingExtensionDataProperty);
94 }
95 else
96 {
97 // IDictionary is a special case since it has polymorphic object semantics on serialization
98 // but needs to use JsonConverter<string> on deserialization.
99 JsonDictionaryConverter<TDictionary, String, System::SharedPtr<Object>>::_valueConverter->WriteAsPropertyNameCore(writer, key, options, state.Current.IsWritingExtensionDataProperty);
100 }
101 }
102
103 System::SharedPtr<Object> element = enumerator->get_Current().get_Value();
104 if (!JsonDictionaryConverter<TDictionary, String, System::SharedPtr<Object>>::_valueConverter->TryWrite(writer, element, options, System::Ref(state)))
105 {
106 return false;
107 }
108
110 } while (enumerator->MoveNext());
111
112 return true;
113 }
114
116 {
117 ASPOSE_UNUSED(options);
118 // Deserialize as Dictionary<TKey,TValue> for interface types that support it.
119 if (jsonTypeInfo->get_HasCreateObject() == false && JsonConverter<TDictionary>::get_Type().IsAssignableFrom(System::ObjectExt::GetType<Collections::Generic::Dictionary<String, System::SharedPtr<Object>>>()))
120 {
121
122 jsonTypeInfo->set_CreateObject(static_cast<Func<System::SharedPtr<Object>>>(static_cast<std::function<System::SharedPtr<System::Object>()>>([]() -> System::SharedPtr<System::Object>
123 {
124 return System::MakeObject<Collections::Generic::Dictionary<String, System::SharedPtr<Object>>>();
125 })));
126 }
127 }
128
129 void SetTemplateWeakPtr(uint32_t argument) override
130 {
131 switch (argument)
132 {
133 case 0:
134 BaseType::SetTemplateWeakPtr(0);
135 break;
136
137 }
138 }
139
140protected:
141
143 {
144 TDictionary collection = System::ExplicitCast<TDictionary>(state.Current.ReturnValue);
145
146 collection->idx_set(System::ExplicitCast<System::Object>(key), value);
147
148 if (this->get_IsValueType())
149 {
150 state.Current.ReturnValue = System::ExplicitCast<System::Object>(collection);
151 }
152 }
153
154 void CreateCollection(Utf8JsonReader& reader, ReadStack& state) override
155 {
157 TDictionary returnValue = System::ExplicitCast<TDictionary>(state.Current.ReturnValue);
158 if (returnValue->get_IsReadOnly())
159 {
160 state.Current.ReturnValue.reset();
161 // clear out for more accurate JsonPath reporting.
163 }
164 }
165
166};
167
168} // namespace Converters
169} // namespace Serialization
170} // namespace Json
171} // namespace Text
172} // namespace System
Forward declaration of Dictionary class.
Definition: dictionary.h:96
Interface for dictionary-alike containers. Objects of this class should only be allocated using Syste...
Definition: idictionary.h:21
Function delegate. This type should be allocated on stack and passed to functions by value or by refe...
Definition: func.h:82
static auto CoalesceAssign(T0 &value, T1 func) -> T0 &
Implementation of '??=' operator translation.
Definition: object_ext.h:748
static std::enable_if< std::is_fundamental< T >::value &&!std::is_enum< T >::value, constSystem::TypeInfo & >::type GetType()
Implements typeof() translation. Overload for primitive types.
Definition: object_type.h:94
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
System::SharedPtr< Object > ReturnValue
Definition: read_stack_frame.h:59
Definition: read_stack.h:35
ReadStackFrame Current
Definition: read_stack.h:44
bool OnWriteResume(System::SharedPtr< Utf8JsonWriter > writer, TDictionary value, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state) override
Definition: i_dictionary_converter.h:57
void ConfigureJsonTypeInfo(System::SharedPtr< Metadata::JsonTypeInfoBase > jsonTypeInfo, System::SharedPtr< JsonSerializerOptions > options) override
Definition: i_dictionary_converter.h:115
void SetTemplateWeakPtr(uint32_t argument) override
Definition: i_dictionary_converter.h:129
void CreateCollection(Utf8JsonReader &reader, ReadStack &state) override
Definition: i_dictionary_converter.h:154
bool get_CanPopulate() override
Definition: i_dictionary_converter.h:52
void Add(String key, const System::SharedPtr< Object > &value, System::SharedPtr< JsonSerializerOptions > options, ReadStack &state) override
Definition: i_dictionary_converter.h:142
Definition: json_converter_of_t.h:80
static void ThrowNotSupportedException_CannotPopulateCollection(const TypeInfo &type, Utf8JsonReader &reader, ReadStack &state)
Definition: utf8_json_reader.h:78
System::SharedPtr< Object > CollectionEnumerator
Definition: write_stack_frame.h:53
bool IsWritingExtensionDataProperty
Definition: write_stack_frame.h:55
System::SharedPtr< System::Text::Json::Serialization::Metadata::JsonTypeInfoBase > JsonTypeInfo
Definition: write_stack_frame.h:56
StackFramePropertyState PropertyState
Definition: write_stack_frame.h:60
Definition: write_stack.h:34
WriteStackFrame Current
Definition: write_stack.h:45
@ Text
Defines color adjustment information for text.
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