2015-12-19 17 views
1

我试图序列化,我从System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()Newtonsoft JSON:我如何序列化不是字符串,整数,等...(如NetworkInterface的)

GetAllNetworkInterfaces()获取信息返回集合一些.NET对象NetworkInterface对象。序列化它基本上没有返回有用的信息。

{ 
     "IsReceiveOnly": false, 
     "Description": "TAP-Windows Adapter V9", 
     "SupportsMulticast": true, 
     "NetworkInterfaceType": 6, 
     "OperationalStatus": 2, 
     "Speed": 100000000, 
     "Id": "{BCE79C39-232A-4483-AF64-1D26E3AA7C83}", 
     "Name": "Ethernet" 
    }, 
    etc... 

每个NetworkInterface对象具有如下功能:GetIPProperties它返回一个IPInterfaceProperties对象又具有一个GetIPv4Properties()函数返回一个IPv4InterfaceProperties对象。

我看到了许多向类属性添加属性的示例,告诉Newtonsoft JSON序列化非正常字符串,整数,布尔等等的属性,甚至序列化来自函数调用的结果,但我难倒我如何可以指出现有的.NET对象。

我现在也很痛苦的解决方法是创建自己的MyNetworkInterface类我想要的属性,然后手动分配样特性:

for each adapter as NetworkInterface in NetworkInterface.GetAllNetworkInterfaces() 
    my_custom_network_interface_object.some_property = real_network_interface_object.some_property 
    dim ip_properties() as IPInterfaceProperties = real_network_interface_object.GetIPProperties() 
    dim gateway_info() as GatewayIPAddressInformationCollection = ip_properties.GatewayAddresses 
    for gw in gateway_info 
    my_custom_network_interface_object.gateway.add(gw.ToString()) 

好像应该有更好的方式,和我只是想念它。

从Python的背景来看,我觉得完全迷失在VB.NET中。 :)

回答

1

要正确地做到这一点,你首先需要定义一些自定义转换器类:

Class NetworkInterfaceJsonConverter 
    Inherits JsonConverter 

    Public Overrides Function CanConvert(objectType As Type) As Boolean 
     Return GetType(NetworkInterface).IsAssignableFrom(objectType) 
    End Function 

    Public Overrides Sub WriteJson(writer As JsonWriter, value As Object, serializer As JsonSerializer) 

     ' prevent infinite recursion by temporarily removing this converter 
     serializer.Converters.Remove(Me) 

     ' build a jObject, starting with the properties we already have 
     Dim jObj = JObject.FromObject(value, serializer) 

     ' add the results from the GetIPProperties call as a nested object 
     Dim adapter As NetworkInterface = value 
     Dim properties = adapter.GetIPProperties() 
     jObj.Add("IPProperties", JObject.FromObject(properties)) 

     ' we can add the GetIPv4Properties results also 
     If adapter.Supports(NetworkInterfaceComponent.IPv4) Then 
      Dim ipv4Properties = properties.GetIPv4Properties() 
      jObj.Add("IPv4Properties", JObject.FromObject(ipv4Properties)) 
     End If 

     ' you can expand this here if you need results from other method calls 

     ' finally, write the jObject to the writer 
     jObj.WriteTo(writer) 

     ' restore the converter 
     serializer.Converters.Add(Me) 

    End Sub 

    Public Overrides Function ReadJson(reader As JsonReader, objectType As Type, existingValue As Object, serializer As JsonSerializer) As Object 
     Throw New NotImplementedException ' You can implement this if you have read usages 
    End Function 
End Class 

Class IPAddressJsonConverter 
    Inherits JsonConverter 

    Public Overrides Function CanConvert(objectType As Type) As Boolean 
     Return GetType(IPAddress).IsAssignableFrom(objectType) 
    End Function 

    Public Overrides Sub WriteJson(writer As JsonWriter, value As Object, serializer As JsonSerializer) 

     ' this one's pretty simple, but it's necessary to get the correct output 
     Dim ipAddress As IPAddress = value 
     writer.WriteValue(ipAddress.ToString()) 
    End Sub 

    Public Overrides Function ReadJson(reader As JsonReader, objectType As Type, existingValue As Object, serializer As JsonSerializer) As Object 
     Throw New NotImplementedException ' You can implement this if you have read usages 
    End Function 
End Class 

然后你就可以将它们备份到默认设置:

JsonConvert.DefaultSettings = 
    Function() 
     Dim settings = New JsonSerializerSettings() 
     settings.Converters.Add(New NetworkInterfaceJsonConverter()) 
     settings.Converters.Add(New IPAddressJsonConverter()) 
     Return settings 
    End Function 

然后你可以正常转换:

Dim interfaces = NetworkInterface.GetAllNetworkInterfaces() 
Dim json = JsonConvert.SerializeObject(interfaces, Formatting.Indented) 
Console.WriteLine(json) 
+0

这正是我所错过的。谢谢你+马特约翰逊。 –

+0

我一直在'jObj.Add(“IPProperties”,JObject.FromObject(properties))''行得到一个System.NullReferenceException异常。如果我调用'Debug.WriteLine(JObject.FromObject(properties))',它会引发同样的错误。 'properties'肯定是一个有效的对象,因为我可以'debug.writeline(properties.blah)'这里blah是可以在对象上调用的每个属性和函数。 –

+0

嗯......也许这是窒息在另一个子属性。你添加了两个转换器吗?可能还需要另一个,具体取决于计算机上适配器数据的外观。上述作品在我的笔记本电脑上,但每台机器可能会有所不同。 –

相关问题