Ejemplo n.º 1
0
        public override FragmentDistributionFailedEvent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            JsonCommentHandling commentHandling = options.ReadCommentHandling;

            reader.SkipComments(commentHandling);
            reader.ExpectJsonToken(JsonTokenType.StartObject);

            Guid?  eventId = null;
            string?hash    = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.Comment)
                {
                    continue;
                }

                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(new FragmentDistributionFailedEvent(
                               hash ?? throw new JsonException($"Property {nameof(FragmentDistributionFailedEvent.Hash)} is not defined."),
                               eventId ?? throw new JsonException($"Property {nameof(FragmentDistributionFailedEvent.EventId)} is not defined.")));
                }

                reader.ExpectJsonToken(JsonTokenType.PropertyName);
                string propertyName = reader.GetString();

                if (propertyName.Equals(nameof(FragmentDistributionFailedEvent.EventId)))
                {
                    if (eventId.HasValue)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.String);
                    eventId = reader.GetGuid();
                }
                else if (propertyName.Equals(nameof(FragmentDistributionFailedEvent.Hash)))
                {
                    if (!(hash is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.String);
                    hash = reader.GetString();
                }
                else
                {
                    reader.Skip();
                }
            }

            throw new JsonException("Json input ended unexpectedly.");
        }
        public override FileMetadata Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            JsonCommentHandling commentHandling = options.ReadCommentHandling;

            reader.SkipComments(commentHandling);
            reader.ExpectJsonToken(JsonTokenType.StartObject);

            long?size = null;
            IEnumerable <string>?fragmentSequence = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.Comment)
                {
                    continue;
                }

                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(new FileMetadata(
                               size ?? throw new JsonException($"Property {nameof(FileMetadata.Size)} is not defined."),
                               fragmentSequence ?? throw new JsonException($"Property {nameof(FileMetadata.FragmentSequence)} is not defined.")));
                }

                reader.ExpectJsonToken(JsonTokenType.PropertyName);
                string propertyName = reader.GetString();

                if (propertyName.Equals(nameof(FileMetadata.Size)))
                {
                    if (size.HasValue)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.Number);
                    size = reader.GetInt64();
                }
                else if (propertyName.Equals(nameof(FileMetadata.FragmentSequence)))
                {
                    if (!(fragmentSequence is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartArray);
                    fragmentSequence = JsonSerializer.Deserialize <IEnumerable <string> >(ref reader, options);
                }
                else
                {
                    reader.Skip();
                }
            }

            throw new JsonException("Json input ended unexpectedly.");
        }
        public override ClientMetadata Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            JsonCommentHandling commentHandling = options.ReadCommentHandling;

            reader.SkipComments(commentHandling);
            reader.ExpectJsonToken(JsonTokenType.StartObject);

            ISet <Uri>?   endpoints       = null;
            ISet <string>?storedFragments = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.Comment)
                {
                    continue;
                }

                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(new ClientMetadata(
                               endpoints ?? throw new JsonException($"Property {nameof(ClientMetadata.Endpoints)} is not defined."),
                               storedFragments ?? throw new JsonException($"Property {nameof(ClientMetadata.StoredFragments)} is not defined.")));
                }

                reader.ExpectJsonToken(JsonTokenType.PropertyName);
                string propertyName = reader.GetString();

                if (propertyName.Equals(nameof(ClientMetadata.Endpoints)))
                {
                    if (!(endpoints is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartArray);
                    endpoints = JsonSerializer.Deserialize <ISet <Uri> >(ref reader, options);
                }
                else if (propertyName.Equals(nameof(ClientMetadata.StoredFragments)))
                {
                    if (!(storedFragments is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartArray);
                    storedFragments = JsonSerializer.Deserialize <ISet <string> >(ref reader, options);
                }
                else
                {
                    reader.Skip();
                }
            }

            throw new JsonException("Json input ended unexpectedly.");
        }
Ejemplo n.º 4
0
        public override ClientRegisteredEvent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            JsonCommentHandling commentHandling = options.ReadCommentHandling;

            reader.SkipComments(commentHandling);
            reader.ExpectJsonToken(JsonTokenType.StartObject);

            Guid?          eventId          = null;
            string?        clientIdentifier = null;
            ClientMetadata?info             = null;
            IDictionary <string, FileMetadata>?    addedFiles     = null;
            IDictionary <string, FragmentMetadata>?addedFragments = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.Comment)
                {
                    continue;
                }

                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(new ClientRegisteredEvent(
                               clientIdentifier ?? throw new JsonException($"Property {nameof(ClientRegisteredEvent.ClientIdentifier)} is not defined."),
                               info ?? throw new JsonException($"Property {nameof(ClientRegisteredEvent.Info)} is not defined."),
                               addedFiles ?? throw new JsonException($"Property {nameof(ClientRegisteredEvent.AddedFiles)} is not defined."),
                               addedFragments ?? throw new JsonException($"Property {nameof(ClientRegisteredEvent.AddedFragments)} is not defined."),
                               eventId ?? throw new JsonException($"Property {nameof(ClientRegisteredEvent.EventId)} is not defined.")));
                }

                reader.ExpectJsonToken(JsonTokenType.PropertyName);
                string propertyName = reader.GetString();

                if (propertyName.Equals(nameof(ClientRegisteredEvent.EventId)))
                {
                    if (eventId.HasValue)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.String);
                    eventId = reader.GetGuid();
                }
                else if (propertyName.Equals(nameof(ClientRegisteredEvent.ClientIdentifier)))
                {
                    if (clientIdentifier != null)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.String);
                    clientIdentifier = reader.GetString();
                }
                else if (propertyName.Equals(nameof(ClientRegisteredEvent.Info)))
                {
                    if (!(info is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartObject);
                    info = JsonSerializer.Deserialize <ClientMetadata>(ref reader, options);
                }
                else if (propertyName.Equals(nameof(ClientRegisteredEvent.AddedFiles)))
                {
                    if (!(addedFiles is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartObject);
                    addedFiles = JsonSerializer.Deserialize <IDictionary <string, FileMetadata> >(ref reader, options);
                }
                else if (propertyName.Equals(nameof(ClientRegisteredEvent.AddedFragments)))
                {
                    if (!(addedFragments is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartObject);
                    addedFragments = JsonSerializer.Deserialize <IDictionary <string, FragmentMetadata> >(ref reader, options);
                }
                else
                {
                    reader.Skip();
                }
            }

            throw new JsonException("Json input ended unexpectedly.");
        }
Ejemplo n.º 5
0
        public override ClientJoinAcceptedEvent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            JsonCommentHandling commentHandling = options.ReadCommentHandling;

            reader.SkipComments(commentHandling);
            reader.ExpectJsonToken(JsonTokenType.StartObject);

            Guid?  eventId          = null;
            string?clientIdentifier = null;
            long?  fragmentSize     = null;
            IDictionary <string, FileMetadata>?    addFileInfos     = null;
            IDictionary <string, FragmentMetadata>?addFragmentInfos = null;
            ISet <string>?removeFileInfos = null;
            ISet <string>?removeFragments = null;
            IDictionary <string, ClientMetadata>?clients = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.Comment)
                {
                    continue;
                }

                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(new ClientJoinAcceptedEvent(
                               eventId: eventId ?? throw new JsonException($"Property {nameof(ClientJoinAcceptedEvent.EventId)} is not defined."),
                               clientIdentifier: clientIdentifier ?? throw new JsonException($"Property {nameof(ClientJoinAcceptedEvent.ClientIdentifier)} is not defined."),
                               fragmentSize: fragmentSize ?? throw new JsonException($"Property {nameof(ClientJoinAcceptedEvent.FragmentSize)} is not defined."),
                               addFileInfos: addFileInfos ?? throw new JsonException($"Property {nameof(ClientJoinAcceptedEvent.AddFileInfos)} is not defined."),
                               addFragmentInfos: addFragmentInfos ?? throw new JsonException($"Property {nameof(ClientJoinAcceptedEvent.AddFragmentInfos)} is not defined."),
                               removeFileInfos: removeFileInfos ?? throw new JsonException($"Property {nameof(ClientJoinAcceptedEvent.RemoveFileInfos)} is not defined."),
                               removeFragmentInfos: removeFragments ?? throw new JsonException($"Property {nameof(ClientJoinAcceptedEvent.RemoveFragmentInfos)} is not defined."),
                               clients: clients ?? throw new JsonException($"Property {nameof(ClientJoinAcceptedEvent.Clients)} is not defined.")));
                }

                reader.ExpectJsonToken(JsonTokenType.PropertyName);
                string propertyName = reader.GetString();

                if (propertyName.Equals(nameof(ClientJoinAcceptedEvent.EventId)))
                {
                    if (eventId.HasValue)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.String);
                    eventId = reader.GetGuid();
                }
                else if (propertyName.Equals(nameof(ClientJoinAcceptedEvent.ClientIdentifier)))
                {
                    if (!(clientIdentifier is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.String);
                    clientIdentifier = reader.GetString();
                }
                else if (propertyName.Equals(nameof(ClientJoinAcceptedEvent.FragmentSize)))
                {
                    if (fragmentSize.HasValue)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.Number);
                    fragmentSize = reader.GetInt64();
                }
                else if (propertyName.Equals(nameof(ClientJoinAcceptedEvent.AddFileInfos)))
                {
                    if (!(addFileInfos is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartObject);
                    addFileInfos = JsonSerializer.Deserialize <IDictionary <string, FileMetadata> >(ref reader, options);
                }
                else if (propertyName.Equals(nameof(ClientJoinAcceptedEvent.AddFragmentInfos)))
                {
                    if (addFragmentInfos != null)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartObject);
                    addFragmentInfos = JsonSerializer.Deserialize <IDictionary <string, FragmentMetadata> >(ref reader, options);
                }
                else if (propertyName.Equals(nameof(ClientJoinAcceptedEvent.RemoveFileInfos)))
                {
                    if (!(removeFileInfos is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartArray);
                    removeFileInfos = JsonSerializer.Deserialize <ISet <string> >(ref reader, options);
                }
                else if (propertyName.Equals(nameof(ClientJoinAcceptedEvent.RemoveFragmentInfos)))
                {
                    if (!(removeFragments is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartArray);
                    removeFragments = JsonSerializer.Deserialize <ISet <string> >(ref reader, options);
                }
                else if (propertyName.Equals(nameof(ClientJoinAcceptedEvent.Clients)))
                {
                    if (!(clients is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartObject);
                    clients = JsonSerializer.Deserialize <IDictionary <string, ClientMetadata> >(ref reader, options);
                }
                else
                {
                    reader.Skip();
                }
            }

            throw new JsonException("Json input ended unexpectedly.");
        }
Ejemplo n.º 6
0
        public override ClientJoinRequestedEvent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            JsonCommentHandling commentHandling = options.ReadCommentHandling;

            reader.SkipComments(commentHandling);
            reader.ExpectJsonToken(JsonTokenType.StartObject);

            bool readFragmentSize = false;

            Guid?  eventId       = null;
            string?hashAlgorithm = null;
            long?  fragmentSize  = null;
            IDictionary <string, FileMetadata>?    knownFileInfos  = null;
            IDictionary <string, FragmentMetadata>?storedFragments = null;
            ISet <Uri>?endpoints = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.Comment)
                {
                    continue;
                }

                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(new ClientJoinRequestedEvent(
                               knownFileInfos ?? throw new JsonException($"Property {nameof(ClientJoinRequestedEvent.KnownFileInfos)} is not defined."),
                               storedFragments ?? throw new JsonException($"Property {nameof(ClientJoinRequestedEvent.StoredFragments)} is not defined."),
                               endpoints ?? throw new JsonException($"Property {nameof(ClientJoinRequestedEvent.Endpoints)} is not defined."),
                               hashAlgorithm ?? throw new JsonException($"Property {nameof(ClientJoinRequestedEvent.HashAlgorithm)} is not defined."),
                               fragmentSize,
                               eventId ?? throw new JsonException($"Property {nameof(ClientJoinRequestedEvent.EventId)} is not defined.")));
                }

                reader.ExpectJsonToken(JsonTokenType.PropertyName);
                string propertyName = reader.GetString();

                if (propertyName.Equals(nameof(ClientJoinRequestedEvent.EventId)))
                {
                    if (eventId.HasValue)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.String);
                    eventId = reader.GetGuid();
                }
                else if (propertyName.Equals(nameof(ClientJoinRequestedEvent.HashAlgorithm)))
                {
                    if (!(hashAlgorithm is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.String);
                    hashAlgorithm = reader.GetString();
                }
                else if (propertyName.Equals(nameof(ClientJoinRequestedEvent.FragmentSize)))
                {
                    if (readFragmentSize)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    readFragmentSize = true;

                    reader.ReadSkippingComments(commentHandling);
                    fragmentSize = reader.TokenType switch
                    {
                        JsonTokenType.Number => new long?(reader.GetInt64()),
                        JsonTokenType.Null => null,
                        _ => throw new JsonException($"Expected Token Type Number or Null. Actual: {reader.TokenType}")
                    };
                }
                else if (propertyName.Equals(nameof(ClientJoinRequestedEvent.KnownFileInfos)))
                {
                    if (!(knownFileInfos is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartObject);
                    knownFileInfos = JsonSerializer.Deserialize <IDictionary <string, FileMetadata> >(ref reader, options);
                }
                else if (propertyName.Equals(nameof(ClientJoinRequestedEvent.StoredFragments)))
                {
                    if (!(storedFragments is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartObject);
                    storedFragments = JsonSerializer.Deserialize <IDictionary <string, FragmentMetadata> >(ref reader, options);
                }
                else if (propertyName.Equals(nameof(ClientJoinRequestedEvent.Endpoints)))
                {
                    if (!(endpoints is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.StartArray);
                    endpoints = JsonSerializer.Deserialize <ISet <Uri> >(ref reader, options);
                }
                else
                {
                    reader.Skip();
                }
            }

            throw new JsonException("Json input ended unexpectedly.");
        }
Ejemplo n.º 7
0
        public override ClientJoinDeniedEvent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            JsonCommentHandling commentHandling = options.ReadCommentHandling;

            reader.SkipComments(commentHandling);
            reader.ExpectJsonToken(JsonTokenType.StartObject);

            bool readReason = false;

            Guid?  eventId                  = null;
            string?clientIdentifier         = null;
            ClientJoinDeniedCode?reasonCode = null;
            string?reason = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.Comment)
                {
                    continue;
                }

                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(new ClientJoinDeniedEvent(
                               eventId: eventId ?? throw new JsonException($"Property {nameof(ClientJoinAcceptedEvent.EventId)} is not defined."),
                               clientIdentifier: clientIdentifier ?? throw new JsonException($"Property {nameof(ClientJoinAcceptedEvent.ClientIdentifier)} is not defined."),
                               reasonCode: reasonCode ?? throw new JsonException($"Property {nameof(ClientJoinAcceptedEvent.ClientIdentifier)} is not defined."),
                               reason: reason));
                }

                reader.ExpectJsonToken(JsonTokenType.PropertyName);
                string propertyName = reader.GetString();

                if (propertyName.Equals(nameof(ClientJoinDeniedEvent.EventId)))
                {
                    if (eventId.HasValue)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.String);
                    eventId = reader.GetGuid();
                }
                else if (propertyName.Equals(nameof(ClientJoinDeniedEvent.ClientIdentifier)))
                {
                    if (!(clientIdentifier is null))
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.String);
                    clientIdentifier = reader.GetString();
                }
                else if (propertyName.Equals(nameof(ClientJoinDeniedEvent.ReasonCode)))
                {
                    if (reasonCode.HasValue)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    reader.ReadSkippingComments(commentHandling);
                    reader.ExpectJsonToken(JsonTokenType.Number);
                    reasonCode = (ClientJoinDeniedCode)reader.GetInt32();

                    if (!Enum.IsDefined(typeof(ClientJoinDeniedCode), reasonCode.Value))
                    {
                        throw new JsonException(reasonCode.Value + " is an invalid enum value of " + nameof(ClientJoinDeniedCode));
                    }
                }
                else if (propertyName.Equals(nameof(ClientJoinDeniedEvent.Reason)))
                {
                    if (readReason)
                    {
                        throw new JsonException("Property defined twice: " + propertyName);
                    }

                    readReason = true;

                    reader.ReadSkippingComments(commentHandling);
                    reason = reader.TokenType switch
                    {
                        JsonTokenType.String => reader.GetString(),
                        JsonTokenType.Null => null,
                        _ => throw new JsonException($"Expected Token Type String or Null. Actual: {reader.TokenType}")
                    };
                }
                else
                {
                    reader.Skip();
                }
            }

            throw new JsonException("Json input ended unexpectedly.");
        }