public override IFuzzyQuery Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { if (reader.GetCurrentJsonToken() == JsonToken.Null) { reader.ReadNext(); return(null); } var count = 0; IFuzzyQuery query = null; string name = null; double? boost = null; MultiTermQueryRewrite multiTermQueryRewrite = null; int? prefixLength = null; int? maxExpansions = null; bool? transpositions = null; while (reader.ReadIsInObject(ref count)) { var field = reader.ReadPropertyName(); // ReSharper disable once TooWideLocalVariableScope ArraySegment <byte> fuzzinessSegment = default; var valueCount = 0; while (reader.ReadIsInObject(ref valueCount)) { var property = reader.ReadPropertyNameSegmentRaw(); if (Fields.TryGetValue(property, out var value)) { switch (value) { case 0: { var token = reader.GetCurrentJsonToken(); switch (token) { case JsonToken.String: var valueSegment = reader.ReadStringSegmentUnsafe(); if (valueSegment.IsDateTime(formatterResolver, out var dateTime)) { query = new FuzzyDateQuery { Field = field, Value = dateTime } } ; else { query = new FuzzyQuery { Field = field, Value = valueSegment.Utf8String() } }; break; case JsonToken.Number: query = new FuzzyNumericQuery { Field = field, Value = reader.ReadDouble() }; break; } if (fuzzinessSegment != default) { var fuzzinessReader = new JsonReader(fuzzinessSegment.Array, fuzzinessSegment.Offset); SetFuzziness(ref fuzzinessReader, query, formatterResolver); } break; } case 1: { if (query != null) { SetFuzziness(ref reader, query, formatterResolver); } else { fuzzinessSegment = reader.ReadNextBlockSegment(); } break; } case 2: prefixLength = reader.ReadInt32(); break; case 3: maxExpansions = reader.ReadInt32(); break; case 4: transpositions = reader.ReadBoolean(); break; case 5: var rewriteFormatter = formatterResolver.GetFormatter <MultiTermQueryRewrite>(); multiTermQueryRewrite = rewriteFormatter.Deserialize(ref reader, formatterResolver); break; case 6: name = reader.ReadString(); break; case 7: boost = reader.ReadDouble(); break; } } } } query.PrefixLength = prefixLength; query.MaxExpansions = maxExpansions; query.Transpositions = transpositions; query.Rewrite = multiTermQueryRewrite; query.Name = name; query.Boost = boost; return(query); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var j = JObject.Load(reader); if (!j.HasValues) { return(null); } var firstProp = j.Properties().FirstOrDefault(); if (firstProp == null) { return(null); } var field = firstProp.Name; var jo = firstProp.Value.Value <JObject>(); if (jo == null) { return(null); } JToken v; if (!jo.TryGetValue("value", out v)) { return(null); } IFuzzyQuery fq; if (v.Type == JTokenType.String) { fq = new FuzzyQuery() { Value = GetPropValue <string>(jo, "value"), Fuzziness = GetPropObject <Fuzziness>(jo, "fuzziness") }; } else if (v.Type == JTokenType.Date) { fq = new FuzzyDateQuery() { Value = GetPropValue <DateTime?>(jo, "value"), Fuzziness = GetPropObject <Time>(jo, "fuzziness") }; } else if (v.Type == JTokenType.Integer || v.Type == JTokenType.Float) { fq = new FuzzyNumericQuery() { Value = GetPropValue <double?>(jo, "value"), Fuzziness = GetPropValue <double?>(jo, "fuzziness") }; } else { return(null); } fq.PrefixLength = GetPropValue <int?>(jo, "prefix_length"); fq.MaxExpansions = GetPropValue <int?>(jo, "max_expansions"); fq.Transpositions = GetPropValue <bool?>(jo, "transpositions"); var rewriteString = GetPropValue <string>(jo, "rewrite"); if (!rewriteString.IsNullOrEmpty()) { fq.Rewrite = rewriteString.ToEnum <RewriteMultiTerm>(); } fq.Name = GetPropValue <string>(jo, "_name"); fq.Boost = GetPropValue <double?>(jo, "boost"); fq.Field = field; return(fq); }