Example #1
0
        /// <summary>
        /// Parses query arguments.
        /// </summary>
        /// <param name="json">The JSON object.</param>
        /// <returns>An instance of query arguments</returns>
        public static QueryArgs Parse(JsonObjectNode json)
        {
            var q = new QueryArgs
            {
                NameQuery   = json.GetStringValue("q") ?? json.GetStringValue("name"),
                NameExactly = json.TryGetBooleanValue("eqname") ?? false,
                Offset      = json.TryGetInt32Value("offset") ?? 0,
                Count       = json.TryGetInt32Value("count") ?? ResourceEntityExtensions.PageSize,
                State       = json.TryGetEnumValue <ResourceEntityStates>("state", true) ?? ResourceEntityStates.Normal,
                Order       = json.TryGetEnumValue <ResourceEntityOrders>("order", true) ?? ResourceEntityOrders.Default
            };

            if (!json.ContainsKey("offset") && json.TryGetInt32Value("pgno", out var pgIndex) && pgIndex > 0)
            {
                q.Offset = pgIndex * q.Count;
            }
            return(q);
        }
Example #2
0
 /// <summary>
 /// Indicates whether this instance and a specified object are equal.
 /// </summary>
 /// <param name="other">The object to compare with the current instance.</param>
 /// <returns>true if obj and this instance represent the same value; otherwise, false.</returns>
 public bool Equals(JsonObjectNode other)
 {
     if (other is null)
     {
         return(false);
     }
     return(AtomicNumber == other.TryGetInt32Value("number") &&
            Symbol == other.TryGetStringValue("symbol"));
 }
Example #3
0
    public void TestJsonAttribute()
    {
        var jArr = new JsonArrayNode
        {
            "abc",
            true,
            "defg"
        };

        jArr.AddNull();
        jArr.Add(1234);
        var jObj = new JsonObjectNode();

        jObj.SetValue("hijk", jArr);
        jObj.SetValue("lmn", "opq");
        jObj.SetValue("rst", "56789K");
        Assert.AreEqual(56789000, jObj.TryGetInt32Value("rst"));
        Assert.AreEqual(56789000, jObj["rst"].GetInt64());
        jObj.SetValue("rst", 56789);
        jObj.SetValue("uvw", false);
        jObj.SetNullValue("x");
        jObj.SetValue("y", new JsonObjectNode());
        jObj.SetValue("z", 0);

        var model = new JsonAttributeTestModel
        {
            A = new DateTime(2020, 1, 1),
            B = new DateTime(2020, 1, 2),
            C = new DateTime(2020, 1, 3),
            D = new DateTime(2020, 1, 4),
            E = new DateTime(2020, 1, 5),
            F = jObj,
            G = jArr,
            H = new List <string>
            {
                "!@#$%^",
                null,
                "&()_+-="
            },
            I = new HashSet <string>
            {
                "***",
                string.Empty,
                "|||||||"
            }
        };
        var str    = JsonSerializer.Serialize(model);
        var model2 = JsonSerializer.Deserialize <JsonAttributeTestModel>(str);

        Assert.AreEqual(model.A, model2.A);
        Assert.AreEqual(model.B, model2.B);
        Assert.AreEqual(model.C, model2.C);
        Assert.AreEqual(model.D, model2.D);
        Assert.AreEqual(model.E, model2.E);
        Assert.AreEqual(model.F.Count, model2.F.Count);
        Assert.AreEqual(model.G.Count, model2.G.Count);
        Assert.AreEqual(model.H.Count, model2.H.Count);
        Assert.AreEqual(model.I.Count - 1, model2.I.Count);

        model = new JsonAttributeTestModel
        {
            A = new DateTime(2020, 1, 1),
            B = null,
            C = new DateTime(2020, 1, 3),
            D = null,
            E = new DateTime(2020, 1, 5)
        };
        str    = JsonSerializer.Serialize(model);
        model2 = JsonSerializer.Deserialize <JsonAttributeTestModel>(str);
        Assert.AreEqual(model.A, model2.A);
        Assert.AreEqual(model.B, model2.B);
        Assert.AreEqual(model.C, model2.C);
        Assert.AreEqual(model.D, model2.D);
        Assert.AreEqual(model.E, model2.E);
        Assert.AreEqual(null, model2.F);
        Assert.AreEqual(null, model2.G);
        Assert.AreEqual(null, model2.H);
        Assert.AreEqual(null, model2.I);

        str    = @"{
""H"": "":,.;/| "",
""I"": ""abcdefg hijklmn    opq\trst\n\nuvw\rxyz"",
""J"": ""123456"",
""K"": ""[11.1, 76.9)"",
""L"": ""[, 999999999999)"",
""M"": ""[3.1415926, " + Numbers.InfiniteSymbol + @"]"",
""N"": ""[3.6.0, 5.0.0)"",
""O"": 2,
""P"": ""unauthorized"",
""R"": ""#CC3333"",
""S"": ""rgba(240, 240, 16, 0.8)"",
""T"": { ""b"": 255 },
""V"": ""unknown"",
""W"": 17.24
}";
        model2 = JsonSerializer.Deserialize <JsonAttributeTestModel>(str);
        Assert.AreEqual(1, model2.H.Count);
        Assert.AreEqual(":,.;/| ", model2.H[0]);
        Assert.AreEqual(6, model2.I.Count);
        Assert.AreEqual((uint)123456, model2.J);
        Assert.IsNotNull(model2.K);
        Assert.AreEqual(12, model2.K.MinValue);
        Assert.IsFalse(model2.K.LeftOpen);
        Assert.AreEqual(76, model2.K.MaxValue);
        Assert.IsFalse(model2.K.RightOpen);
        Assert.IsFalse(model2.K.IsInInterval(11));
        Assert.IsTrue(model2.K.IsInInterval(12));
        Assert.IsTrue(model2.K.IsInInterval(76));
        Assert.IsFalse(model2.K.IsInInterval(77));
        Assert.IsNotNull(model2.L);
        Assert.IsNull(model2.L.MinValue);
        Assert.IsTrue(model2.L.LeftOpen);
        Assert.IsFalse(model2.L.LeftBounded);
        Assert.AreEqual(999999999999, model2.L.MaxValue);
        Assert.IsTrue(model2.L.RightOpen);
        Assert.IsTrue(model2.L.RightBounded);
        Assert.IsNotNull(model2.M);
        Assert.AreEqual(3.1415926, model2.M.MinValue);
        Assert.IsFalse(model2.M.LeftOpen);
        Assert.IsTrue(model2.M.LeftBounded);
        Assert.IsTrue(model2.M.RightOpen);
        Assert.IsFalse(model2.M.RightBounded);
        Assert.IsFalse(model2.M.IsInInterval(3));
        Assert.IsTrue(model2.M.IsInInterval(3.1415926));
        Assert.IsTrue(model2.M.IsInInterval(200000000000000));
        Assert.IsNotNull(model2.N);
        Assert.AreEqual("3.6.0", model2.N.MinValue);
        Assert.IsFalse(model2.N.LeftOpen);
        Assert.IsTrue(model2.N.LeftBounded);
        Assert.AreEqual("5.0.0", model2.N.MaxValue);
        Assert.IsTrue(model2.N.RightOpen);
        Assert.IsTrue(model2.N.RightBounded);
        Assert.IsFalse(model2.N.IsInInterval("3.0.0.0"));
        Assert.IsTrue(model2.N.IsInInterval("3.6.0.0"));
        Assert.IsTrue(model2.N.IsInInterval("4.2.0.0"));
        Assert.IsFalse(model2.N.IsInInterval("5.0.0.0"));
        Assert.IsFalse(model2.N.IsInInterval("6.0.0.0"));
        Assert.IsNotNull(model2.N.MinVersion);
        Assert.AreEqual(6, model2.N.MinVersion.Minor);
        Assert.AreEqual(Data.ChangeErrorKinds.Unauthorized, model2.O);
        Assert.AreEqual(Data.ChangeErrorKinds.Unauthorized, model2.P);
        Assert.AreEqual(Data.ChangeErrorKinds.None, model2.Q);
        Assert.AreEqual(0xCC, model2.R.R);
        Assert.AreEqual(240, model2.S.G);
        Assert.IsTrue(model.S.A <0.9 && model2.S.A> 0.7);
        Assert.AreEqual(255, model2.T.B);
        Assert.IsNull(model2.U);
        Assert.AreEqual(Data.ChangeMethods.Unknown, model2.V);
        Assert.AreEqual(17, model2.W.Degree);
        Assert.IsTrue(model2.W.Arcminute > 0);
        str = JsonSerializer.Serialize(model2);
        Assert.IsNotNull(str);
    }
Example #4
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));
    }