protected internal override Task <string> SerializeWrapper <T>(T value, JsonSerializerOptions?options = null) { if (options != null) { return(Task.FromResult(Serialize(value, options))); } JsonTypeInfo <T> typeInfo = (JsonTypeInfo <T>)_defaultContext.GetTypeInfo(typeof(T)); return(Task.FromResult(JsonSerializer.Serialize(value, typeInfo))); }
private T Deserialize <T>(string json, JsonSerializerOptions options) { JsonSerializerContext context = _customContextCreator(new JsonSerializerOptions(options)); JsonTypeInfo <T> typeInfo = (JsonTypeInfo <T>)context.GetTypeInfo(typeof(T)); return(JsonSerializer.Deserialize <T>(json, typeInfo)); }
private string Serialize <T>(T value, JsonSerializerOptions options) { JsonSerializerContext context = _customContextCreator(new JsonSerializerOptions(options)); JsonTypeInfo <T> typeInfo = (JsonTypeInfo <T>)context.GetTypeInfo(typeof(T)); return(JsonSerializer.Serialize(value, typeInfo)); }
public override Task <T> DeserializeWrapper <T>(string json, JsonSerializerOptions?options = null) { JsonSerializerContext context = GetJsonSerializerContext(options); JsonTypeInfo <T> typeInfo = (JsonTypeInfo <T>)context.GetTypeInfo(typeof(T)); return(Task.FromResult(JsonSerializer.Deserialize <T>(json, typeInfo))); }
private static JsonTypeInfo GetTypeInfo(JsonSerializerContext context, Type type) { Debug.Assert(context != null); Debug.Assert(type != null); JsonTypeInfo?info = context.GetTypeInfo(type); if (info is null) { ThrowHelper.ThrowInvalidOperationException_NoMetadataForType(type); } return(info); }
public override Task <string> SerializeWrapper <T>(T value, JsonSerializerOptions?options = null) { Type runtimeType = GetRuntimeType(value); if (runtimeType != typeof(T)) { return(SerializeWrapper(value, runtimeType, options)); } JsonSerializerContext context = GetJsonSerializerContext(options); JsonTypeInfo <T> typeInfo = (JsonTypeInfo <T>)context.GetTypeInfo(typeof(T)); return(Task.FromResult(JsonSerializer.Serialize(value, typeInfo))); }
private static JsonTypeInfo GetTypeInfo(JsonSerializerContext context, Type inputType) { Debug.Assert(context != null); Debug.Assert(inputType != null); JsonTypeInfo?info = context.GetTypeInfo(inputType); if (info is null) { ThrowHelper.ThrowInvalidOperationException_NoMetadataForType(inputType, context); } info.EnsureConfigured(); return(info); }
/// <summary> /// Gets the <see cref="JsonTypeInfo{T}" /> for <typeparamref name="T"/> from the <paramref name="context"/>. /// </summary> /// <typeparam name="T">Type to lookup.</typeparam> /// <param name="context">The <see cref="JsonSerializerContext"/>.</param> /// <param name="typeInfo">The <see cref="JsonTypeInfo{T}"/> from the context.</param> /// <returns>True if the <see cref="JsonTypeInfo{T}"/> was found.</returns> public static bool TryGetTypeInfo <T>(this JsonSerializerContext context, [MaybeNullWhen(false)] out JsonTypeInfo <T> typeInfo) { if (context == null) { ThrowHelper.ThrowArgumentNullException(nameof(context)); } if (context.GetTypeInfo(typeof(T)) is JsonTypeInfo <T> newTypeInfo) { typeInfo = newTypeInfo; return(true); } typeInfo = null; return(false); }
/// <inheritdoc /> public string Serialize <TSerializableType>(TSerializableType serializable) { var typeInfo = (JsonTypeInfo <TSerializableType>)(serializerContext.GetTypeInfo(typeof(TSerializableType)) ?? throw new Exception($"Could not find type info for type {typeof(TSerializableType).Name}.")); return(SystemTextJsonSerializer.Serialize(serializable, typeInfo)); }
public override T?ToObject <T>() where T : default { var typeInfo = _context.GetTypeInfo <T>(); return(RootNode.Deserialize <T>(typeInfo)); }