Skip to content

Commit

Permalink
DictionaryStringObjectJsonConverter is only needed when deserializing (
Browse files Browse the repository at this point in the history
…#441)

* DictionaryStringObjectJsonConverter is only needed when deserializing 
* Bumped version to 10.0.0-beta11

Co-authored-by: Alexander Batishchev <[email protected]>
  • Loading branch information
hartmark and abatishchev authored Sep 26, 2022
1 parent a2d7035 commit 96e61e1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/JWT/JWT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<Authors>Alexander Batishchev, John Sheehan, Michael Lehenbauer</Authors>
<PackageTags>jwt;json;authorization</PackageTags>
<PackageLicenseExpression>CC0-1.0</PackageLicenseExpression>
<Version>10.0.0-beta10</Version>
<Version>10.0.0-beta11</Version>
<FileVersion>10.0.0.0</FileVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
<RootNamespace>JWT</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#if MODERN_DOTNET
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -40,101 +37,9 @@ public override Dictionary<string, object> Read(ref Utf8JsonReader reader, Type

public override void Write(Utf8JsonWriter writer, Dictionary<string, object> value, JsonSerializerOptions options)
{
writer.WriteStartObject();
foreach (var key in value.Keys)
{
HandleValue(writer, key, value[key]);
}
writer.WriteEndObject();
}

private static void HandleValue(Utf8JsonWriter writer, string key, object objectValue)
{
if (key is object)
writer.WritePropertyName(key);

switch (objectValue)
{
case string stringValue:
{
writer.WriteStringValue(stringValue);
break;
}
case DateTime dateTime:
{
writer.WriteStringValue(dateTime);
break;
}
case long longValue:
{
writer.WriteNumberValue(longValue);
break;
}
case int intValue:
{
writer.WriteNumberValue(intValue);
break;
}
case float floatValue:
{
writer.WriteNumberValue(floatValue);
break;
}
case double doubleValue:
{
writer.WriteNumberValue(doubleValue);
break;
}
case decimal decimalValue:
{
writer.WriteNumberValue(decimalValue);
break;
}
case bool boolValue:
{
writer.WriteBooleanValue(boolValue);
break;
}
case Dictionary<string, object> dic:
{
writer.WriteStartObject();
foreach (var item in dic)
{
HandleValue(writer, item.Key, item.Value);
}
writer.WriteEndObject();
break;
}
case IEnumerable enumerable:
{
writer.WriteStartArray();
foreach (var item in enumerable)
{
HandleValue(writer, item);
}

writer.WriteEndArray();
break;
}
default:
{
var dic = objectValue.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(p => p.Name, p => p.GetValue(objectValue, null));
writer.WriteStartObject();
foreach (var p in dic)
{
HandleValue(writer, p.Key, p.Value);
}
writer.WriteEndObject();
break;
}
}
throw new NotSupportedException("Use the built in logic for serializing to json.");
}

private static void HandleValue(Utf8JsonWriter writer, object value) =>
HandleValue(writer, null, value);

private object ExtractValue(ref Utf8JsonReader reader, JsonSerializerOptions options)
{
switch (reader.TokenType)
Expand Down
8 changes: 5 additions & 3 deletions src/JWT/Serializers/SystemTextSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ namespace JWT.Serializers
/// </summary>
public class SystemTextSerializer : IJsonSerializer
{
private static readonly JsonSerializerOptions _options = new JsonSerializerOptions
private static readonly JsonSerializerOptions _optionsForSerialize = new JsonSerializerOptions();

private static readonly JsonSerializerOptions _optionsForDeserialize = new JsonSerializerOptions
{
Converters =
{
Expand All @@ -25,7 +27,7 @@ public string Serialize(object obj)
if (obj is null)
throw new ArgumentNullException(nameof(obj));

return JsonSerializer.Serialize(obj, _options);
return JsonSerializer.Serialize(obj, _optionsForSerialize);
}


Expand All @@ -39,7 +41,7 @@ public object Deserialize(Type type, string json)
if (String.IsNullOrEmpty(json))
throw new ArgumentException(nameof(json));

return JsonSerializer.Deserialize(json, type, _options);
return JsonSerializer.Deserialize(json, type, _optionsForDeserialize);
}
}
}
Expand Down

0 comments on commit 96e61e1

Please sign in to comment.