Class CachedValueJsonConverter
Custom JSON converter for CachedValue that handles polymorphic serialization of the Value property using the TypeName field.
public sealed class CachedValueJsonConverter : JsonConverter<CachedValue>
- Inheritance
-
CachedValueJsonConverter
- Inherited Members
- Extension Methods
Remarks
AOT / Trimming Behavior: This converter uses runtime type-name resolution and Deserialize(string, Type, JsonSerializerOptions) to reconstruct typed values from their serialized form. In AOT or trimmed environments, type resolution may fail if the target type is not preserved. When this occurs, the Value property falls back to a JsonElement representation, preserving the raw JSON data without throwing. Consumers should check for JsonElement as a fallback type when running in AOT scenarios.
Methods
Read(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads and converts the JSON to type CachedValue.
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with RequiresUnreferencedCodeAttribute may break with trimming", Justification = "Reconstructing a cached value by its stored type name is what this converter is for, so the requirement cannot be removed here. It is not hidden from the consumer: a converter runs only inside a JsonSerializer call, and those overloads carry the same annotation at the consumer's own call site.")]
[UnconditionalSuppressMessage("Aot", "IL3050:Calling members annotated with RequiresDynamicCodeAttribute may break functionality when AOT compiling.", Justification = "Serializing a cached value by its runtime type is what this converter is for, so the requirement cannot be removed here. It is not hidden from the consumer: a converter runs only inside a JsonSerializer call, and those overloads carry the same annotation at the consumer's own call site.")]
public override CachedValue? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe reader.
typeToConvertTypeThe type to convert.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.
Returns
- CachedValue
The converted value.
Write(Utf8JsonWriter, CachedValue, JsonSerializerOptions)
Writes a specified value as JSON.
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with RequiresUnreferencedCodeAttribute may break with trimming", Justification = "Reconstructing a cached value by its stored type name is what this converter is for, so the requirement cannot be removed here. It is not hidden from the consumer: a converter runs only inside a JsonSerializer call, and those overloads carry the same annotation at the consumer's own call site.")]
[UnconditionalSuppressMessage("Aot", "IL3050:Calling members annotated with RequiresDynamicCodeAttribute may break functionality when AOT compiling.", Justification = "Serializing a cached value by its runtime type is what this converter is for, so the requirement cannot be removed here. It is not hidden from the consumer: a converter runs only inside a JsonSerializer call, and those overloads carry the same annotation at the consumer's own call site.")]
public override void Write(Utf8JsonWriter writer, CachedValue value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe writer to write to.
valueCachedValueThe value to convert to JSON.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.