Код: Выделить всё
public class ProductIdJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
if (value is ProductId id)
{
serializer.Serialize(writer, id.value);
}
else
{
writer.WriteNull();
}
}
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
var guid = serializer.Deserialize(reader);
return ProductId.Create(guid);
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ProductId);
}
}
Код: Выделить всё
[EfCoreValueConverter(typeof(ProductConverter.ProductIdValueConverter))]
[JsonConverter(typeof(ProductConverter.ProductIdJsonConverter))]
[TypeConverter(typeof(ProductConverter.ProductIdTypeConverter))]
public sealed class ProductId : ValueObjectId,IValueObjectId
{
public ProductId(Guid id) : base(id)
{
}
public static ProductId Create(Guid id)
{
return new ProductId(id);
}
public static ProductId CreateUnique()
{
return new(Guid.NewGuid());
}
}
Код: Выделить всё
public abstract class ValueObjectId : ValueObject
{
public Guid value { get; set; }
protected ValueObjectId(Guid id)
{
value = id;
}
public override IEnumerable GetEqualityComponents()
{
yield return value;
}
}
Код: Выделить всё
builder.Services.AddControllers()
.AddNewtonsoftJson(op =>
{
op.SerializerSettings.Converters.Add(new ProductConverter.ProductIdJsonConverter());
});
введите описание изображения здесь
Я пытался создать преобразователь customejson для идентификатора продукта в пользовательскую сериализацию для json api
но это не сработало
Подробнее здесь: https://stackoverflow.com/questions/786 ... ng-type-id
Мобильная версия