3#include <system/type_info.h>
4#include <system/scope_guard.h>
5#include <system/object_ext.h>
6#include <system/nullable.h>
7#include <system/exceptions.h>
8#include <system/diagnostics/debug.h>
9#include <system/details/pointer_collection_helpers.h>
10#include <system/details/patterns.h>
11#include <system/default.h>
14#include "system/text/json/reader/utf8_json_reader.h"
15#include "system/text/json/serialization/json_serializer_options.h"
16#include "system/text/json/writer/utf8_json_writer.h"
17#include "system/text/json/throw_helper.h"
18#include "system/text/json/serialization/json_serializer_helpers.h"
19#include "system/text/json/serialization/write_stack.h"
20#include "system/text/json/serialization/json_number_handling.h"
21#include "system/text/json/serialization/read_stack.h"
22#include "system/text/json/json_token_type.h"
23#include "system/text/json/serialization/converter_strategy.h"
24#include "system/text/json/serialization/read_stack_frame.h"
25#include "system/text/json/serialization/write_stack_frame.h"
26#include "system/text/json/serialization/polymorphic_serialization_state.h"
27#include "system/text/json/json_helpers.h"
28#include "system/text/json/reader/json_reader_exception.h"
29#include "system/text/json/json_exception.h"
30#include "system/text/json/serialization/converters/collection/json_dictionary_converter.h"
31#include "system/text/json/serialization/converters/json_metadata_services_converter.h"
32#include "system/text/json/serialization/metadata/json_property_info.h"
33#include "system/text/json/serialization/metadata/json_type_info_of_t.h"
43template <
typename>
class JsonValuePrimitive;
49template <
typename,
typename>
class ArrayConverter;
50template <
typename>
class CastingConverter;
51template <
typename,
typename,
typename>
class DictionaryDefaultConverter;
52template <
typename,
typename,
typename>
class DictionaryOfTKeyTValueConverter;
53template <
typename>
class IDictionaryConverter;
54template <
typename>
class IEnumerableConverter;
55template <
typename,
typename>
class IEnumerableDefaultConverter;
56template <
typename>
class IListConverter;
57template <
typename>
class JsonMetadataServicesConverter;
58class JsonObjectConverter;
59template <
typename,
typename>
class ListOfTConverter;
60template <
typename>
class NullableConverter;
61template <
typename>
class StackOrQueueConverter;
63template <
typename,
typename>
class JsonCollectionConverter;
64template <
typename...>
class JsonDictionaryConverter;
84 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
85 RTTI_INFO_TEMPLATE_CLASS(
ThisType, ThisTypeBaseTypesInfo);
111 set_UsesDefaultHandleNull(
true);
122 return typeToConvert == System::ObjectExt::GetType<T>();
131 if (System::IsNull(fallbackConverter))
136 return fallbackConverter->ReadAsPropertyNameCore(
System::Ref(reader), typeToConvert, options);
143 if (System::IsNull(fallbackConverter))
148 fallbackConverter->WriteAsPropertyNameCore(writer,
value, options,
false);
156 System::Details::CollectionHelpers::SetWeakPointer(0, _fallbackConverterForPropertyNameSerialization);
166 System::Details::ThisProtector __local_self_ref(
this);
168 init_IsValueType(System::ObjectExt::GetType<T>().get_IsValueType());
170 if (get_HandleNull())
172 init_HandleNullOnRead(
true);
173 init_HandleNullOnWrite(
true);
175 else if (get_UsesDefaultHandleNull())
179 init_HandleNullOnRead(!System::IsNull(System::Default<T>()));
182 init_HandleNullOnWrite(
false);
193 T valueOfT = System::Text::Json::Serialization::Details::UnboxOnWrite<T>(
value);
194 Write(writer, valueOfT, options);
199 T valueOfT = System::Text::Json::Serialization::Details::UnboxOnWrite<T>(
value);
200 return OnTryWrite(writer, valueOfT, options,
System::Ref(state));
205 T valueOfT = System::Text::Json::Serialization::Details::UnboxOnWrite<T>(
value);
206 WriteAsPropertyName(writer, valueOfT, options);
211 T valueOfT = System::Text::Json::Serialization::Details::UnboxOnWrite<T>(
value);
212 WriteAsPropertyNameCore(writer, valueOfT, options, isWritingExtensionDataProperty);
217 T valueOfT = System::Text::Json::Serialization::Details::UnboxOnWrite<T>(
value);
218 WriteNumberWithCustomHandling(writer, valueOfT, handling);
223 T valueOfT = System::Text::Json::Serialization::Details::UnboxOnWrite<T>(
value);
224 return TryWrite(writer, valueOfT, options,
System::Ref(state));
229 ASPOSE_UNUSED(state);
230 Write(writer,
value, options);
236 ASPOSE_UNUSED(state);
245 if (!System::IsNull(System::Default<T>()))
250 value = System::Default<T>();
251 isPopulatedValue =
false;
264 if (System::Is(state.
Current.
NumberHandling, !(_==
nullptr)) && get_IsInternalConverterForNumberType())
273 VerifyRead(originalPropertyTokenType, originalPropertyDepth, originalPropertyBytesConsumed,
true,
System::Ref(reader));
276 isPopulatedValue =
false;
283 if (!System::ObjectExt::GetType<T>().get_IsValueType() && get_CanBePolymorphic())
290 isPopulatedValue =
false;
312 if (!(parentObj ==
nullptr) && !(propertyInfo ==
nullptr) && !propertyInfo->get_IsForTypeInfo())
340 T typedValue = System::Default<T>();
342 value = System::ExplicitCast<System::Object>(typedValue);
348 T typedValue = System::Default<T>();
350 value = System::ExplicitCast<System::Object>(typedValue);
356 T typedValue = Read(
System::Ref(reader), typeToConvert, options);
357 return System::ExplicitCast<System::Object>(typedValue);
362 T typedValue = ReadAsPropertyName(
System::Ref(reader), typeToConvert, options);
363 return System::ExplicitCast<System::Object>(typedValue);
368 T typedValue = ReadAsPropertyNameCore(
System::Ref(reader), typeToConvert, options);
369 return System::ExplicitCast<System::Object>(typedValue);
374 T typedValue = ReadNumberWithCustomHandling(
System::Ref(reader), handling, options);
375 return System::ExplicitCast<System::Object>(typedValue);
380 if (writer->get_CurrentDepth() >= options->get_EffectiveMaxDepth())
385 if (System::IsNull(System::Default<T>()) && !get_HandleNullOnWrite() && IsNull(
value))
389 writer->WriteNullValue();
396 int32_t originalPropertyDepth = writer->get_CurrentDepth();
398 if (System::Is(state.
Current.
NumberHandling, !(_==
nullptr)) && get_IsInternalConverterForNumberType())
404 Write(writer,
value, options);
407 VerifyWrite(originalPropertyDepth, writer);
418 bool canBePolymorphic = get_CanBePolymorphic() || System::Is(jsonTypeInfo->get_PolymorphicTypeResolver(), !(_==
nullptr));
421 if (!(polymorphicConverter ==
nullptr))
423 success = polymorphicConverter->TryWriteAsObject(writer, System::ExplicitCast<System::Object>(
value), options,
System::Ref(state));
441 if (!get_IsInternalConverter())
448 if (System::IsNull(dictionaryConverter))
454 WriteExtensionDataValue(writer,
value, options);
459 if (writer->get_CurrentDepth() >= options->get_EffectiveMaxDepth())
479 success = dictionaryConverter->OnWriteResume(writer,
value, options,
System::Ref(state));
492 ASPOSE_UNUSED(writer);
493 ASPOSE_UNUSED(
value);
494 ASPOSE_UNUSED(options);
495 CODEPORTING_DEBUG_FAIL1(u
"Should not be reachable.");
497 throw InvalidOperationException();
528 if (isValueConverter)
551 if (originalDepth != writer->get_CurrentDepth())
561 T result = ReadAsPropertyName(
System::Ref(reader), typeToConvert, options);
572 if (System::IsNull(
value))
574 throw System::ArgumentNullException(u
"value");
577 if (isWritingExtensionDataProperty)
586 int32_t originalDepth = writer->get_CurrentDepth();
587 WriteAsPropertyName(writer,
value, options);
596 ASPOSE_UNUSED(reader);
597 ASPOSE_UNUSED(handling);
598 ASPOSE_UNUSED(options);
599 throw InvalidOperationException();
604 ASPOSE_UNUSED(writer);
605 ASPOSE_UNUSED(
value);
606 ASPOSE_UNUSED(handling);
607 throw InvalidOperationException();
618 if (!JsonHelpers::TryAdvanceWithOptionalReadAhead(reader, get_RequiresReadAhead()))
626 value = System::ExplicitCast<T>(result);
630 value = System::Default<T>();
657 if (System::ObjectExt::Is<JsonReaderException>(ex))
661 else if (System::ObjectExt::Is<FormatException>(ex))
665 else if (System::ObjectExt::Is<JsonException>(ex))
669 else if (System::ObjectExt::Is<NotSupportedException>(ex))
688 if (System::ObjectExt::Is<JsonException>(ex))
692 else if (System::ObjectExt::Is<NotSupportedException>(ex))
705 #ifdef ASPOSE_GET_SHARED_MEMBERS
706 void GetSharedMembers(System::Object::shared_members_type& result)
const override
708 JsonConverterBase::GetSharedMembers(result);
710 result.Add(
"System::Text::Json::Serialization::JsonConverter::pr_Type", this->pr_Type);
711 result.Add(
"System::Text::Json::Serialization::JsonConverter::_fallbackConverterForPropertyNameSerialization", this->_fallbackConverterForPropertyNameSerialization);
725 static bool IsNull(T value)
727 return System::IsNull(value);
732 ASPOSE_UNUSED(options);
T get_Value() const
Returns a copy of the value represented by the current object.
Definition: nullable.h:144
Provides static methods that emulate C# Object methods called for non-Object C++ types (strings,...
Definition: object_ext.h:23
static String ToString(const char_t *obj)
Substitution for C# ToString method to work on any C++ type.
Definition: object_ext.h:103
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
Definition: json_value_of_t_primitive.h:47
bool HasParentObject
Definition: read_stack_frame.h:75
bool IsPopulating
Definition: read_stack_frame.h:76
Nullable< Serialization::JsonNumberHandling > NumberHandling
Definition: read_stack_frame.h:73
System::SharedPtr< Object > ReturnValue
Definition: read_stack_frame.h:59
int32_t OriginalDepth
Definition: read_stack_frame.h:57
JsonTokenType OriginalTokenType
Definition: read_stack_frame.h:58
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
bool get_IsContinuation()
ReadStackFrame Current
Definition: read_stack.h:44
Definition: array_converter.h:32
Definition: casting_converter.h:48
Definition: dictionary_default_converter.h:32
Definition: dictionary_of_t_key_t_value_converter.h:36
Definition: i_dictionary_converter.h:39
Definition: i_enumerable_converter.h:35
Definition: i_enumerable_default_converter.h:30
Definition: i_list_converter.h:36
Definition: json_metadata_services_converter.h:38
Definition: json_object_converter.h:50
Definition: list_of_t_converter.h:35
Definition: nullable_converter.h:52
Definition: stack_or_queue_converter.h:37
Definition: json_converter.h:76
Definition: json_converter_of_t.h:80
bool TryReadAsObject(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options, ReadStack &state, System::SharedPtr< Object > &value) override
Definition: json_converter_of_t.h:346
void WriteNumberWithCustomHandlingAsObject(System::SharedPtr< Utf8JsonWriter > writer, System::SharedPtr< Object > value, JsonNumberHandling handling) override
Definition: json_converter_of_t.h:215
System::SharedPtr< Object > ReadNumberWithCustomHandlingAsObject(Utf8JsonReader &reader, JsonNumberHandling handling, System::SharedPtr< JsonSerializerOptions > options) override
Definition: json_converter_of_t.h:372
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
System::SharedPtr< Object > ReadAsPropertyNameCoreAsObject(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options) override
Definition: json_converter_of_t.h:366
bool WriteCore(System::SharedPtr< Utf8JsonWriter > writer, const T &value, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state)
Definition: json_converter_of_t.h:678
bool OnTryWriteAsObject(System::SharedPtr< Utf8JsonWriter > writer, System::SharedPtr< Object > value, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state) override
Definition: json_converter_of_t.h:197
virtual T ReadAsPropertyNameCore(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options)
Definition: json_converter_of_t.h:557
void WriteAsPropertyNameAsObject(System::SharedPtr< Utf8JsonWriter > writer, System::SharedPtr< Object > value, System::SharedPtr< JsonSerializerOptions > options) override
Definition: json_converter_of_t.h:203
virtual T Read(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options)=0
virtual void Write(System::SharedPtr< Utf8JsonWriter > writer, T value, System::SharedPtr< JsonSerializerOptions > options)=0
System::TypeInfo get_Type() override
Definition: json_converter_of_t.h:115
void WriteAsObject(System::SharedPtr< Utf8JsonWriter > writer, System::SharedPtr< Object > value, System::SharedPtr< JsonSerializerOptions > options) override
Definition: json_converter_of_t.h:191
virtual T ReadNumberWithCustomHandling(Utf8JsonReader &reader, JsonNumberHandling handling, System::SharedPtr< JsonSerializerOptions > options)
Definition: json_converter_of_t.h:594
bool ReadCore(Utf8JsonReader &reader, T &value, System::SharedPtr< JsonSerializerOptions > options, ReadStack &state)
Definition: json_converter_of_t.h:610
friend class JsonCollectionConverter
Definition: json_converter_of_t.h:92
virtual bool OnTryWrite(System::SharedPtr< Utf8JsonWriter > writer, T value, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state)
Definition: json_converter_of_t.h:227
bool CanConvert(const System::TypeInfo &typeToConvert) override
Definition: json_converter_of_t.h:120
virtual void WriteNumberWithCustomHandling(System::SharedPtr< Utf8JsonWriter > writer, T value, JsonNumberHandling handling)
Definition: json_converter_of_t.h:602
bool TryWriteDataExtensionProperty(System::SharedPtr< Utf8JsonWriter > writer, T value, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state)
Definition: json_converter_of_t.h:438
bool TryWriteAsObject(System::SharedPtr< Utf8JsonWriter > writer, System::SharedPtr< Object > value, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state) override
Definition: json_converter_of_t.h:221
System::SharedPtr< Metadata::JsonTypeInfoBase > CreateJsonTypeInfo(System::SharedPtr< JsonSerializerOptions > options) override
Definition: json_converter_of_t.h:186
System::SharedPtr< Object > ReadAsPropertyNameAsObject(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options) override
Definition: json_converter_of_t.h:360
bool OnTryReadAsObject(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options, ReadStack &state, System::SharedPtr< Object > &value) override
Definition: json_converter_of_t.h:338
virtual bool OnTryRead(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options, ReadStack &state, T &value)
Definition: json_converter_of_t.h:234
virtual ~JsonConverter()
Definition: json_converter_of_t.h:701
bool TryWrite(System::SharedPtr< Utf8JsonWriter > writer, const T &value, System::SharedPtr< JsonSerializerOptions > options, WriteStack &state)
Definition: json_converter_of_t.h:378
virtual void WriteExtensionDataValue(System::SharedPtr< Utf8JsonWriter > writer, T value, System::SharedPtr< JsonSerializerOptions > options)
Definition: json_converter_of_t.h:490
virtual T ReadAsPropertyName(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options)
Definition: json_converter_of_t.h:127
void WriteAsPropertyNameCoreAsObject(System::SharedPtr< Utf8JsonWriter > writer, System::SharedPtr< Object > value, System::SharedPtr< JsonSerializerOptions > options, bool isWritingExtensionDataProperty) override
Definition: json_converter_of_t.h:209
void VerifyRead(JsonTokenType tokenType, int32_t depth, int64_t bytesConsumed, bool isValueConverter, Utf8JsonReader &reader)
Definition: json_converter_of_t.h:500
System::SharedPtr< Object > ReadAsObject(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options) override
Definition: json_converter_of_t.h:354
JsonConverter()
Definition: json_converter_of_t.h:164
virtual bool get_HandleNull()
Definition: json_converter_of_t.h:109
bool TryRead(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options, ReadStack &state, T &value, bool &isPopulatedValue)
Definition: json_converter_of_t.h:241
void VerifyWrite(int32_t originalDepth, System::SharedPtr< Utf8JsonWriter > writer)
Definition: json_converter_of_t.h:549
virtual void WriteAsPropertyName(System::SharedPtr< Utf8JsonWriter > writer, T value, System::SharedPtr< JsonSerializerOptions > options)
Definition: json_converter_of_t.h:139
friend class JsonDictionaryConverter
Definition: json_converter_of_t.h:89
Definition: json_converter.h:45
Definition: json_property_info.h:80
Definition: json_property_info_of_t.h:69
Definition: json_type_info_of_t.h:84
static void ThrowJsonException_DeserializeUnableToConvertValue(const TypeInfo &propertyType)
static void ThrowNotSupportedException(ReadStack &state, Utf8JsonReader &reader, Exception innerException)
static void ReThrowWithPath(ReadStack &state, JsonReaderException ex)
static void AddJsonExceptionInformation(ReadStack &state, Utf8JsonReader &reader, JsonException ex)
static void ThrowJsonException_SerializationConverterWrite(System::SharedPtr< Serialization::JsonConverterBase > converter)
static void ThrowNotSupportedException_DictionaryKeyTypeNotSupported(const TypeInfo &keyType, System::SharedPtr< Serialization::JsonConverterBase > converter)
static void ThrowJsonException_SerializationConverterRead(System::SharedPtr< Serialization::JsonConverterBase > converter)
static void ThrowJsonException_SerializerCycleDetected(int32_t maxDepth)
static void ThrowInvalidOperationException_SerializerCycleDetected(int32_t maxDepth)
Definition: utf8_json_reader.h:78
int64_t get_BytesConsumed()
bool get_AllowMultipleValues()
JsonTokenType get_TokenType()
int32_t get_CurrentDepth()
Nullable< Serialization::JsonNumberHandling > NumberHandling
Definition: write_stack_frame.h:66
System::SharedPtr< System::Text::Json::Serialization::Metadata::JsonPropertyInfoBase > JsonPropertyInfo
Definition: write_stack_frame.h:54
System::Text::Json::PolymorphicSerializationState PolymorphicSerializationState
Definition: write_stack_frame.h:64
bool IsWritingExtensionDataProperty
Definition: write_stack_frame.h:55
int32_t OriginalDepth
Definition: write_stack_frame.h:57
System::SharedPtr< System::Text::Json::Serialization::Metadata::JsonTypeInfoBase > JsonTypeInfo
Definition: write_stack_frame.h:56
void ExitPolymorphicConverter(bool success)
Definition: write_stack.h:34
void DisposePendingDisposablesOnException()
bool get_IsContinuation()
System::SharedPtr< Serialization::Metadata::JsonTypeInfoBase > PeekNestedJsonTypeInfo()
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.
JsonNumberHandling
Definition: json_number_handling.h:16
JsonTokenType
Definition: json_token_type.h:12
@ PolymorphicReEntryStarted
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
std::enable_if_t< Details::CastType< Source, Result >::Static, Result > AsCast(const Source &value)
Casts the source type to the result type using 'as' operator cast. Used when simple constructor-like ...
Definition: object_ext.h:1219
static auto SafeInvoke(T0 &&expr, T1 &&func)
Implementation of '?.' operator translation.
Definition: object_ext.h:1434