From c0fb09be139956b1df4ff88eb2d3480a06057f6f Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 11 Feb 2017 13:20:22 +0100 Subject: [PATCH] Named ID --- .../Json/NamedGuidIdConverted.cs | 53 +++++++++++++++++++ .../Json/NamedStringIdConverted.cs | 46 ++++++++++++++++ src/Squidex.Infrastructure/NamedId.cs | 49 +++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 src/Squidex.Infrastructure/Json/NamedGuidIdConverted.cs create mode 100644 src/Squidex.Infrastructure/Json/NamedStringIdConverted.cs create mode 100644 src/Squidex.Infrastructure/NamedId.cs diff --git a/src/Squidex.Infrastructure/Json/NamedGuidIdConverted.cs b/src/Squidex.Infrastructure/Json/NamedGuidIdConverted.cs new file mode 100644 index 000000000..0b8cfb425 --- /dev/null +++ b/src/Squidex.Infrastructure/Json/NamedGuidIdConverted.cs @@ -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)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(id, string.Join(",", parts.Skip(1))); + } + + public override bool CanConvert(Type objectType) + { + return objectType == typeof(NamedId); + } + } +} diff --git a/src/Squidex.Infrastructure/Json/NamedStringIdConverted.cs b/src/Squidex.Infrastructure/Json/NamedStringIdConverted.cs new file mode 100644 index 000000000..da7f3599e --- /dev/null +++ b/src/Squidex.Infrastructure/Json/NamedStringIdConverted.cs @@ -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)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(parts[0], string.Join(",", parts.Skip(1))); + } + + public override bool CanConvert(Type objectType) + { + return objectType == typeof(NamedId); + } + } +} diff --git a/src/Squidex.Infrastructure/NamedId.cs b/src/Squidex.Infrastructure/NamedId.cs new file mode 100644 index 000000000..8849f8a99 --- /dev/null +++ b/src/Squidex.Infrastructure/NamedId.cs @@ -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 : IEquatable> + { + 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); + } + + public bool Equals(NamedId other) + { + return other != null && (ReferenceEquals(this, other) || Id.Equals(other.Id))); + } + + public override int GetHashCode() + { + return Id.GetHashCode(); + } + } +}