mirror of https://github.com/Squidex/squidex.git
3 changed files with 148 additions and 0 deletions
@ -0,0 +1,53 @@ |
|||
// ==========================================================================
|
|||
// NamedGuidIdConverted.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Linq; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Infrastructure.Json |
|||
{ |
|||
public sealed class NamedGuidIdConverted : JsonConverter |
|||
{ |
|||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
|||
{ |
|||
var namedId = (NamedId<Guid>)value; |
|||
|
|||
writer.WriteValue($"{namedId.Id},{namedId.Name}"); |
|||
} |
|||
|
|||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
|||
{ |
|||
if (reader.TokenType == JsonToken.Null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
var parts = ((string)reader.Value).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); |
|||
|
|||
if (parts.Length < 2) |
|||
{ |
|||
throw new JsonException("Named id must have more than 2 parts divided by commata"); |
|||
} |
|||
|
|||
Guid id; |
|||
|
|||
if (!Guid.TryParse(parts[0], out id)) |
|||
{ |
|||
throw new JsonException("Named id must be a valid guid"); |
|||
} |
|||
|
|||
return new NamedId<Guid>(id, string.Join(",", parts.Skip(1))); |
|||
} |
|||
|
|||
public override bool CanConvert(Type objectType) |
|||
{ |
|||
return objectType == typeof(NamedId<Guid>); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
// ==========================================================================
|
|||
// NamedStringIdConverted.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Linq; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Infrastructure.Json |
|||
{ |
|||
public sealed class NamedStringIdConverted : JsonConverter |
|||
{ |
|||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
|||
{ |
|||
var namedId = (NamedId<string>)value; |
|||
|
|||
writer.WriteValue($"{namedId.Id},{namedId.Name}"); |
|||
} |
|||
|
|||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
|||
{ |
|||
if (reader.TokenType == JsonToken.Null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
var parts = ((string)reader.Value).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); |
|||
|
|||
if (parts.Length < 2) |
|||
{ |
|||
throw new JsonException("Named id must have more than 2 parts divided by colon"); |
|||
} |
|||
|
|||
return new NamedId<string>(parts[0], string.Join(",", parts.Skip(1))); |
|||
} |
|||
|
|||
public override bool CanConvert(Type objectType) |
|||
{ |
|||
return objectType == typeof(NamedId<string>); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,49 @@ |
|||
// ==========================================================================
|
|||
// NamedId.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
|
|||
namespace Squidex.Infrastructure |
|||
{ |
|||
public sealed class NamedId<T> : IEquatable<NamedId<T>> |
|||
{ |
|||
public T Id { get; } |
|||
|
|||
public string Name { get; } |
|||
|
|||
public NamedId(T id, string name) |
|||
{ |
|||
Guard.NotNull(id, nameof(id)); |
|||
Guard.NotNull(name, nameof(name)); |
|||
|
|||
Id = id; |
|||
|
|||
Name = name; |
|||
} |
|||
|
|||
public override string ToString() |
|||
{ |
|||
return $"{Id},{Name}"; |
|||
} |
|||
|
|||
public override bool Equals(object obj) |
|||
{ |
|||
return Equals(obj as NamedId<T>); |
|||
} |
|||
|
|||
public bool Equals(NamedId<T> other) |
|||
{ |
|||
return other != null && (ReferenceEquals(this, other) || Id.Equals(other.Id))); |
|||
} |
|||
|
|||
public override int GetHashCode() |
|||
{ |
|||
return Id.GetHashCode(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue