Original:
Http://stackoverflow.com/questions/18668617/json-net-error-getting-value-from-scopeid-on-system-net-ipaddress
classipaddressconverter:jsonconverter{ Public Override BOOLCanconvert (Type objectType) {return(ObjectType = =typeof(IPAddress)); } Public Override voidWritejson (Jsonwriter writer,Objectvalue, Jsonserializer serializer) {IPAddress IP=(IPAddress) value; Writer. WriteValue (IP. ToString ()); } Public Override ObjectReadjson (Jsonreader Reader, Type ObjectType,ObjectExistingvalue, Jsonserializer Serializer) {Jtoken token=jtoken.load (reader); returnIpaddress.parse (token. value<string>()); }}classipendpointconverter:jsonconverter{ Public Override BOOLCanconvert (Type objectType) {return(ObjectType = =typeof(IPEndPoint)); } Public Override voidWritejson (Jsonwriter writer,Objectvalue, Jsonserializer serializer) {IPEndPoint EP=(ipendpoint) value; Writer. Writestartobject (); Writer. Writepropertyname ("Address"); Serializer. Serialize (writer, ep. Address); Writer. Writepropertyname ("Port"); Writer. WriteValue (ep. Port); Writer. Writeendobject (); } Public Override ObjectReadjson (Jsonreader Reader, Type ObjectType,ObjectExistingvalue, Jsonserializer Serializer) {Jobject Jo=jobject.load (reader); IPAddress Address= jo["Address"]. Toobject<ipaddress>(serializer); intPort = jo["Port"]. value<int>(); return NewIPEndPoint (address, port); }} Public classprogram{Static voidMain (string[] args) {ipendpoint[] Endpoints=Newipendpoint[] {NewIPEndPoint (Ipaddress.parse ("8.8.4.4"), -), NewIPEndPoint (Ipaddress.parse ("2001:db8::ff00:42:8329"),Bayi) }; Jsonserializersettings Settings=Newjsonserializersettings (); Settings. Converters.add (NewIpaddressconverter ()); Settings. Converters.add (NewIpendpointconverter ()); Settings. Formatting=formatting.indented; stringJSON =jsonconvert.serializeobject (endpoints, settings); Console.WriteLine (JSON); Ipendpoint[] Endpoints2=Jsonconvert.deserializeobject<IPEndPoint[]>(JSON, settings); foreach(IPEndPoint EPinchendpoints2) {Console.WriteLine (); Console.WriteLine ("addressfamily:"+EP. AddressFamily); Console.WriteLine ("Address:"+EP. Address); Console.WriteLine ("Port:"+EP. Port); } }}
Json.NET serialization of IPAddress or IPEndPoint