public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) { writer.WriteNull(); return; } var objectType = value.GetType(); if (TypesDiscriminatorValues.FallbackType != null && objectType == TypesDiscriminatorValues.FallbackType) { _isInWrite.Value = true; try { serializer.Serialize(writer, value, objectType); } finally { _isInWrite.Value = false; } return; } if (TypesDiscriminatorValues.TryGetDiscriminator(objectType, out var discriminator)) { _isInWrite.Value = true; var writerProxy = new JsonKnownProxyWriter(TypesDiscriminatorValues.FieldName, discriminator, writer); try { serializer.Serialize(writerProxy, value, objectType); } finally { _isInWrite.Value = false; } } else { throw new JsonKnownTypesException($"There is no discriminator for {objectType.Name} type"); } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value == null) { writer.WriteNull(); return; } var objectType = value.GetType(); if (_typesDiscriminatorValues.FallbackType != null && objectType == _typesDiscriminatorValues.FallbackType) { _isInWrite.Value = true; try { serializer.Serialize(writer, value, objectType); return; } finally { _isInWrite.Value = false; } } if (TryGetDiscriminator(objectType, _typesDiscriminatorValues.UseBaseTypeDescriminators, out var discriminator)) { try { _isInWrite.Value = true; var writerProxy = new JsonKnownProxyWriter(_typesDiscriminatorValues.FieldName, discriminator, writer); serializer.Serialize(writerProxy, value, objectType); } finally { _isInWrite.Value = false; } } else { throw new JsonKnownTypesException($"There is no discriminator for {objectType.Name} type. If {objectType.Name} is derived from a type that provides a discriminator value then you can try adding 'UseBaseTypeDescriminators = true' to the 'JsonDiscriminator' attribute for the type '{typeof(T).Name}'"); } }