예제 #1
0
    internal static JsonObjectNode CreateModel(DateTime now)
    {
        var json = new JsonObjectNode();

        json.SetValue("now", now);
        json.SetValue("str-a", "abcdefg");
        json.SetValue("str-a", "hijklmn");
        json.SetValue("str-b", "opq");
        json.SetRange(new Dictionary <string, string>
        {
            { "str-b", "rst" },
            { "str-c", "uvw" },
            { "str-d", "xyz" },
            { "str-e", "$$$" }
        });
        json.Remove("str-e");
        json.SetValue("str-d", "0123456789");
        json.SetValue("num", 123);
        json.SetJavaScriptDateTicksValue("ticks", now);
        json.SetValue("props", json);
        json.SetValue("arr", new JsonArrayNode());
        json.SetRange(json);
        return(json);
    }
예제 #2
0
    /// <inheritdoc />
    public override ChemicalElement Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.Null)
        {
            return(null);
        }
        try
        {
            if (reader.TokenType == JsonTokenType.Number)
            {
                return(ChemicalElement.Get(reader.GetInt32()));
            }
        }
        catch (FormatException ex)
        {
            throw new JsonException("Expect a valid element symbol or the atomic numbers.", ex);
        }

        if (reader.TokenType == JsonTokenType.String)
        {
            return(ChemicalElement.Get(reader.GetString()));
        }
        if (reader.TokenType != JsonTokenType.StartObject)
        {
            throw new JsonException("The format is not correct.", new FormatException("The value should be a date time JSON token format."));
        }
        var obj = new JsonObjectNode();

        obj.SetRange(ref reader);
        var             z   = obj.TryGetInt32Value("number") ?? obj.TryGetInt32Value("z");
        var             s   = obj.TryGetStringValue("symbol")?.Trim();
        var             n   = obj.TryGetStringValue("name_en")?.Trim() ?? obj.TryGetStringValue("name")?.Trim();
        var             w   = obj.TryGetDoubleValue("weight");
        ChemicalElement ele = null;

        if (z.HasValue && z != 0)
        {
            if (z.Value < 1)
            {
                return(null);
            }
            ele = ChemicalElement.Get(z.Value);
        }
        else if (!string.IsNullOrEmpty(s))
        {
            ele = ChemicalElement.Get(s);
        }
        else if (!string.IsNullOrEmpty(n))
        {
            ele = ChemicalElement.Where(ele => n.Equals(ele.EnglishName, StringComparison.Ordinal)).FirstOrDefault();
            if (ele is null && n.Length < 4)
            {
                ele = ChemicalElement.Get(n);
            }
        }
        else
        {
            return(null);
        }

        if (ele != null &&
            (!z.HasValue || ele.AtomicNumber == z.Value) &&
            (string.IsNullOrEmpty(s) || s.Equals(ele.Symbol, StringComparison.OrdinalIgnoreCase)) &&
            (!w.HasValue || (!ele.HasAtomicWeight && double.IsNaN(w.Value)) || (ele.HasAtomicWeight && !double.IsNaN(w.Value) && Math.Abs(ele.AtomicWeight - w.Value) < 0.000000001)))
        {
            return(ele);
        }

        if (!z.HasValue || z.Value < 1 || string.IsNullOrEmpty(s))
        {
            return(null);
        }
        return(string.IsNullOrEmpty(n)
            ? new ChemicalElement(z.Value, s, null, w.HasValue ? w.Value : double.NaN)
            : new ChemicalElement(z.Value, s, n, true, w.HasValue ? w.Value : double.NaN));
    }
예제 #3
0
    /// <summary>
    /// Returns the typed collection.
    /// </summary>
    /// <typeparam name="T">The type of each instance in the collection.</typeparam>
    /// <param name="creator">The instance factory.</param>
    /// <param name="propertyNames">The optional property names to map.</param>
    /// <returns>A typed collection based on the data parsed.</returns>
    public IEnumerable <T> ConvertTo <T>(Func <IReadOnlyList <string>, T> creator, IEnumerable <string> propertyNames = null)
    {
        var type  = typeof(T);
        var props = propertyNames?.Select(ele =>
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(ele))
                {
                    return(type.GetProperty(ele));
                }
            }
            catch (AmbiguousMatchException)
            {
            }

            return(null);
        })?.ToList();

        if (props != null && props.Count == 0)
        {
            props = null;
        }
        if (creator == null)
        {
            if (props == null)
            {
                if (type == typeof(JsonArrayNode))
                {
                    foreach (var item in this)
                    {
                        var instance = new JsonArrayNode();
                        instance.AddRange(item);
                        yield return((T)(object)instance);
                    }
                }
                else if (type == typeof(System.Text.Json.Nodes.JsonArray))
                {
                    foreach (var item in this)
                    {
                        var instance = new System.Text.Json.Nodes.JsonArray();
                        foreach (var ele in item)
                        {
                            instance.Add(ele);
                        }

                        yield return((T)(object)instance);
                    }
                }

                yield break;
            }
            else
            {
                if (type == typeof(JsonObjectNode))
                {
                    foreach (var item in this)
                    {
                        var instance = new JsonObjectNode();
                        instance.SetRange(item, propertyNames);
                        yield return((T)(object)instance);
                    }

                    yield break;
                }
            }
        }

        foreach (var item in this)
        {
            var instance = ObjectConvert.Invoke(item, creator, props);
            if (instance == null)
            {
                continue;
            }
            yield return(instance);
        }
    }