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..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 @@ -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..5c974d5a9 --- /dev/null +++ b/src/JWT/Serializers/SystemTextJsonSerializer.cs @@ -0,0 +1,58 @@ +#if SYSTEMTEXTJSON + +using System; +using System.Collections.Generic; +using System.Text.Json; + +namespace JWT.Serializers +{ + public sealed class SystemTextJsonSerializer : IJsonSerializer + { + public string Serialize(object obj) => + JsonSerializer.Serialize(obj); + + public T Deserialize(string json) + { + var data = JsonSerializer.Deserialize(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; + + // 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 = (JsonElement)odata[key]; + switch (value.ValueKind) + { + 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: + default: + throw new NotSupportedException(nameof(value.ValueKind)); + } + } + + return ndata is T obj ? obj : data; + } + } +} +#endif