3#include <system/value_tuple.h>
4#include <system/type_info.h>
5#include <system/span.h>
6#include <system/primitive_types.h>
7#include <system/object_ext.h>
8#include <system/nullable.h>
9#include <system/math.h>
10#include <system/diagnostics/debug.h>
11#include <system/details/pointer_collection_helpers.h>
12#include <system/details/patterns.h>
13#include <system/default.h>
14#include <system/constraints.h>
15#include <system/collections/idictionary.h>
16#include <system/collections/collection_extensions.h>
17#include <system/array.h>
18#include <system/action.h>
21#include "system/text/json/json_collection_extensions.h"
22#include "system/text/json/serialization/converters/object/object_default_converter.h"
23#include "system/text/json/reader/utf8_json_reader.h"
24#include "system/text/json/serialization/json_serializer_options.h"
25#include "system/text/json/serialization/read_stack.h"
26#include "system/text/json/serialization/read_stack_frame.h"
27#include "system/text/json/serialization/argument_state.h"
28#include "system/text/json/json_token_type.h"
29#include "system/text/json/throw_helper.h"
30#include "system/text/json/reader/json_reader_state.h"
31#include "system/text/json/serialization/json_serializer_helpers.h"
32#include "system/text/json/serialization/stack_frame_object_state.h"
33#include "system/text/json/serialization/metadata_property_name.h"
34#include "system/text/json/serialization/polymorphic_serialization_state.h"
35#include "system/text/json/serialization/json_converter.h"
36#include "system/text/json/document/json_element.h"
37#include "system/text/json/serialization/metadata/json_type_info_kind.h"
38#include "system/text/json/json_helpers.h"
39#include "system/text/json/serialization/stack_frame_property_state.h"
68 typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
69 RTTI_INFO_TEMPLATE_CLASS(
ThisType, ThisTypeBaseTypesInfo);
108 value = System::ExplicitCast<T>(populatedObject);
124 jsonTypeInfo->InvokeOnDeserializing(obj);
126 if (argumentState->FoundPropertyCount > 0)
131 for (int32_t i = 0; i < argumentState->FoundPropertyCount; i++)
134 int64_t resumptionByteIndex = properties[i].template Item<2>();
136 String dataExtKey = properties[i].template Item<4>();
138 tempReader = originalSequence.
get_IsEmpty() ?
Utf8JsonReader(originalSpan.Slice(System::CheckedCast<int32_t>(resumptionByteIndex)),
true, properties[i].template Item<1>()) :
Utf8JsonReader(originalSequence.
Slice(resumptionByteIndex),
true, properties[i].template Item<1>());
144 bool useExtensionProperty = !(dataExtKey ==
nullptr);
146 if (useExtensionProperty)
156 argumentState->FoundProperties.
reset();
179 value = System::Default<T>();
191 bool success = polymorphicConverter->OnTryReadAsObject(
System::Ref(reader), polymorphicConverter->get_Type(), options,
System::Ref(state), objectResult);
192 value = System::ExplicitCast<T>(objectResult);
205 jsonTypeInfo->InvokeOnDeserializing(populatedObject);
226 value = System::Default<T>();
237 jsonTypeInfo->InvokeOnDeserializing(obj);
239 if (argumentState->FoundPropertyCount > 0)
241 for (int32_t i = 0; i < argumentState->FoundPropertyCount; i++)
245 String dataExtKey = argumentState->FoundPropertiesAsync[i].template Item<2>();
247 if (dataExtKey ==
nullptr)
249 if (!(propValue ==
nullptr) || !jsonPropertyInfo->get_IgnoreNullTokensOnRead() || !System::IsNull(System::Default<T>()))
251 jsonPropertyInfo->get_Set()(obj, propValue);
262 dict->idx_set(dataExtKey, System::ExplicitCast<System::Text::Json::JsonElement>(propValue));
268 objDict->idx_set(dataExtKey, propValue);
274 argumentState->FoundPropertiesAsync.
reset();
279 jsonTypeInfo->InvokeOnDeserialized(obj);
282 value = System::ExplicitCast<T>(obj);
311 bool useExtensionProperty;
320 jsonParameterInfo = jsonPropertyInfo->get_AssociatedParameter();
321 if (!(jsonParameterInfo ==
nullptr))
358 bool isAlreadyReadMetadataProperty;
360 if (isAlreadyReadMetadataProperty)
372 if (!jsonParameterInfo->get_ShouldDeserialize())
380 bool success = reader.
TrySkip();
381 CODEPORTING_DEBUG_ASSERT2(success, u
"Serializer should guarantee sufficient read-ahead has been done.");
392 if (jsonPropertyInfo->get_CanDeserialize())
396 if (argumentState->FoundProperties ==
nullptr)
400 else if (argumentState->FoundPropertyCount == argumentState->FoundProperties->get_Length())
405 auto newCache = Buffers::ArrayPool<ValueTuple<System::SharedPtr<Metadata::JsonPropertyInfoBase>, JsonReaderState, int64_t,
System::SharedPtr<Array<uint8_t>>, String>>::get_Shared()->Rent(argumentState->FoundProperties->get_Length() * 2);
407 argumentState->FoundProperties->CopyTo(newCache, 0);
410 argumentState->FoundProperties = newCache;
412 Buffers::ArrayPool<ValueTuple<System::SharedPtr<Metadata::JsonPropertyInfoBase>, JsonReaderState, int64_t,
System::SharedPtr<Array<uint8_t>>, String>>::get_Shared()->Return(toReturn,
true);
415 argumentState->FoundProperties[argumentState->FoundPropertyCount++] =
System::MakeTuple(jsonPropertyInfo,
458 bool isAlreadyReadMetadataProperty;
460 if (isAlreadyReadMetadataProperty)
463 state.Current.EndProperty();
469 jsonPropertyInfo.
reset();
476 jsonParameterInfo = state.Current.CtorArgumentState->JsonParameterInfo;
477 jsonPropertyInfo = state.Current.JsonPropertyInfo;
480 if (!(jsonParameterInfo ==
nullptr))
482 if (!HandleConstructorArgumentWithContinuation(
System::Ref(state),
System::Ref(reader), jsonParameterInfo))
501 if (!jsonParameterInfo->get_ShouldDeserialize())
503 if (!reader.TrySkipPartial(state.Current.OriginalDepth + 1))
508 state.Current.EndConstructorParameter();
525 state.Current.EndConstructorParameter();
533 if (!jsonPropertyInfo->get_CanDeserialize())
535 if (!reader.TrySkipPartial(state.Current.OriginalDepth + 1))
540 state.Current.EndProperty();
554 if (state.Current.UseExtensionProperty)
573 if (argumentState->FoundPropertiesAsync ==
nullptr)
575 argumentState->FoundPropertiesAsync = Buffers::ArrayPool<ValueTuple<System::SharedPtr<Metadata::JsonPropertyInfoBase>,
System::SharedPtr<Object>, String>>::get_Shared()->Rent(
System::Math::Max(1, state.Current.JsonTypeInfo->get_PropertyCache().get_Length()));
577 else if (argumentState->FoundPropertyCount == argumentState->FoundPropertiesAsync->get_Length())
581 auto newCache = Buffers::ArrayPool<ValueTuple<System::SharedPtr<Metadata::JsonPropertyInfoBase>,
System::SharedPtr<Object>, String>>::get_Shared()->Rent(argumentState->FoundPropertiesAsync->get_Length() * 2);
583 argumentState->FoundPropertiesAsync->CopyTo(newCache, 0);
586 argumentState->FoundPropertiesAsync = newCache;
588 Buffers::ArrayPool<ValueTuple<System::SharedPtr<Metadata::JsonPropertyInfoBase>,
System::SharedPtr<Object>, String>>::get_Shared()->Return(toReturn,
true);
592 argumentState->FoundPropertiesAsync[argumentState->FoundPropertyCount++] =
System::MakeTuple(jsonPropertyInfo,
594 state.Current.JsonPropertyNameAsString);
596 state.Current.EndProperty();
604 jsonTypeInfo->ValidateCanBeUsedForPropertyMetadataSerialization();
606 if (jsonTypeInfo->get_ParameterCount() != jsonTypeInfo->get_ParameterCache().get_Length())
611 state.Current.InitializePropertiesValidationState(jsonTypeInfo);
614 state.Current.JsonPropertyInfo.reset();
Provides a thread-safe pool for reusing managed array instances (ArrayPtr<T>).
Definition: array_pool.h:21
bool get_IsEmpty() const
Determines whether the sequence is empty.
Definition: read_only_sequence.h:145
ReadOnlySequence Slice(int64_t start) const
Creates a slice of the sequence starting at the given offset.
Definition: read_only_sequence.h:288
Interface for dictionary-alike containers. Objects of this class should only be allocated using Syste...
Definition: idictionary.h:21
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Pointee_ * get() const
Gets pointed object.
Definition: smart_ptr.h:508
void reset(Pointee_ *ptr)
Sets pointed object.
Definition: smart_ptr.h:520
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 void SkipWithVerify(Utf8JsonReader &reader)
Definition: json_reader_state.h:31
Definition: read_stack_frame.h:42
System::Text::Json::PolymorphicSerializationState PolymorphicSerializationState
Definition: read_stack_frame.h:65
bool IsPopulating
Definition: read_stack_frame.h:76
String JsonPropertyNameAsString
Definition: read_stack_frame.h:55
void InitializePropertiesValidationState(System::SharedPtr< System::Text::Json::Serialization::Metadata::JsonTypeInfoBase > typeInfo)
bool CanContainMetadata
Definition: read_stack_frame.h:62
System::SharedPtr< Array< uint8_t > > JsonPropertyName
Definition: read_stack_frame.h:54
Nullable< Serialization::JsonNumberHandling > NumberHandling
Definition: read_stack_frame.h:73
void MarkPropertyAsRead(System::Text::Json::Serialization::Metadata::JsonPropertyInfoBase *propertyInfo)
bool UseExtensionProperty
Definition: read_stack_frame.h:53
void EndConstructorParameter()
System::SharedPtr< Object > ReturnValue
Definition: read_stack_frame.h:59
MetadataPropertyName MetadataPropertyNames
Definition: read_stack_frame.h:64
System::SharedPtr< System::Text::Json::Serialization::Metadata::PropertyRefCacheBuilder > PropertyRefCacheBuilder
Definition: read_stack_frame.h:71
System::SharedPtr< ArgumentState > CtorArgumentState
Definition: read_stack_frame.h:72
StackFrameObjectState ObjectState
Definition: read_stack_frame.h:61
void ValidateAllRequiredPropertiesAreRead(System::SharedPtr< System::Text::Json::Serialization::Metadata::JsonTypeInfoBase > typeInfo)
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: object_default_converter.h:64
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: object_default_converter.h:484
static void PopulatePropertiesFastPath(System::SharedPtr< Object > obj, System::SharedPtr< Metadata::JsonTypeInfoBase > jsonTypeInfo, System::SharedPtr< JsonSerializerOptions > options, Utf8JsonReader &reader, ReadStack &state)
Definition: object_default_converter.h:304
static void ReadPropertyValue(System::SharedPtr< Object > obj, ReadStack &state, Utf8JsonReader &reader, System::SharedPtr< Metadata::JsonPropertyInfoBase > jsonPropertyInfo, bool useExtensionProperty)
Definition: object_default_converter.h:497
static bool ReadAheadPropertyValue(ReadStack &state, Utf8JsonReader &reader, System::SharedPtr< Metadata::JsonPropertyInfoBase > jsonPropertyInfo)
Definition: object_default_converter.h:530
bool OnTryRead(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options, ReadStack &state, T &value) override
Definition: object_default_converter.h:85
Definition: object_with_parameterized_constructor_converter.h:64
void SetTemplateWeakPtr(uint32_t argument) override
Set n'th template argument a weak pointer (rather than shared). Allows switching pointers in containe...
Definition: object_with_parameterized_constructor_converter.h:293
virtual void InitializeConstructorArgumentCaches(ReadStack &state, System::SharedPtr< JsonSerializerOptions > options)=0
bool OnTryRead(Utf8JsonReader &reader, const System::TypeInfo &typeToConvert, System::SharedPtr< JsonSerializerOptions > options, ReadStack &state, T &value) override
Definition: object_with_parameterized_constructor_converter.h:80
bool get_ConstructorIsParameterized() override
Definition: object_with_parameterized_constructor_converter.h:75
virtual ~ObjectWithParameterizedConstructorConverter()
Definition: object_with_parameterized_constructor_converter.h:335
virtual System::SharedPtr< Object > CreateObject(ReadStackFrame &frame)=0
virtual bool ReadAndCacheConstructorArgument(ReadStack &state, Utf8JsonReader &reader, System::SharedPtr< Metadata::JsonParameterInfoBase > jsonParameterInfo)=0
static bool TryLookupConstructorParameter(ReadOnlySpan< uint8_t > unescapedPropertyName, ReadStack &state, System::SharedPtr< JsonSerializerOptions > options, System::SharedPtr< Metadata::JsonPropertyInfoBase > &jsonPropertyInfo, System::SharedPtr< Metadata::JsonParameterInfoBase > &jsonParameterInfo)
Definition: object_with_parameterized_constructor_converter.h:309
Definition: json_converter_of_t.h:80
System::TypeInfo get_Type() override
Definition: json_converter_of_t.h:115
Definition: json_object_converter.h:19
Definition: json_resumable_converter_of_t.h:42
static System::SharedPtr< JsonPropertyInfoBase > s_missingProperty
Definition: json_property_info.h:140
static void ThrowJsonException_DeserializeUnableToConvertValue(const TypeInfo &propertyType)
static void ThrowInvalidOperationException_ConstructorParameterIncompleteBinding(const TypeInfo &parentType)
Definition: utf8_json_reader.h:78
int64_t get_BytesConsumed()
ReadOnlySpan< uint8_t > get_OriginalSpan()
JsonReaderState get_CurrentState()
Buffers::ReadOnlySequence< uint8_t > get_OriginalSequence()
JsonTokenType get_TokenType()
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)
ReadOnlySpan< uint8_t > GetPropertyName(ReadStack &state, Utf8JsonReader &reader, System::SharedPtr< JsonSerializerOptions > options, bool &isAlreadyReadMetadataProperty)
void CreateExtensionDataProperty(System::SharedPtr< Object > obj, System::SharedPtr< Metadata::JsonPropertyInfoBase > jsonPropertyInfo, System::SharedPtr< JsonSerializerOptions > options)
System::SharedPtr< Metadata::JsonPropertyInfoBase > LookupProperty(System::SharedPtr< Object > obj, ReadOnlySpan< uint8_t > unescapedPropertyName, ReadStack &state, System::SharedPtr< JsonSerializerOptions > options, bool &useExtensionProperty, bool createExtensionProperty=true)
bool TryReadMetadata(System::SharedPtr< JsonConverterBase > converter, System::SharedPtr< Metadata::JsonTypeInfoBase > jsonTypeInfo, Utf8JsonReader &reader, ReadStack &state)
JsonTokenType
Definition: json_token_type.h:12
@ PolymorphicReEntryStarted
MetadataPropertyName
Definition: metadata_property_name.h:13
Definition: db_command.h:9
ValueTuple< Args... > MakeTuple(Args... args)
Creates tuple on stack.
Definition: value_tuple.h:300
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
static auto Max(T0 val1, T1 val2) -> decltype(val1+val2)
Returns the greatest value out of two numeric ones specified.
Definition: math.h:327