public static TokenSet DeserializeTokenSet(this DeserializeContext context, ref ReadOnlySpan <byte> buffer) { var builder = new TokenSet.Builder(); var count = context.ReadInt32(ref buffer); for (var i = 0; i < count; i++) { var word = context.ReadString(ref buffer); builder.Insert(word); } return(builder.Root); }
public override Index Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { InvertedIndex?invertedIndex = null; Dictionary <string, Vector>?fieldVectors = null; Pipeline? pipeline = null; IEnumerable <string>?fields = null; var tokenSetBuilder = new TokenSet.Builder(); if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException("An index can only be deserialized from an object."); } reader.Read(); while (reader.AdvanceTo(JsonTokenType.PropertyName, JsonTokenType.EndObject) != JsonTokenType.EndObject) { string propertyName = reader.ReadValue <string>(options); switch (propertyName) { case "version": var parsedVersion = Version.Parse(reader.ReadValue <string>(options)); if (parsedVersion.Major != Version.Major || parsedVersion.Minor != Version.Minor) { System.Diagnostics.Debug.Write($"Version mismatch when loading serialised index. Current version of Lunr '{VersionString}' does not match serialized index '{parsedVersion}'"); } break; case "invertedIndex": invertedIndex = reader.ReadValue <InvertedIndex>(options); break; case "fieldVectors": fieldVectors = reader.ReadDictionaryFromKeyValueSequence <Vector>(options); break; case "pipeline": pipeline = new Pipeline(reader.ReadArray <string>(options)); break; case "fields": fields = reader.ReadArray <string>(options); break; } } if (invertedIndex is null) { throw new JsonException("Serialized index is missing invertedIndex."); } if (fieldVectors is null) { throw new JsonException("Serialized index is missing fieldVectors."); } if (pipeline is null) { throw new JsonException("Serialized index is missing a pipeline."); } if (fields is null) { throw new JsonException("Serialized index is missing a list of fields."); } foreach (string term in invertedIndex.Keys) { tokenSetBuilder.Insert(term); } tokenSetBuilder.Finish(); return(new Index(invertedIndex, fieldVectors, tokenSetBuilder.Root, fields, pipeline)); }