JSON反序列化为构造的受保护的setter数组

时间:2014-09-20 15:04:24

标签: c# arrays json serialization json.net

我使用Newtonsoft JSON来序列化/反序列化我的对象。其中一个包含一个带有受保护setter的数组,因为构造函数本身构建了数组,只有成员被操作。

这可以没有问题地序列化,但是当反序列化时,它被忽略,因为它不是公共的。我尝试了一个自定义转换器,它也没有被调用,因为它不公开。

这是一个最小化的例子:

public static class TestCoordsDeserialization
{
    private class Coords
    {
        public Double X { get; set; }
        public Double Y { get; set; }
        public Double Z { get; set; }
        public Double A { get; set; }
    }

    private class Engine
    {
        public string Text { get; set; }
        public int Id { get; set; }
        public Coords[] Outs { get; protected set; }

        public Engine()
        {
            this.Outs = new Coords[3];
            for (int i = 0; i < this.Outs.Length; i++)
            {
                this.Outs[i] = new Coords();
            }
        }
    }

    public static void Test()
    {
        Engine e = new Engine();
        e.Id = 42;
        e.Text = "MyText";
        e.Outs[0] = new Coords() { A = 0, X = 10, Y = 11, Z = 0 };
        e.Outs[1] = new Coords() { A = 0, X = 20, Y = 22, Z = 0 };
        e.Outs[2] = new Coords() { A = 0, X = 30, Y = 33, Z = 0 };
        string json = JsonConvert.SerializeObject(e);
        Console.WriteLine(json); //{"Text":"MyText","Id":42,"Positions":{"Test":9,"Outs":[{"X":10.0,"Y":11.0,"Z":0.0,"A":0.0},{"X":20.0,"Y":22.0,"Z":0.0,"A":0.0},{"X":30.0,"Y":33.0,"Z":0.0,"A":0.0}]}}
        Engine r = JsonConvert.DeserializeObject<Engine>(json);
        double value = r.Outs[1].X; // should be '20.0'
        Console.WriteLine(value);
        Debugger.Break();
    }
}

如何让value成为 20.0

2 个答案:

答案 0 :(得分:16)

使用Outs属性标记[JsonProperty]

    private class Engine
    {
        public string Text { get; set; }
        public int Id { get; set; }
        [JsonProperty]  // Causes the protected setter to be called on deserialization.
        public Coords[] Outs { get; protected set; }

        public Engine()
        {
            this.Outs = new Coords[3];
            for (int i = 0; i < this.Outs.Length; i++)
            {
                this.Outs[i] = new Coords();
            }
        }
    }

答案 1 :(得分:0)

尝试一下:

var contractResolver = new DefaultContractResolver();
contractResolver.DefaultMembersSearchFlags |= BindingFlags.NonPublic;
Engine r = JsonConvert.DeserializeObject<Engine>(json), new JsonSerializerSettings
            {
                ContractResolver = contractResolver
            });

使用上述代码,您将收到以下警告消息:

  

'DefaultContractResolver.DefaultMembersSearchFlags'已过时:   'DefaultMembersSearchFlags已过时。修改成员   从DefaultContractResolver继承并继承   改为使用GetSerializableMembers方法。'

要解决此问题,您可以改用以下解决方案:

创建一个继承自DefaultContractResolver的类以获取非公共属性:

public class CustomContractResolver : DefaultContractResolver
    {
        protected override JsonProperty CreateProperty(
            MemberInfo member,
            MemberSerialization memberSerialization)
        {
            var prop = base.CreateProperty(member, memberSerialization);

            if (!prop.Writable)
            {
                var property = member as PropertyInfo;
                if (property != null)
                {
                    var hasNonPublicSetter = property.GetSetMethod(true) != null;
                    prop.Writable = hasNonPublicSetter;
                }
            }

            return prop;
        }
    }

最后按如下所示使用它:

var contractResolver = new CustomContractResolver();
Engine r = JsonConvert.DeserializeObject<Engine>(json), new JsonSerializerSettings
            {
                ContractResolver = contractResolver
            });