[fix]修正UdpServer在接收广播时连续启动接收的错误,在StarAgent中,此时可能收到广播包,SocketFlags是Broadcast,需要清空,否则报错“参考的对象类型不支持尝试的操作”; 无需设置SocketOptionName.PacketInformation,在ReceiveMessageFromAsync时会自动设置,并且支持ipv6;
石头 编写于 2024-10-10 00:36:00 石头 提交于 2024-10-10 00:45:43
X
using System;
using System.Collections.Generic;
using NewLife;
using NewLife.Serialization;
using Xunit;

namespace XUnitTest.Serialization;

public class JsonParserTests
{
    [Fact]
    public void Decode()
    {
        var obj = new { Name = "NewLife", Version = "5.0" };
        var json = obj.ToJson();

        var jp = new JsonParser(json);
        var rs = jp.Decode();
        Assert.NotNull(rs);

        var dic = rs as IDictionary<String, Object>;
        Assert.NotNull(dic);
        Assert.Equal("NewLife", dic["Name"]);
        Assert.Equal("5.0", dic["Version"]);
    }

    [Fact]
    public void DecodeAsDictionary()
    {
        var json = """{"Name":"NewLife","Version":"5.0"}""";

        var dic = JsonParser.Decode(json);
        Assert.NotNull(dic);
        Assert.Equal("NewLife", dic["Name"]);
        Assert.Equal("5.0", dic["Version"]);
    }

    [Fact]
    public void DecodeError()
    {
        var json = """{"Name":"NewLife","Version":"5.0}""";

        var ex = Assert.Throws<XException>(() => JsonParser.Decode(json));
        Assert.Equal("Reached the end of the string while parsing it [5.0}]", ex.Message);
    }

    [Fact]
    public void DecodeString()
    {
        var json = "NewLife";

        var ex = Assert.Throws<XException>(() => JsonParser.Decode(json));
        Assert.Equal("Non standard Json string [NewLife]", ex.Message);
    }

    [Fact]
    public void DecodeString2()
    {
        var json = " {\"name\":\"NewLife\"}";

        var dic = JsonParser.Decode(json);
        Assert.NotNull(dic);
    }

    [Fact]
    public void DecodeString3()
    {
        var json = " \r\n  \t";

        var dic = JsonParser.Decode(json);
        Assert.Null(dic);
    }
}