Skip to content

Commit 8aa8a05

Browse files
v2.2.2 update
1 parent 0ac43a9 commit 8aa8a05

File tree

9 files changed

+148
-30
lines changed

9 files changed

+148
-30
lines changed

Open.Serialization.Json.Newtonsoft/JsonSerializerFactory.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ public JsonSerializerFactory(JsonSerializerSettings? defaultOptions = null)
1414
_settings = defaultOptions?.Clone() ?? DefaultOptions;
1515
}
1616

17-
JsonSerializerInternal? _default;
18-
JsonSerializerInternal Default => LazyInitializer.EnsureInitialized(ref _default, () => new JsonSerializerInternal(_settings))!;
17+
JsonSerializerInternal? _defaultSerializer;
18+
internal JsonSerializerInternal DefaultSerializer
19+
=> LazyInitializer.EnsureInitialized(ref _defaultSerializer, () => new JsonSerializerInternal(_settings))!;
20+
21+
static JsonSerializerFactory? _default;
22+
public static JsonSerializerFactory Default
23+
=> LazyInitializer.EnsureInitialized(ref _default)!;
1924

2025
#if NETSTANDARD2_1
2126
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull("options")]
@@ -59,16 +64,16 @@ public JsonSerializerFactory(JsonSerializerSettings? defaultOptions = null)
5964
return o;
6065
}
6166

62-
public IJsonSerializer GetSerializer(IJsonSerializationOptions? options = null, bool caseSensitive = false)
67+
internal JsonSerializerInternal GetSerializerInternal(IJsonSerializationOptions? options = null, bool caseSensitive = false)
6368
{
6469
var o = GetJsonSerializerSettings(options, caseSensitive);
65-
return o == null ? Default : new JsonSerializerInternal(o);
70+
return o == null ? DefaultSerializer : new JsonSerializerInternal(o);
6671
}
6772

68-
public IJsonObjectSerializer GetObjectSerializer(IJsonSerializationOptions? options, bool caseSensitive)
69-
{
70-
var o = GetJsonSerializerSettings(options, caseSensitive);
71-
return o == null ? Default : new JsonSerializerInternal(o);
72-
}
73+
public IJsonSerializer GetSerializer(IJsonSerializationOptions? options = null, bool caseSensitive = false)
74+
=> GetSerializerInternal(options, caseSensitive);
75+
76+
public IJsonObjectSerializer GetObjectSerializer(IJsonSerializationOptions? options, bool caseSensitive = false)
77+
=> GetSerializerInternal(options, caseSensitive);
7378
}
7479
}

Open.Serialization.Json.Newtonsoft/Open.Serialization.Json.Newtonsoft.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<RepositoryUrl>https://github.com/electricessence/Open.Serialization</RepositoryUrl>
1818
<RepositoryType>git</RepositoryType>
1919
<PackageTags>serialization json newtonsoft</PackageTags>
20-
<Version>2.2.1</Version>
20+
<Version>2.2.2</Version>
2121
<Nullable>enable</Nullable>
2222
</PropertyGroup>
2323

@@ -26,8 +26,9 @@
2626
<PrivateAssets>all</PrivateAssets>
2727
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2828
</PackageReference>
29+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
2930
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
30-
<PackageReference Include="Open.Serialization.Json" Version="2.2.1" />
31+
<PackageReference Include="Open.Serialization.Json" Version="2.2.2" />
3132
</ItemGroup>
3233

3334
</Project>

Open.Serialization.Json.Newtonsoft/SerializationExtensions.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Newtonsoft.Json;
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Newtonsoft.Json;
23
using Open.Serialization.Json.Newtonsoft.Converters;
34
using System;
45
using System.Collections.Generic;
@@ -9,6 +10,34 @@ namespace Open.Serialization.Json.Newtonsoft
910
{
1011
public static class SerializationExtensions
1112
{
13+
/// <summary>
14+
/// Adds a generic serializer (<code>IJsonSerializer</code>) and non-generic (<code>IJsonObjectSerializer</code>) to the service collection.
15+
/// </summary>
16+
/// <param name="services">The service collection.</param>
17+
/// <param name="options">The options overrides.</param>
18+
/// <returns>The service collection.</returns>
19+
public static IServiceCollection AddJsonSerializer(this IServiceCollection services, IJsonSerializationOptions? options = null)
20+
{
21+
var factory = JsonSerializerFactory.Default;
22+
var serializer = factory.GetSerializerInternal(options);
23+
services.AddSingleton<IJsonSerializer>(serializer);
24+
services.AddSingleton<IJsonObjectSerializer>(serializer);
25+
return services;
26+
}
27+
28+
/// <summary>
29+
/// Adds a generic serializer (<code>IJsonSerializer<typeparamref name="T"/></code>) to the service collection.
30+
/// </summary>
31+
/// <param name="services">The service collection.</param>
32+
/// <param name="options">The options overrides.</param>
33+
/// <returns>The service collection.</returns>
34+
public static IServiceCollection AddJsonSerializer<T>(this IServiceCollection services, IJsonSerializationOptions? options = null)
35+
{
36+
var factory = JsonSerializerFactory.Default;
37+
services.AddSingleton<IJsonSerializer<T>>(factory.GetSerializerInternal(options).Cast<T>());
38+
return services;
39+
}
40+
1241
public static Func<string?, T> GetDeserialize<T>(this JsonSerializerSettings settings)
1342
=> json => JsonConvert.DeserializeObject<T>(json, settings);
1443

Open.Serialization.Json.System/JsonSerializerFactory.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,26 @@ public JsonSerializerFactory(JsonSerializerOptions? defaultOptions = null)
1515
JsonSerializerInternal? _caseSensitive;
1616
JsonSerializerInternal? _ignoreCase;
1717

18-
public IJsonSerializer GetSerializer(IJsonSerializationOptions? options = null, bool caseSensitive = false)
18+
static JsonSerializerFactory? _default;
19+
public static JsonSerializerFactory Default
20+
=> LazyInitializer.EnsureInitialized(ref _default)!;
21+
22+
JsonSerializerOptions? GetJsonSerializerSettings(IJsonSerializationOptions? options = null)
1923
{
20-
if (options == null)
24+
if (options == null) return null;
25+
26+
var o = _options.Clone();
27+
o.IgnoreNullValues = options.OmitNull ?? o.IgnoreNullValues;
28+
o.WriteIndented = options.Indent ?? o.WriteIndented;
29+
o.DictionaryKeyPolicy = options.CamelCaseKeys == true ? JsonNamingPolicy.CamelCase : o.DictionaryKeyPolicy;
30+
o.PropertyNamingPolicy = options.CamelCaseProperties == true ? JsonNamingPolicy.CamelCase : o.PropertyNamingPolicy;
31+
return o;
32+
}
33+
34+
internal JsonSerializerInternal GetSerializerInternal(IJsonSerializationOptions? options = null, bool caseSensitive = false)
35+
{
36+
var o = GetJsonSerializerSettings(options);
37+
if (o == null)
2138
{
2239
#pragma warning disable CS8603 // Possible null reference return.
2340
return caseSensitive
@@ -28,13 +45,10 @@ public IJsonSerializer GetSerializer(IJsonSerializationOptions? options = null,
2845
#pragma warning restore CS8603 // Possible null reference return.
2946
}
3047

31-
var o = _options.Clone();
32-
o.IgnoreNullValues = options.OmitNull ?? o.IgnoreNullValues;
33-
o.WriteIndented = options.Indent ?? o.WriteIndented;
34-
o.DictionaryKeyPolicy = options.CamelCaseKeys == true ? JsonNamingPolicy.CamelCase : o.DictionaryKeyPolicy;
35-
o.PropertyNamingPolicy = options.CamelCaseProperties == true ? JsonNamingPolicy.CamelCase : o.PropertyNamingPolicy;
36-
3748
return new JsonSerializerInternal(o);
3849
}
50+
51+
public IJsonSerializer GetSerializer(IJsonSerializationOptions? options = null, bool caseSensitive = false)
52+
=> GetSerializerInternal(options, caseSensitive);
3953
}
4054
}

Open.Serialization.Json.System/Open.Serialization.Json.System.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Part of the "Open" set of libraries.
1717
<RepositoryUrl>https://github.com/electricessence/Open.Serialization</RepositoryUrl>
1818
<RepositoryType>git</RepositoryType>
1919
<PackageTags>serialization json stj</PackageTags>
20-
<Version>2.2.1</Version>
20+
<Version>2.2.2</Version>
2121
<Nullable>enable</Nullable>
2222
</PropertyGroup>
2323

@@ -26,7 +26,8 @@ Part of the "Open" set of libraries.
2626
<PrivateAssets>all</PrivateAssets>
2727
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2828
</PackageReference>
29-
<PackageReference Include="Open.Serialization.Json" Version="2.2.1" />
29+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
30+
<PackageReference Include="Open.Serialization.Json" Version="2.2.2" />
3031
<PackageReference Include="System.Text.Json" Version="4.7.0" />
3132
</ItemGroup>
3233

Open.Serialization.Json.System/SerializationsExtensions.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Open.Serialization.Json.System.Converters;
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Open.Serialization.Json.System.Converters;
23
using System;
34
using System.Diagnostics.Contracts;
45
using System.Linq;
@@ -10,6 +11,32 @@ namespace Open.Serialization.Json.System
1011
{
1112
public static class SerializationsExtensions
1213
{
14+
/// <summary>
15+
/// Adds a generic serializer (<code>IJsonSerializer</code>) to the service collection.
16+
/// </summary>
17+
/// <param name="services">The service collection.</param>
18+
/// <param name="options">The options overrides.</param>
19+
/// <returns>The service collection.</returns>
20+
public static IServiceCollection AddJsonSerializer(this IServiceCollection services, IJsonSerializationOptions? options = null, bool caseSensitive = false)
21+
{
22+
var factory = JsonSerializerFactory.Default;
23+
services.AddSingleton(factory.GetSerializer(options, caseSensitive));
24+
return services;
25+
}
26+
27+
/// <summary>
28+
/// Adds a generic serializer (<code>IJsonSerializer<typeparamref name="T"/></code>) to the service collection.
29+
/// </summary>
30+
/// <param name="services">The service collection.</param>
31+
/// <param name="options">The options overrides.</param>
32+
/// <returns>The service collection.</returns>
33+
public static IServiceCollection AddJsonSerializer<T>(this IServiceCollection services, IJsonSerializationOptions? options = null, bool caseSensitive = false)
34+
{
35+
var factory = JsonSerializerFactory.Default;
36+
services.AddSingleton<IJsonSerializer<T>>(factory.GetSerializerInternal(options, caseSensitive).Cast<T>());
37+
return services;
38+
}
39+
1340
public static Func<string?, T> GetDeserialize<T>(this JsonSerializerOptions options)
1441
=> json => JsonSerializer.Deserialize<T>(json, options);
1542

Open.Serialization.Json.Utf8Json/JsonSerializerFactory.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ public JsonSerializerFactory(IJsonFormatterResolver? defaultResolver = null, boo
1717
_indent = indent;
1818
}
1919

20-
JsonSerializerInternal? _default;
21-
JsonSerializerInternal Default => LazyInitializer.EnsureInitialized(ref _default, () => new JsonSerializerInternal(_resolver, _indent))!;
20+
JsonSerializerInternal? _defaultSerializer;
21+
JsonSerializerInternal DefaultSerializer
22+
=> LazyInitializer.EnsureInitialized(ref _defaultSerializer, () => new JsonSerializerInternal(_resolver, _indent))!;
2223

23-
public IJsonSerializer GetSerializer(IJsonSerializationOptions? options = null, bool caseSensitive = false)
24+
static JsonSerializerFactory? _default;
25+
public static JsonSerializerFactory Default
26+
=> LazyInitializer.EnsureInitialized(ref _default)!;
27+
28+
internal JsonSerializerInternal GetSerializerInternal(IJsonSerializationOptions? options = null, bool caseSensitive = false)
2429
{
2530
if (caseSensitive)
2631
throw new NotSupportedException("Utf8Json does not support case-sensitive deserialization.");
2732

2833
if (options == null)
29-
return Default;
34+
return DefaultSerializer;
3035

3136
if (options.CamelCaseKeys == true)
3237
throw new NotSupportedException("Utf8Json does not support camel casing keys.");
@@ -38,5 +43,11 @@ public IJsonSerializer GetSerializer(IJsonSerializationOptions? options = null,
3843
? new JsonSerializerInternal(omitNull ? StandardResolver.ExcludeNullCamelCase : StandardResolver.CamelCase, options.Indent ?? _indent)
3944
: new JsonSerializerInternal(omitNull ? StandardResolver.ExcludeNull : StandardResolver.Default, options.Indent ?? _indent);
4045
}
46+
47+
public IJsonSerializer GetSerializer(IJsonSerializationOptions? options = null, bool caseSensitive = false)
48+
=> GetSerializerInternal(options, caseSensitive);
49+
50+
public IJsonObjectSerializer GetObjectSerializer(IJsonSerializationOptions? options, bool caseSensitive = false)
51+
=> GetSerializerInternal(options, caseSensitive);
4152
}
4253
}

Open.Serialization.Json.Utf8Json/Open.Serialization.Json.Utf8Json.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Part of the "Open" set of libraries.
1717
<RepositoryUrl>https://github.com/electricessence/Open.Serialization</RepositoryUrl>
1818
<RepositoryType>git</RepositoryType>
1919
<PackageTags>serialization json utf8json</PackageTags>
20-
<Version>2.2.1</Version>
20+
<Version>2.2.2</Version>
2121
<Nullable>enable</Nullable>
2222
</PropertyGroup>
2323

@@ -26,7 +26,8 @@ Part of the "Open" set of libraries.
2626
<PrivateAssets>all</PrivateAssets>
2727
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2828
</PackageReference>
29-
<PackageReference Include="Open.Serialization.Json" Version="2.2.1" />
29+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
30+
<PackageReference Include="Open.Serialization.Json" Version="2.2.2" />
3031
<PackageReference Include="Utf8Json" Version="1.3.7" />
3132
</ItemGroup>
3233

Open.Serialization.Json.Utf8Json/SerializationExtensions.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
1-
using System;
1+
using Microsoft.Extensions.DependencyInjection;
2+
using System;
23
using Utf8Json;
34

45
namespace Open.Serialization.Json.Utf8Json
56
{
67
public static class SerializationExtensions
78
{
9+
/// <summary>
10+
/// Adds a generic serializer (<code>IJsonSerializer</code>) and non-generic (<code>IJsonObjectSerializer</code>) to the service collection.
11+
/// </summary>
12+
/// <param name="services">The service collection.</param>
13+
/// <param name="options">The options overrides.</param>
14+
/// <returns>The service collection.</returns>
15+
public static IServiceCollection AddJsonSerializer(this IServiceCollection services, IJsonSerializationOptions? options = null)
16+
{
17+
var factory = JsonSerializerFactory.Default;
18+
var serializer = factory.GetSerializerInternal(options);
19+
services.AddSingleton<IJsonSerializer>(serializer);
20+
services.AddSingleton<IJsonObjectSerializer>(serializer);
21+
return services;
22+
}
23+
24+
/// <summary>
25+
/// Adds a generic serializer (<code>IJsonSerializer<typeparamref name="T"/></code>) to the service collection.
26+
/// </summary>
27+
/// <param name="services">The service collection.</param>
28+
/// <param name="options">The options overrides.</param>
29+
/// <returns>The service collection.</returns>
30+
public static IServiceCollection AddJsonSerializer<T>(this IServiceCollection services, IJsonSerializationOptions? options = null)
31+
{
32+
var factory = JsonSerializerFactory.Default;
33+
services.AddSingleton<IJsonSerializer<T>>(factory.GetSerializerInternal(options).Cast<T>());
34+
return services;
35+
}
36+
837
public static Func<string?, T> GetDeserialize<T>(this IJsonFormatterResolver options)
938
=> json => JsonSerializer.Deserialize<T>(json, options);
1039

0 commit comments

Comments
 (0)