From a63c9109d6b4e40d64999e9284d9cb5b71b88358 Mon Sep 17 00:00:00 2001 From: Luca Poretti Date: Fri, 15 May 2020 23:00:55 +0200 Subject: [PATCH 1/6] fix #218 --- src/JWT/Builder/JwtHeader.cs | 24 ++++++++ src/JWT/JWT.csproj | 11 +++- .../Serializers/SystemTextJsonSerializer.cs | 60 +++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/JWT/Serializers/SystemTextJsonSerializer.cs diff --git a/src/JWT/Builder/JwtHeader.cs b/src/JWT/Builder/JwtHeader.cs index e34ff2769..ab3639dc5 100644 --- a/src/JWT/Builder/JwtHeader.cs +++ b/src/JWT/Builder/JwtHeader.cs @@ -1,4 +1,7 @@ using Newtonsoft.Json; +#if SYSTEMTEXTJSON +using System.Text.Json.Serialization; +#endif namespace JWT.Builder { @@ -8,24 +11,45 @@ namespace JWT.Builder public class JwtHeader { [JsonProperty("typ")] +#if SYSTEMTEXTJSON + [JsonPropertyName("typ")] +#endif public string Type { get; set; } [JsonProperty("cty")] +#if SYSTEMTEXTJSON + [JsonPropertyName("cty")] +#endif public string ContentType { get; set; } [JsonProperty("alg")] +#if SYSTEMTEXTJSON + [JsonPropertyName("alg")] +#endif public string Algorithm { get; set; } [JsonProperty("kid")] +#if SYSTEMTEXTJSON + [JsonPropertyName("kid")] +#endif public string KeyId { get; set; } [JsonProperty("x5u")] +#if SYSTEMTEXTJSON + [JsonPropertyName("x5u")] +#endif public string X5u { get; set; } [JsonProperty("x5c")] +#if SYSTEMTEXTJSON + [JsonPropertyName("x5c")] +#endif public string X5c { get; set; } [JsonProperty("x5t")] +#if SYSTEMTEXTJSON + [JsonPropertyName("x5t")] +#endif public string X5t { get; set; } } } \ No newline at end of file diff --git a/src/JWT/JWT.csproj b/src/JWT/JWT.csproj index ac992144b..8a9904f2e 100644 --- a/src/JWT/JWT.csproj +++ b/src/JWT/JWT.csproj @@ -36,8 +36,16 @@ true + + SYSTEMTEXTJSON;$(DefineConstants) + + + + + + - + @@ -49,4 +57,5 @@ + diff --git a/src/JWT/Serializers/SystemTextJsonSerializer.cs b/src/JWT/Serializers/SystemTextJsonSerializer.cs new file mode 100644 index 000000000..2efa18289 --- /dev/null +++ b/src/JWT/Serializers/SystemTextJsonSerializer.cs @@ -0,0 +1,60 @@ +#if SYSTEMTEXTJSON +namespace JWT.Serializers +{ + using System; + using System.Collections.Generic; + using System.Text.Json; + using JWT; + + public class SystemTextJsonSerializer : IJsonSerializer + { + public string Serialize(object obj) + { + // Implement using favorite JSON serializer + return JsonSerializer.Serialize(obj); + } + + public T Deserialize(string json) + { + // Implement using favorite JSON serializer + var data = JsonSerializer.Deserialize(json); + + if (data is Dictionary odata) + { + var ndata = new Dictionary(); + foreach (var key in odata.Keys) + { + var value = (JsonElement)odata[key]; + switch (value.ValueKind) + { + case JsonValueKind.Undefined: + break; + case JsonValueKind.Object: + break; + case JsonValueKind.Array: + break; + case JsonValueKind.String: + ndata.Add(key, value.GetString()); + break; + case JsonValueKind.Number: + ndata.Add(key, value.GetInt64()); + break; + case JsonValueKind.True: + ndata.Add(key, true); + break; + case JsonValueKind.False: + ndata.Add(key, false); + break; + case JsonValueKind.Null: + break; + } + } + + if (ndata is T obj) return obj; + } + + return data; + } + } +} +#endif \ No newline at end of file From 0e609714b28414cd06d2cf853b6ac32f1b52d575 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Fri, 15 May 2020 19:16:49 -0700 Subject: [PATCH 2/6] Update JWT.csproj --- src/JWT/JWT.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/JWT/JWT.csproj b/src/JWT/JWT.csproj index 8a9904f2e..64cafe9b6 100644 --- a/src/JWT/JWT.csproj +++ b/src/JWT/JWT.csproj @@ -18,9 +18,9 @@ Alexander Batishchev, John Sheehan, Michael Lehenbauer jwt;json CC0-1.0 - 7.2.0 - 7.0.0.0 - 7.0.0.0 + 8.0.0-beta1 + 8.0.0.0 + 8.0.0.0 JWT true From 55d204d2a33f9366c985106b2362db4b19f6261a Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Fri, 15 May 2020 19:18:04 -0700 Subject: [PATCH 3/6] Update SystemTextJsonSerializer.cs --- .../Serializers/SystemTextJsonSerializer.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/JWT/Serializers/SystemTextJsonSerializer.cs b/src/JWT/Serializers/SystemTextJsonSerializer.cs index 2efa18289..d9bd857f5 100644 --- a/src/JWT/Serializers/SystemTextJsonSerializer.cs +++ b/src/JWT/Serializers/SystemTextJsonSerializer.cs @@ -1,12 +1,14 @@ #if SYSTEMTEXTJSON + +using System; +using System.Collections.Generic; +using System.Text.Json; +using JWT; + + namespace JWT.Serializers { - using System; - using System.Collections.Generic; - using System.Text.Json; - using JWT; - - public class SystemTextJsonSerializer : IJsonSerializer + public sealed class SystemTextJsonSerializer : IJsonSerializer { public string Serialize(object obj) { @@ -50,11 +52,12 @@ public T Deserialize(string json) } } - if (ndata is T obj) return obj; + if (ndata is T obj) + return obj; } return data; } } } -#endif \ No newline at end of file +#endif From b2a139820aacabf15f28972950ec779cdb3b4430 Mon Sep 17 00:00:00 2001 From: Luca Poretti Date: Sat, 16 May 2020 10:00:19 +0200 Subject: [PATCH 4/6] code cleanup --- .../Serializers/SystemTextJsonSerializer.cs | 58 ++++++++----------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/src/JWT/Serializers/SystemTextJsonSerializer.cs b/src/JWT/Serializers/SystemTextJsonSerializer.cs index d9bd857f5..30ed389a8 100644 --- a/src/JWT/Serializers/SystemTextJsonSerializer.cs +++ b/src/JWT/Serializers/SystemTextJsonSerializer.cs @@ -5,58 +5,48 @@ using System.Text.Json; using JWT; - namespace JWT.Serializers { public sealed class SystemTextJsonSerializer : IJsonSerializer { public string Serialize(object obj) { - // Implement using favorite JSON serializer return JsonSerializer.Serialize(obj); } public T Deserialize(string json) { - // Implement using favorite JSON serializer var data = JsonSerializer.Deserialize(json); - if (data is Dictionary odata) + if (!(data is Dictionary odata)) return data; + + var ndata = new Dictionary(); + foreach (var key in odata.Keys) { - var ndata = new Dictionary(); - foreach (var key in odata.Keys) + var value = odata[key]; + switch (value.ValueKind) { - var value = (JsonElement)odata[key]; - switch (value.ValueKind) - { - case JsonValueKind.Undefined: - break; - case JsonValueKind.Object: - break; - case JsonValueKind.Array: - break; - case JsonValueKind.String: - ndata.Add(key, value.GetString()); - break; - case JsonValueKind.Number: - ndata.Add(key, value.GetInt64()); - break; - case JsonValueKind.True: - ndata.Add(key, true); - break; - case JsonValueKind.False: - ndata.Add(key, false); - break; - case JsonValueKind.Null: - break; - } + case JsonValueKind.String: + ndata.Add(key, value.GetString()); + break; + case JsonValueKind.Number: + ndata.Add(key, value.GetInt64()); + break; + case JsonValueKind.True: + ndata.Add(key, true); + break; + case JsonValueKind.False: + ndata.Add(key, false); + break; + case JsonValueKind.Undefined: + case JsonValueKind.Object: + case JsonValueKind.Array: + case JsonValueKind.Null: + throw new NotSupportedException(nameof(value.ValueKind)); } - - if (ndata is T obj) - return obj; } - return data; + return ndata is T obj ? obj : data; } } } From 255db652b380c587e56d319d88aa2080eeb06c37 Mon Sep 17 00:00:00 2001 From: Luca Poretti Date: Sun, 17 May 2020 21:13:16 +0200 Subject: [PATCH 5/6] comments + fix --- src/JWT/Serializers/SystemTextJsonSerializer.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/JWT/Serializers/SystemTextJsonSerializer.cs b/src/JWT/Serializers/SystemTextJsonSerializer.cs index 30ed389a8..fb5a34aeb 100644 --- a/src/JWT/Serializers/SystemTextJsonSerializer.cs +++ b/src/JWT/Serializers/SystemTextJsonSerializer.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Text.Json; -using JWT; namespace JWT.Serializers { @@ -18,12 +17,18 @@ public T Deserialize(string json) { var data = JsonSerializer.Deserialize(json); - if (!(data is Dictionary odata)) return data; + // when deserializing a Dictionary + // System.Text.Json create JsonElement objects for every value of the dictionary + // but application will expect to have native basic types (not a JsonElement class) + if (!(data is Dictionary odata)) return data; + // we need to create another dictionary and fill it with the real values + // only basic types are supported (no complex object allowed, throw a NotSupportedException in these cases) + // number always converted to long (int64) basic type var ndata = new Dictionary(); foreach (var key in odata.Keys) { - var value = odata[key]; + var value = (JsonElement)odata[key]; switch (value.ValueKind) { case JsonValueKind.String: @@ -42,6 +47,7 @@ public T Deserialize(string json) case JsonValueKind.Object: case JsonValueKind.Array: case JsonValueKind.Null: + default: throw new NotSupportedException(nameof(value.ValueKind)); } } From 4f1adb58bc58bb75161b9ff82fe1bcdc22c31953 Mon Sep 17 00:00:00 2001 From: Alexander Batishchev Date: Mon, 18 May 2020 12:04:28 -0700 Subject: [PATCH 6/6] Update SystemTextJsonSerializer.cs --- src/JWT/Serializers/SystemTextJsonSerializer.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/JWT/Serializers/SystemTextJsonSerializer.cs b/src/JWT/Serializers/SystemTextJsonSerializer.cs index fb5a34aeb..5c974d5a9 100644 --- a/src/JWT/Serializers/SystemTextJsonSerializer.cs +++ b/src/JWT/Serializers/SystemTextJsonSerializer.cs @@ -8,10 +8,8 @@ namespace JWT.Serializers { public sealed class SystemTextJsonSerializer : IJsonSerializer { - public string Serialize(object obj) - { - return JsonSerializer.Serialize(obj); - } + public string Serialize(object obj) => + JsonSerializer.Serialize(obj); public T Deserialize(string json) { @@ -20,7 +18,8 @@ public T Deserialize(string json) // when deserializing a Dictionary // System.Text.Json create JsonElement objects for every value of the dictionary // but application will expect to have native basic types (not a JsonElement class) - if (!(data is Dictionary odata)) return data; + if (!(data is Dictionary odata)) + return data; // we need to create another dictionary and fill it with the real values // only basic types are supported (no complex object allowed, throw a NotSupportedException in these cases)