mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.3 KiB
37 lines
1.3 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Squidex.Infrastructure.Json
|
|
{
|
|
public sealed class NamedGuidIdConverter : JsonClassConverter<NamedId<Guid>>
|
|
{
|
|
protected override void WriteValue(JsonWriter writer, NamedId<Guid> value, JsonSerializer serializer)
|
|
{
|
|
writer.WriteValue($"{value.Id},{value.Name}");
|
|
}
|
|
|
|
protected override NamedId<Guid> ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer)
|
|
{
|
|
if (reader.TokenType != JsonToken.String)
|
|
{
|
|
throw new JsonException($"Expected String, but got {reader.TokenType}.");
|
|
}
|
|
|
|
try
|
|
{
|
|
return NamedId<Guid>.Parse(reader.Value.ToString(), Guid.TryParse);
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
throw new JsonException(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|