JsonConverter
Converts an object or value to or from JSON.
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization.Metadata;
namespace System.Text.Json.Serialization
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public abstract class JsonConverter
{
internal bool IsInternalConverterForNumberType;
internal abstract ConverterStrategy ConverterStrategy { get; }
internal bool CanUseDirectReadOrWrite { get; set; }
internal virtual bool CanHaveIdMetadata => false;
internal bool CanBePolymorphic { get; set; }
[System.Runtime.CompilerServices.Nullable(2)]
internal abstract Type ElementType { get; }
[System.Runtime.CompilerServices.Nullable(2)]
internal abstract Type KeyType { get; }
internal bool IsValueType { get; set; }
internal bool IsInternalConverter { get; set; }
internal virtual Type RuntimeType => TypeToConvert;
internal abstract Type TypeToConvert { get; }
internal virtual bool ConstructorIsParameterized { get; }
[System.Runtime.CompilerServices.Nullable(2)]
internal ConstructorInfo ConstructorInfo { get; set; }
internal virtual bool RequiresDynamicMemberAccessors { get; }
internal JsonConverter()
{
}
public abstract bool CanConvert(Type typeToConvert);
internal virtual object CreateObject(JsonSerializerOptions options)
{
throw new InvalidOperationException(System.SR.NodeJsonObjectCustomConverterNotAllowedOnExtensionProperty);
}
internal virtual void ReadElementAndSetProperty(object obj, string propertyName, ref Utf8JsonReader reader, JsonSerializerOptions options, ref ReadStack state)
{
throw new InvalidOperationException(System.SR.NodeJsonObjectCustomConverterNotAllowedOnExtensionProperty);
}
internal abstract JsonPropertyInfo CreateJsonPropertyInfo();
internal abstract JsonParameterInfo CreateJsonParameterInfo();
internal abstract object ReadCoreAsObject(ref Utf8JsonReader reader, JsonSerializerOptions options, ref ReadStack state);
internal bool ShouldFlush(Utf8JsonWriter writer, ref WriteStack state)
{
if (state.FlushThreshold > 0)
return writer.BytesPending > state.FlushThreshold;
return false;
}
internal abstract bool TryReadAsObject(ref Utf8JsonReader reader, JsonSerializerOptions options, ref ReadStack state, out object value);
internal abstract bool TryWriteAsObject(Utf8JsonWriter writer, object value, JsonSerializerOptions options, ref WriteStack state);
internal abstract bool WriteCoreAsObject(Utf8JsonWriter writer, object value, JsonSerializerOptions options, ref WriteStack state);
internal abstract void WriteAsPropertyNameCoreAsObject(Utf8JsonWriter writer, object value, JsonSerializerOptions options, bool isWritingExtensionDataProperty);
internal virtual void Initialize(JsonSerializerOptions options, JsonTypeInfo jsonTypeInfo = null)
{
}
internal virtual void CreateInstanceForReferenceResolver(ref Utf8JsonReader reader, ref ReadStack state, JsonSerializerOptions options)
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool SingleValueReadWithReadAhead(ConverterStrategy converterStrategy, ref Utf8JsonReader reader, ref ReadStack state)
{
if (!state.ReadAhead || converterStrategy != ConverterStrategy.Value)
return reader.Read();
return DoSingleValueReadWithReadAhead(ref reader, ref state);
}
internal static bool DoSingleValueReadWithReadAhead(ref Utf8JsonReader reader, ref ReadStack state)
{
JsonReaderState currentState = reader.CurrentState;
long bytesConsumed = reader.BytesConsumed;
if (!reader.Read())
return false;
JsonTokenType tokenType = reader.TokenType;
if (tokenType == JsonTokenType.StartObject || tokenType == JsonTokenType.StartArray) {
bool flag = reader.TrySkip();
reader = new Utf8JsonReader(reader.OriginalSpan.Slice(checked((int)bytesConsumed)), reader.IsFinalBlock, currentState);
state.BytesConsumed += bytesConsumed;
if (!flag)
return false;
ref reader.ReadWithVerify();
}
return true;
}
}
}