public static FastSerializableDictionary <TKey, TValue> Deserialize(FastSerializableKeys <TKey> keys, ReadOnlySequence <byte> stream) { var values = ArrayPool <TValue> .Shared.Rent(keys.Length); var reader = new Utf8JsonReader(stream); if (!reader.Read()) { throw new FormatException("Invalid start"); } if (reader.TokenType != JsonTokenType.StartArray) { throw new FormatException("Invalid start"); } var index = 0; while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndArray) { break; } var value = reader.FastReadObject <TValue>(); values[index++] = value; } if (reader.TokenType != JsonTokenType.EndArray) { throw new FormatException("Unexpected end of input"); } return(new FastSerializableDictionary <TKey, TValue>(keys, values)); }
public static async IAsyncEnumerable <ResultRow> Reader(PipeReader reader) { var keys = new FastSerializableKeys <string>(); while (true) { var result = await reader.ReadAsync().ConfigureAwait(false); var buffer = result.Buffer; SequencePosition?position; do { position = buffer.PositionOf((byte)'\n'); if (position == null) { continue; } var row = DeserializeRow(keys, buffer.Slice(0, position.Value)); yield return(row); buffer = buffer.Slice(buffer.GetPosition(1, position.Value)); } while (position != null); reader.AdvanceTo(buffer.Start, buffer.End); if (result.IsCompleted) { break; } } await reader.CompleteAsync().ConfigureAwait(false); }
private static ResultRow DeserializeRow(FastSerializableKeys <string> keys, ReadOnlySequence <byte> line) { var fields = new FastSerializableDictionary <string, object>(keys); var reader = new Utf8JsonReader(line); var foundStart = false; while (reader.Read()) { if (!foundStart) { if (reader.TokenType != JsonTokenType.StartObject) { throw new FormatException(); } foundStart = true; continue; } if (reader.TokenType == JsonTokenType.EndObject) { if (reader.BytesConsumed != line.Length) { throw new FormatException(); } break; } if (reader.TokenType != JsonTokenType.PropertyName) { throw new ArgumentOutOfRangeException(); } var key = reader.GetString(); if (!reader.Read()) { throw new FormatException(); } fields[key] = reader.FastReadObject <object>(); } return(new ResultRow(fields)); }
public static FastSerializableDictionary <TKey, TValue> FromDictionary(FastSerializableKeys <TKey> keys, IReadOnlyDictionary <TKey, TValue> source) { var indices = new Dictionary <TKey, int>(); var count = 0; foreach (var key in source.Keys) { var index = keys.GetOrAddKey(key); indices.Add(key, index); count = count < index ? index : count; } var values = ArrayPool <TValue> .Shared.Rent(count); foreach (var kvp in source) { values[indices[kvp.Key]] = kvp.Value; } return(new FastSerializableDictionary <TKey, TValue>(keys, values)); }
private static ResultRow DeserializeRow(FastSerializableKeys <string> keys, ReadOnlySequence <byte> line) { var fields = FastSerializableDictionary <string, object> .Deserialize(keys, line); return(new ResultRow(fields)); }
private static FastSerializableKeys <string> DeserializeKeys(ReadOnlySequence <byte> slice) { return(FastSerializableKeys <string> .Deserialize(slice)); }
private FastSerializableDictionary(FastSerializableKeys <TKey> keys, TValue[] values) { _keys = keys; _values = values; }
public FastSerializableDictionary(FastSerializableKeys <TKey> keys) { _keys = keys; _values = EmptyValues; }