简体   繁体   中英

Json deserializing key correctly but not value

I am trying to deserialize a JSON string into object. Following is the class of network interface names and a list of IP address mappings:

[DataContract]
public class NicMappings
{
    public NicMappings()
    {
        this.Ips = new List<IPAddress>();
    }

    [DataMember]
    public string NetworkInterfaceName { get; set; }

    [DataMember]
    public List<IPAddress> Ips { get; }
}

There is another class Settings which contains a list of NicMappings:

[DataContract]
public class Settings
{
    public Settings()
    {
        this.PrivateIps = new List<IPAddress>();
        this.VirtualIps = new List<IPAddress>();
        this.IpRanges = new List<IPPrefix>();
        this.NicMappings = new List<NicMappings>();
    }

    [DataMember]
    public List<IPAddress> PrivateIps { get; private set; }

    [DataMember]
    public List<IPAddress> VirtualIps { get; private set; }

    [DataMember]
    public List<IPPrefix> IpRanges { get; private set; }

    [DataMember]
    public List<NicMappings> NicMappingsList { get; private set; }
}

For testing, I am reading a single NicMapping containing a name and a single IP which gets serialized fine (along with other members of the Settings class). This is the output json:

{"PrivateIps":["10.0.0.1","10.0.0.2","10.0.0.3"],..."NicMappings":[{"NetworkInterfaceName":"Nic","Ips":["127.0.0.1"]}]}

Deserialization is the problem. All the other settings get deserialized fine, except for NicMappings. It deserializes the name fine, but doesn't deserialize the Ips. It doesn't even throw an exception. I added a log to statement to check the count of Ips, it prints 0. This is the code I'm using to deserialize it:

Settings settings = Serializer.GetSettings<Settings>(file);

Serializer is a class which has JsonSerializerSettings object with a binder and some converters defined.

public T GetSettings(string file)
{
    string json = File.ReadAllText(file);
    return Deserialize<T>(json);
}

Next,

List<NicData> nicData = GetNicData(settings.NicMappingsList);

private List<NicData> GetNicData(List<NicMappings> NicMappingsList)
{
    List<NicData> NicDatas = new List<NicData>();
    foreach (NicMappings nicMapping in NicMappingsList)
    {
        NicData nicData = new NicData
        {
            NetworkInterfaceName = nicMapping.NetworkInterfaceName
        };

        foreach (IPAddress ipAddress in nicMapping.Ips)
        {
            nicData.Ips.Add(ipAddress.ToString());
        }

        nicDataList.Add(NicData);
    }

    return nicDataList;
}

The above returns me a list with a single element, with the correct name, but an empty Ips list.

Btw, NicData is the pretty much the same as NicMappings

public class NicData
{
    public NicData()
    {
        Ips = new List<string>();
    }

    public string NetworkInterfaceName { get; set; }

    public List<string> Ips { get; }
}

I'm not sure where to look. Any idea which part of the code should I check for problem? Please let me know if the problem is not clear or you need more info. Thanks!

Try to add a setter:

public List<string> Ips { get; set;}

More info on serialization here

It says:

DO implement a getter and setter on all properties that have Data-MemberAttribute. Data contract serializers require both the getter and the setter for the type to be considered serializable. If the type won't be used in partial trust, one or both of the property accessors can be nonpublic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM