Skip to content

Commit

Permalink
Fixes #113 have writer use settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekctek committed Feb 10, 2023
1 parent 88686ff commit 3e324e3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ private static bool TryGetObjectFromString(Context context, out object? destinat
private static bool TryGetObjectFromObject(Context context, out object? destinationValue, out Exception? exception)
{
Dictionary<string, RpcParameter> obj = context.SourceValue.GetObjectValue();
string json = JsonStringGeneratorUtil.FromObject(obj);
string json = JsonStringGeneratorUtil.FromObject(obj, context.SerializerOptions);
return TryDeserializeJson(json, context, out destinationValue, out exception);
}

private static bool TryGetArrayFromArray(Context context, out object? destinationValue, out Exception? exception)
{
RpcParameter[] array = context.SourceValue.GetArrayValue();
string json = JsonStringGeneratorUtil.FromArray(array);
string json = JsonStringGeneratorUtil.FromArray(array, context.SerializerOptions);
return TryDeserializeJson(json, context, out destinationValue, out exception);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EdjCase.JsonRpc.Common;
using EdjCase.JsonRpc.Common;
using EdjCase.JsonRpc.Router.Abstractions;
using EdjCase.JsonRpc.Router.Utilities;
using Microsoft.Extensions.Options;
using System;
using System.Buffers.Text;
Expand Down Expand Up @@ -32,7 +33,8 @@ public Task SerializeAsync(RpcResponse response, Stream stream)

private async Task SerializeInternalAsync(IEnumerable<RpcResponse> responses, bool isBulkRequest, Stream stream)
{
var jsonWriter = new Utf8JsonWriter(stream);
JsonWriterOptions options = this.serverConfig.Value.JsonSerializerSettings.ToWriterOptions();
var jsonWriter = new Utf8JsonWriter(stream, options);
try
{
if (isBulkRequest)
Expand Down
13 changes: 13 additions & 0 deletions src/EdjCase.JsonRpc.Router/Utilities/ExtensionsUtil.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Text.Json;
using System.Threading.Tasks;

namespace EdjCase.JsonRpc.Router.Utilities
Expand All @@ -16,6 +18,17 @@ public static bool IsNullableType(this Type type)
{
return !type.IsValueType || Nullable.GetUnderlyingType(type) != null;
}

public static JsonWriterOptions ToWriterOptions(this JsonSerializerOptions? options)
{
JsonWriterOptions writerOptions = default;
if (options != null)
{
writerOptions.Encoder = options.Encoder;
writerOptions.Indented = options.WriteIndented;
}
return writerOptions;
}
}

internal static class RouteContextExtensions
Expand Down
17 changes: 9 additions & 8 deletions src/EdjCase.JsonRpc.Router/Utilities/JsonStringGeneratorUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
Expand All @@ -10,21 +10,22 @@ internal class JsonStringGeneratorUtil
{
private delegate void WriteJson<T>(T value, ref Utf8JsonWriter writer);

public static string FromObject(Dictionary<string, RpcParameter> obj)
public static string FromObject(Dictionary<string, RpcParameter> obj, JsonSerializerOptions? options = null)
{
return JsonStringGeneratorUtil.From(obj, JsonStringGeneratorUtil.WriteObject);
return JsonStringGeneratorUtil.From(obj, JsonStringGeneratorUtil.WriteObject, options);
}

public static string FromArray(RpcParameter[] array)
public static string FromArray(RpcParameter[] array, JsonSerializerOptions? options = null)
{
return JsonStringGeneratorUtil.From(array, JsonStringGeneratorUtil.WriteArray);
return JsonStringGeneratorUtil.From(array, JsonStringGeneratorUtil.WriteArray, options);
}

private static string From<T>(T value, WriteJson<T> writeJsonFunc)
private static string From<T>(T value, WriteJson<T> writeJsonFunc, JsonSerializerOptions? options)
{
using (var utf8Stream = new MemoryStream())
{
var writer = new Utf8JsonWriter(utf8Stream);
{
JsonWriterOptions writerOptions = options.ToWriterOptions();
var writer = new Utf8JsonWriter(utf8Stream, writerOptions);
try
{
writeJsonFunc(value, ref writer);
Expand Down

0 comments on commit 3e324e3

Please sign in to comment.