/// <summary> /// Read the start date from the stream and consume to the end of the payload. /// </summary> /// <param name="streamReader">The reader from which to read the start date.</param> /// <returns>The start date read from the payload.</returns> public static DateTimeOffset ReadStartDate(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(StartDatePropertyString)) { throw new JsonException($"Expected to find the {StartDatePropertyString}."); } streamReader.Read(); if (streamReader.TokenType != JsonTokenType.String) { throw new JsonException("Expected to find a string-encoded DateTimeOffset property."); } DateTimeOffset startDate = streamReader.GetDateTimeOffset(); // 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(startDate); }
/// <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); }
/// <summary> /// Read the ToDoItemId 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 Guid ReadToDoItemId(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(IdPropertyString)) { throw new JsonException($"Expected to find the {IdPropertyString}."); } streamReader.Read(); if (streamReader.TokenType != JsonTokenType.String) { throw new JsonException("Expected to find a string-encoded GUID property."); } Guid id = streamReader.GetGuid(); // 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(id); }
/// <summary> /// Find the value of the payload property in the event. /// </summary> /// <param name="streamReader">The <see cref="Utf8JsonStreamReader"/> from which we are reading the event.</param> public static void FindPayload(ref Utf8JsonStreamReader streamReader) { streamReader.Read(); if (streamReader.TokenType != JsonTokenType.PropertyName || !streamReader.Match(EventPayloadPropertyNameString)) { throw new JsonException($"Expected to find the {EventPayloadPropertyNameString} property."); } // position at the start of the payload streamReader.Read(); }
/// <summary> /// Find the event type value in the stream reader. /// </summary> /// <param name="streamReader">The stream reader in which to find the event type.</param> public static void FindEventType(ref Utf8JsonStreamReader streamReader) { streamReader.Read(); if (streamReader.TokenType != JsonTokenType.PropertyName || !streamReader.Match(EventTypePropertyNameString)) { throw new JsonException($"Expected to find the {EventTypePropertyNameString} property."); } streamReader.Read(); if (streamReader.TokenType != JsonTokenType.String) { throw new JsonException($"Expected the {EventTypePropertyNameString} property to be a string."); } }
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)); }
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)); }
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)); }
/// <summary> /// Read the event sequence number from the <see cref="Utf8JsonStreamReader"/>. /// </summary> /// <param name="streamReader">The stream reader from which to read the event sequence number.</param> /// <returns>The event sequence number.</returns> public static long ReadEventSequenceNumber(ref Utf8JsonStreamReader streamReader) { streamReader.Read(); if (streamReader.TokenType != JsonTokenType.PropertyName || !streamReader.Match(EventSequenceNumberPropertyNameString)) { throw new JsonException($"Expected to find the {EventSequenceNumberPropertyNameString} property."); } streamReader.Read(); if (streamReader.TokenType != JsonTokenType.Number) { throw new JsonException($"Expected the {EventSequenceNumberPropertyNameString} property to be a number."); } return(streamReader.GetInt64()); }
private static void ConsumeToken(ref Utf8JsonStreamReader reader, JsonTokenType expected) { if (reader.Read() && expected == reader.TokenType) { return; } ThrowUnexpectedTokenException(ref reader, expected); }
private static void ConsumeToken(ref Utf8JsonStreamReader reader, JsonTokenType expected) { if (reader.Read() && expected == reader.TokenType) { return; } throw new WorkloadManifestFormatException(Strings.ExpectedTokenAtOffset, expected, reader.TokenStartIndex); }
private static bool ReadBool(ref Utf8JsonStreamReader reader) { if (reader.Read() && reader.TokenType.IsBool()) { return(reader.GetBool()); } throw new WorkloadManifestFormatException(Strings.ExpectedBoolAtOffset, reader.TokenStartIndex); }
private static string ReadString(ref Utf8JsonStreamReader reader) { if (reader.Read() && reader.TokenType == JsonTokenType.String) { return(reader.GetString()); } throw new WorkloadManifestFormatException(Strings.ExpectedStringAtOffset, reader.TokenStartIndex); }
/// <summary> /// Read to the end of the event, having read to the end of the payload. /// </summary> /// <param name="streamReader">The <see cref="Utf8JsonStreamReader"/> from which to read the payload.</param> public static void ReadToEndOfEvent(ref Utf8JsonStreamReader streamReader) { // Skip to the end of the event streamReader.Read(); if (streamReader.TokenType != JsonTokenType.EndObject) { throw new JsonException("Expected to see the end of the Event object."); } }
private static IReadOnlyList <DocumentId> ReadFirstDocumentIdInBlock(ref Utf8JsonStreamReader reader) { var firstDocumentIdInBlock = new List <DocumentId>(); H.ReadToken(ref reader, JsonTokenType.StartArray); while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) { firstDocumentIdInBlock.Add(new DocumentId(reader.GetUInt32())); } return(firstDocumentIdInBlock); }
private static bool FindDocumentsProperty(ref Utf8JsonStreamReader streamReader) { while (streamReader.TokenType != JsonTokenType.PropertyName || !streamReader.Match(DocumentsName)) { if (!streamReader.Read()) { return(false); } } return(true); }
private static long ReadInt64(ref Utf8JsonStreamReader reader) { if (reader.Read() && reader.TokenType.IsInt()) { if (reader.TryGetInt64(out long value)) { return(value); } } throw new WorkloadManifestFormatException(Strings.ExpectedIntegerAtOffset, reader.TokenStartIndex); }
private static IList <DocumentMetadata> ReadDocumentsMetadata(ref Utf8JsonStreamReader reader) { var docs = new List <DocumentMetadata>(); JsonReaderHelper.ReadToken(ref reader, JsonTokenType.StartArray); while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) { var doc = ReadDocumentMetadata(ref reader); docs.Add(doc); } return(docs); }
private static bool FindDocumentsArray(ref Utf8JsonStreamReader streamReader) { streamReader.Read(); return(streamReader.TokenType == JsonTokenType.StartArray); }