Ejemplo n.º 1
0
        private static BlockMetadata ReadBlockMetadata(ref Utf8JsonStreamReader reader)
        {
            ushort id = 0;
            IList <DocumentMetadata> docs = Array.Empty <DocumentMetadata>();

            JsonReaderHelper.ReadToken(ref reader, JsonTokenType.StartObject);

            while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
            {
                var propertyName = reader.GetString();
                switch (propertyName)
                {
                case BlockIdPropertyName:
                    id = ParseId(JsonReaderHelper.ReadString(ref reader).AsSpan());
                    break;

                case DocumentMetadataListPropertyName:
                    docs = ReadDocumentsMetadata(ref reader);
                    break;

                default:
                    throw new InvalidDataException($"Unexpected property at position {reader.Position}: {propertyName}");
                }
            }

            return(new BlockMetadata(id, docs));
        }
Ejemplo n.º 2
0
        private static DocumentMetadata ReadDocumentMetadata(ref Utf8JsonStreamReader reader)
        {
            var id    = DocumentId.Zero;
            var title = string.Empty;

            while (reader.Read() && reader.TokenType == JsonTokenType.PropertyName)
            {
                var propertyName = reader.GetString();
                switch (propertyName)
                {
                case DocumentIdPropertyName:
                    id = DocumentId.Parse(JsonReaderHelper.ReadString(ref reader).AsSpan());
                    break;

                case TitlePropertyName:
                    title = JsonReaderHelper.ReadString(ref reader);
                    break;

                default:
                    throw new InvalidDataException($"Unexpected property at position {reader.Position}: {propertyName}");
                }
            }

            return(new DocumentMetadata(id, title));
        }
Ejemplo n.º 3
0
        private static CorpusZipMetadata ReadCorpusMetadata(ref Utf8JsonStreamReader reader)
        {
            IReadOnlyList <DocumentId> firstDocumentIdInBlock = Array.Empty <DocumentId>();

            H.ReadToken(ref reader, JsonTokenType.StartObject);
            while (reader.Read() && reader.TokenType == JsonTokenType.PropertyName)
            {
                var propertyName = reader.GetString();
                switch (propertyName)
                {
                case FirstDocumentIdInBlockPropertyName:
                    firstDocumentIdInBlock = ReadFirstDocumentIdInBlock(ref reader);
                    break;

                default:
                    throw new InvalidDataException($"Unexpected property at position {reader.Position}: {propertyName}");
                }
            }

            if (firstDocumentIdInBlock.Count == 0)
            {
                throw new InvalidDataException("Mandatory field ReadFirstDocumentIdInBlock was not found");
            }

            return(new CorpusZipMetadata(firstDocumentIdInBlock));
        }
Ejemplo n.º 4
0
            /// <summary>
            /// Read the owner from the stream and consume to the end of the payload.
            /// </summary>
            /// <param name="streamReader">The reader from which to read the id.</param>
            /// <returns>The ID read from the payload.</returns>
            public static string ReadOwner(ref Utf8JsonStreamReader streamReader)
            {
                if (streamReader.TokenType != JsonTokenType.StartObject)
                {
                    throw new JsonException("Expected to be at the start of the event payload.");
                }

                // Read the ID
                streamReader.Read();
                if (streamReader.TokenType != JsonTokenType.PropertyName || !streamReader.Match(OwnerPropertyString))
                {
                    throw new JsonException($"Expected to find the {OwnerPropertyString}.");
                }

                streamReader.Read();
                if (streamReader.TokenType != JsonTokenType.String)
                {
                    throw new JsonException("Expected to find a string property.");
                }

                string owner = streamReader.GetString() ?? throw new JsonException("Expected to find an owner.");

                // Read to the end of the object
                streamReader.Read();

                if (streamReader.TokenType != JsonTokenType.EndObject)
                {
                    throw new JsonException("Expected to be at the end of the event payload.");
                }

                return(owner);
            }
        private static string ReadString(ref Utf8JsonStreamReader reader)
        {
            if (reader.Read() && reader.TokenType == JsonTokenType.String)
            {
                return(reader.GetString());
            }

            throw new WorkloadManifestFormatException(Strings.ExpectedStringAtOffset, reader.TokenStartIndex);
        }
Ejemplo n.º 6
0
 private static string?ReadStringLocalized(ref Utf8JsonStreamReader reader, LocalizationCatalog?localizationCatalog, string id)
 {
     ConsumeToken(ref reader, JsonTokenType.String);
     return(localizationCatalog?.Localize(id) ?? reader.GetString());
 }
Ejemplo n.º 7
0
 private static string ReadString(ref Utf8JsonStreamReader reader)
 {
     ConsumeToken(ref reader, JsonTokenType.String);
     return(reader.GetString());
 }