Skip to content

Commit

Permalink
Make converter public.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed May 11, 2022
1 parent f1a8785 commit 6355af1
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class EnrichedEventsTests
[Fact]
public void Should_deserialize_EnrichedContentEvent()
{
var envelope = EnrichedEventEnvelope.DeserializeEnvelope(JsonEnrichedContentEvent);
var envelope = EnrichedEventEnvelope.FromJson(JsonEnrichedContentEvent);

Assert.True(envelope.Payload is EnrichedContentEvent);

Expand Down Expand Up @@ -94,7 +94,7 @@ public void Should_deserialize_EnrichedContentEvent()
[Fact]
public void Should_deserialize_EnrichedCommentEvent()
{
var envelope = EnrichedEventEnvelope.DeserializeEnvelope(JsonEnrichedCommentEvent);
var envelope = EnrichedEventEnvelope.FromJson(JsonEnrichedCommentEvent);

Assert.True(envelope.Payload is EnrichedCommentEvent);

Expand Down Expand Up @@ -135,7 +135,7 @@ public void Should_deserialize_EnrichedCommentEvent()
[Fact]
public void Should_deserialize_EnrichedAssetEvent()
{
var envelope = EnrichedEventEnvelope.DeserializeEnvelope(JsonEnrichedAssetEvent);
var envelope = EnrichedEventEnvelope.FromJson(JsonEnrichedAssetEvent);

Assert.True(envelope.Payload is EnrichedAssetEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// ==========================================================================

using Newtonsoft.Json;
using Squidex.ClientLibrary.Utils;

namespace Squidex.ClientLibrary.EnrichedEvents
{
Expand All @@ -31,13 +32,13 @@ public class EnrichedEventEnvelope

/// <summary>
/// Utils to deserialize an Envelope.
/// It uses TypeNameHandling = TypeNameHandling.Objects and SerializationBinder = new EnrichedEventSerializationBinder().
/// </summary>
/// <param name="json">The string to be deserialized.</param>
/// <param name="settings">Custom JsonSerializerSettings settings. TypeNameHandling and SerializationBinder will be overwritten.</param>
/// <returns>
/// The enriched event.
/// </returns>
[Obsolete("Use FromJson instead.")]
public static EnrichedEventEnvelope DeserializeEnvelope(string json, JsonSerializerSettings? settings = null)
{
if (settings == null)
Expand All @@ -46,8 +47,21 @@ public static EnrichedEventEnvelope DeserializeEnvelope(string json, JsonSeriali
}

settings.SerializationBinder = new EnrichedEventSerializationBinder();
settings.TypeNameHandling = TypeNameHandling.Auto;

return JsonConvert.DeserializeObject<EnrichedEventEnvelope>(json, settings)!;
}

/// <summary>
/// Utils to deserialize an Envelope.
/// </summary>
/// <param name="json">The string to be deserialized.</param>
/// <returns>
/// The enriched event.
/// </returns>
public static EnrichedEventEnvelope FromJson(string json)
{
return HttpClientExtensions.FromJsonWithTypes<EnrichedEventEnvelope>(json);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Squidex.ClientLibrary
{
/// <summary>
/// A custom JSON converter for invariant fields to convert nested invariant values to flat values.
/// A JSON converter to for invariant fields to convert nested invariant values to flat values.
/// </summary>
/// <seealso cref="JsonConverter" />
public sealed class InvariantConverter : JsonConverter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
namespace Squidex.ClientLibrary
{
/// <summary>
/// A custom JSON converter for invariant fields to convert nested invariant values to flat values
/// when serializing objects to JSON.
/// A JSON converter for invariant fields to convert nested invariant values to flat values when serializing objects to JSON.
/// </summary>
/// <seealso cref="JsonConverter" />
public sealed class InvariantWriteConverter : JsonConverter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageProjectUrl>https://github.com/Squidex/squidex/</PackageProjectUrl>
<PackageTags>Squidex HeadlessCMS</PackageTags>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<Version>8.19.0</Version>
<Version>8.20.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
namespace Squidex.ClientLibrary.Utils
{
/// <summary>
/// Convert actor string
/// Example of input: "subject:123456789".
/// A JSON converter for <see cref="Actor"/> instances.
/// </summary>
/// <remarks>
/// Example of input: "subject:123456789".
/// </remarks>
public class ActorConverter : JsonConverter<Actor>
{
/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,54 @@
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using Squidex.ClientLibrary.EnrichedEvents;

namespace Squidex.ClientLibrary.Utils
{
/// <summary>
/// Extension methods to deal with JSON.
/// </summary>
public static class HttpClientExtensions
{
private static readonly JsonSerializerSettings SerializerSettings;
private static readonly JsonSerializerSettings SerializerSettingsWithTypes;
private static readonly JsonSerializer Serializer;

static HttpClientExtensions()
{
SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new JsonNullContractResolver()
};

SerializerSettings.Converters.Add(new StringEnumConverter());
SerializerSettings.Converters.Add(new UTCIsoDateTimeConverter());
SerializerSettings = new JsonSerializerSettings().SetupSquidex();
SerializerSettingsWithTypes = new JsonSerializerSettings().SetupSquidex();
SerializerSettingsWithTypes.SerializationBinder = new EnrichedEventSerializationBinder();
SerializerSettingsWithTypes.TypeNameHandling = TypeNameHandling.Auto;

Serializer = JsonSerializer.CreateDefault(SerializerSettings);
}

/// <summary>
/// Converts the serializer settings to deal with squidex data.
/// </summary>
/// <param name="settings">The JSON settings.</param>
/// <returns>
/// The settings.
/// </returns>
public static JsonSerializerSettings SetupSquidex(this JsonSerializerSettings settings)
{
settings.ContractResolver = new JsonNullContractResolver();

settings.Converters.Add(new StringEnumConverter());
settings.Converters.Add(new UTCIsoDateTimeConverter());

return settings;
}

/// <summary>
/// Converts a value to a <see cref="HttpContent"/> instance which contains a JSON body.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="value">The value to convert.</param>
/// <returns>
/// The created <see cref="HttpContent"/> instance.
/// </returns>
public static HttpContent ToContent<T>(this T value)
{
var json = value.ToJson();
Expand All @@ -42,6 +66,15 @@ public static HttpContent ToContent<T>(this T value)
return content;
}

/// <summary>
/// Converts a stream to a <see cref="HttpContent"/> instance which contains a JSON body.
/// </summary>
/// <param name="stream">The stream to convert.</param>
/// <param name="name">The file name.</param>
/// <param name="mimeType">The mime type.</param>
/// <returns>
/// The created <see cref="HttpContent"/> instance.
/// </returns>
public static HttpContent ToContent(this Stream stream, string name, string mimeType)
{
var streamContent = new StreamContent(stream);
Expand All @@ -56,20 +89,59 @@ public static HttpContent ToContent(this Stream stream, string name, string mime
return requestContent;
}

/// <summary>
/// Converts a value to a JSON string.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="value">The value to convert.</param>
/// <returns>
/// The JSON string.
/// </returns>
public static string ToJson<T>(this T value)
{
var json = JsonConvert.SerializeObject(value, Formatting.Indented, SerializerSettings);

return json;
}

/// <summary>
/// Read a value from a JSON string.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="value">The JSON string.</param>
/// <returns>
/// The deserialized value.
/// </returns>
public static T FromJson<T>(this string value)
{
var json = JsonConvert.DeserializeObject<T>(value, SerializerSettings)!;

return json;
}

/// <summary>
/// Read a value from a JSON string and uses explicit type name handling.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="value">The JSON string.</param>
/// <returns>
/// The deserialized value.
/// </returns>
public static T FromJsonWithTypes<T>(this string value)
{
var json = JsonConvert.DeserializeObject<T>(value, SerializerSettingsWithTypes)!;

return json;
}

/// <summary>
/// Read a JSON value from a <see cref="HttpContent"/> instance.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="content">The content to read from.</param>
/// <returns>
/// The deserialized value.
/// </returns>
public static async Task<T?> ReadAsJsonAsync<T>(this HttpContent content)
{
#if NET5_0_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,18 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Reflection;

namespace Squidex.ClientLibrary.Utils
{
internal class JsonNullContractResolver : CamelCasePropertyNamesContractResolver
/// <summary>
/// a custom contract resolver that registeres the necessary converters.
/// </summary>
public class JsonNullContractResolver : CamelCasePropertyNamesContractResolver
{
/*
protected override JsonContract CreateContract(Type objectType)
{
var contract = base.CreateContract(objectType);
if (contract.UnderlyingType.IsGenericType && contract.UnderlyingType.GetGenericTypeDefinition() == typeof(JsonNull<>))
{
var converterType = typeof(JsonNullConverter<>).MakeGenericType(contract.UnderlyingType.GetGenericArguments()[0]);
var converterObj = Activator.CreateInstance(converterType) as JsonConverter;
contract.Converter = converterObj;
}
return contract;
}
*/

/// <inheritdoc/>
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
Expand Down Expand Up @@ -58,6 +45,7 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
return property;
}

/// <inheritdoc/>
protected override JsonConverter? ResolveContractConverter(Type objectType)
{
if (objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof(JsonNull<>))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Squidex.ClientLibrary.Utils
{
/// <summary>
/// Convert JsonNull to its actual value.
/// A JSON converter for <see cref="JsonNull{T}"/> instances.
/// </summary>
/// <typeparam name="T">The wrapped type.</typeparam>
public class JsonNullConverter<T> : JsonConverter<JsonNull<T>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

namespace Squidex.ClientLibrary.Utils
{
internal sealed class JsonNullInvariantConverter<T> : JsonConverter<JsonNull<T>>
/// <summary>
/// A JSON converter for <see cref="JsonNull{T}"/> instances and invariant fields.
/// </summary>
/// <typeparam name="T">The wrapped type.</typeparam>
public sealed class JsonNullInvariantConverter<T> : JsonConverter<JsonNull<T>>
{
/// <inheritdoc/>
public override void WriteJson(JsonWriter writer, JsonNull<T> value, JsonSerializer serializer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

namespace Squidex.ClientLibrary.Utils
{
internal sealed class JsonNullInvariantWriteConverter<T> : JsonNullConverter<T>
/// <summary>
/// A JSON converter for <see cref="JsonNull{T}"/> instances and invariant fiels (write only).
/// </summary>
/// <typeparam name="T">The wrapped type.</typeparam>
public sealed class JsonNullInvariantWriteConverter<T> : JsonNullConverter<T>
{
/// <inheritdoc/>
public override void WriteJson(JsonWriter writer, JsonNull<T> value, JsonSerializer serializer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Squidex.ClientLibrary.Utils
{
/// <summary>
/// Convert comma separated string to NamedId.
/// A JSON converter to for <see cref="NamedId"/> instances.
/// </summary>
/// <remarks>
/// Example of input: "00000000-0000-0000-0000-000000000000,name".
Expand Down

0 comments on commit 6355af1

Please sign in to comment.