public override StreamMetadata Read(ref Utf8JsonReader reader, Type typeToConvert,
                                            JsonSerializerOptions options)
        {
            int?           maxCount = null;
            TimeSpan?      maxAge = null, cacheControl = null;
            StreamRevision?truncateBefore = null;
            StreamAcl      acl            = null;

            using var stream = new MemoryStream();
            using var customMetadataWriter = new Utf8JsonWriter(stream);

            if (reader.TokenType != JsonTokenType.StartObject)
            {
                throw new InvalidOperationException();
            }

            customMetadataWriter.WriteStartObject();

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    break;
                }

                if (reader.TokenType != JsonTokenType.PropertyName)
                {
                    throw new InvalidOperationException();
                }

                switch (reader.GetString())
                {
                case SystemMetadata.MaxCount:
                    if (!reader.Read())
                    {
                        throw new InvalidOperationException();
                    }

                    maxCount = reader.GetInt32();
                    break;

                case SystemMetadata.MaxAge:
                    if (!reader.Read())
                    {
                        throw new InvalidOperationException();
                    }

                    var int64 = reader.GetInt64();
                    maxAge = TimeSpan.FromSeconds(int64);
                    break;

                case SystemMetadata.CacheControl:
                    if (!reader.Read())
                    {
                        throw new InvalidOperationException();
                    }

                    cacheControl = TimeSpan.FromSeconds(reader.GetInt64());
                    break;

                case SystemMetadata.TruncateBefore:
                    if (!reader.Read())
                    {
                        throw new InvalidOperationException();
                    }

                    var value = reader.GetInt64();
                    truncateBefore = value == long.MaxValue
                                                        ? StreamRevision.End
                                                        : StreamRevision.FromInt64(value);
                    break;

                case SystemMetadata.Acl:
                    if (!reader.Read())
                    {
                        throw new InvalidOperationException();
                    }

                    acl = StreamAclJsonConverter.Instance.Read(ref reader, typeof(StreamAcl), options);
                    break;

                default:
                    customMetadataWriter.WritePropertyName(reader.GetString());
                    reader.Read();
                    switch (reader.TokenType)
                    {
                    case JsonTokenType.Comment:
                        customMetadataWriter.WriteCommentValue(reader.GetComment());
                        break;

                    case JsonTokenType.String:
                        customMetadataWriter.WriteStringValue(reader.GetString());
                        break;

                    case JsonTokenType.Number:
                        customMetadataWriter.WriteNumberValue(reader.GetDouble());
                        break;

                    case JsonTokenType.True:
                    case JsonTokenType.False:
                        customMetadataWriter.WriteBooleanValue(reader.GetBoolean());
                        break;

                    case JsonTokenType.Null:
                        reader.Read();
                        customMetadataWriter.WriteNullValue();
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    break;
                }
            }

            customMetadataWriter.WriteEndObject();
            customMetadataWriter.Flush();

            stream.Position = 0;

            return(new StreamMetadata(maxCount, maxAge, truncateBefore, cacheControl, acl,
                                      JsonDocument.Parse(stream)));
        }
Beispiel #2
0
 public void FromInt64ExpectedResult(long value, StreamRevision expected)
 => Assert.Equal(expected, StreamRevision.FromInt64(value));