Ejemplo n.º 1
0
        internal void WriteTo(Utf8JsonWriter writer)
        {
            switch (Type)
            {
            case JwtValueKind.String:
                writer.WriteString(Name, (string)Value);
                break;

            case JwtValueKind.JsonEncodedString:
                writer.WriteString(Name, (JsonEncodedText)Value);
                break;

            case JwtValueKind.Int32:
                writer.WriteNumber(Name, (int)Value);
                break;

            case JwtValueKind.Int64:
                writer.WriteNumber(Name, (long)Value);
                break;

            case JwtValueKind.UInt32:
                writer.WriteNumber(Name, (uint)Value);
                break;

            case JwtValueKind.UInt64:
                writer.WriteNumber(Name, (ulong)Value);
                break;

            case JwtValueKind.Float:
                writer.WriteNumber(Name, (float)Value);
                break;

            case JwtValueKind.Double:
                writer.WriteNumber(Name, (double)Value);
                break;

            case JwtValueKind.Object:
                if (Value is IJwtSerializable serializable)
                {
                    writer.WritePropertyName(Name);
                    serializable.WriteTo(writer);
                }
                else
                {
                    writer.WritePropertyName(Name);
                    JsonSerializer.Serialize(writer, Value, JsonSerializationBehavior.SerializerOptions);
                }
                break;

            case JwtValueKind.Array:
                writer.WritePropertyName(Name);
                JsonSerializer.Serialize(writer, Value, JsonSerializationBehavior.SerializerOptions);
                break;

            case JwtValueKind.True:
                writer.WriteBoolean(Name, true);
                break;

            case JwtValueKind.False:
                writer.WriteBoolean(Name, false);
                break;

            case JwtValueKind.Null:
                writer.WriteNull(Name);
                break;

            default:
                ThrowHelper.ThrowInvalidOperationException_NotSupportedJsonType(Type);
                break;
            }
        }
Ejemplo n.º 2
0
        public static Address FromJson(ReadOnlySpan <byte> json)
        {
            Utf8JsonReader reader = new Utf8JsonReader(json, true, default);

            var address = new Address();

            while (reader.Read())
            {
                switch (reader.TokenType)
                {
                case JsonTokenType.EndObject:
                    return(address);

                case JsonTokenType.PropertyName:
                    var propertyName = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;

                    reader.Read();
                    switch (reader.TokenType)
                    {
                    case JsonTokenType.String:
                        if (propertyName.SequenceEqual(OidcClaims.FormattedUtf8))
                        {
                            address.Formatted = reader.GetString();
                        }
                        else if (propertyName.SequenceEqual(OidcClaims.StreetAddressUtf8))
                        {
                            address.StreetAddress = reader.GetString();
                        }
                        else if (propertyName.SequenceEqual(OidcClaims.LocalityUtf8))
                        {
                            address.Locality = reader.GetString();
                        }
                        else if (propertyName.SequenceEqual(OidcClaims.RegionUtf8))
                        {
                            address.Region = reader.GetString();
                        }
                        else if (propertyName.SequenceEqual(OidcClaims.PostalCodeUtf8))
                        {
                            address.PostalCode = reader.GetString();
                        }
                        else if (propertyName.SequenceEqual(OidcClaims.CountryUtf8))
                        {
                            address.Country = reader.GetString();
                        }
                        break;

                    case JsonTokenType.StartArray:
                        JsonParser.ConsumeJsonArray(ref reader);
                        break;

                    case JsonTokenType.StartObject:
                        JsonParser.ConsumeJsonObject(ref reader);
                        break;
                    }
                    break;

                default:
                    ThrowHelper.ThrowFormatException_MalformedJson();
                    break;
                }
            }

            ThrowHelper.ThrowFormatException_MalformedJson();
            return(null);
        }