internal static ScoringFunction DeserializeScoringFunction(JsonElement element)
        {
            if (element.TryGetProperty("type", out JsonElement discriminator))
            {
                switch (discriminator.GetString())
                {
                case "distance": return(DistanceScoringFunction.DeserializeDistanceScoringFunction(element));

                case "freshness": return(FreshnessScoringFunction.DeserializeFreshnessScoringFunction(element));

                case "magnitude": return(MagnitudeScoringFunction.DeserializeMagnitudeScoringFunction(element));

                case "tag": return(TagScoringFunction.DeserializeTagScoringFunction(element));
                }
            }
            string type      = default;
            string fieldName = default;
            double boost     = default;
            ScoringFunctionInterpolation?interpolation = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("type"))
                {
                    type = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("fieldName"))
                {
                    fieldName = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("boost"))
                {
                    boost = property.Value.GetDouble();
                    continue;
                }
                if (property.NameEquals("interpolation"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    interpolation = property.Value.GetString().ToScoringFunctionInterpolation();
                    continue;
                }
            }
            return(new ScoringFunction(type, fieldName, boost, interpolation));
        }
Esempio n. 2
0
        internal static FreshnessScoringFunction DeserializeFreshnessScoringFunction(JsonElement element)
        {
            FreshnessScoringFunction result = new FreshnessScoringFunction();

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("freshness"))
                {
                    result.Parameters = FreshnessScoringParameters.DeserializeFreshnessScoringParameters(property.Value);
                    continue;
                }
                if (property.NameEquals("type"))
                {
                    result.Type = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("fieldName"))
                {
                    result.FieldName = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("boost"))
                {
                    result.Boost = property.Value.GetDouble();
                    continue;
                }
                if (property.NameEquals("interpolation"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    result.Interpolation = property.Value.GetString().ToScoringFunctionInterpolation();
                    continue;
                }
            }
            return(result);
        }