Using the Newtonsoft.Json library, imagine I have got
public class Test
{
public Object Obj { get; set; }
}
Now, attempting to serialize this like so
var json = JsonConvert.SerializeObject(new Test(){ Obj = new Uri(@"http://www.google.com") });
…will give me the following JSON
{
"Obj": "http://www.google.com"
}
Which is clearly not enough information to deserialize this back into a Uri object, and in fact, attempting to deserialize it will give me a String object instead.
Is there any existing way to correctly serialize and deserialize the type information here so that the object will be read back in as a Uri instead of a String? In this particular case, I am only attempting to interop with a .NET application and it is extremely important that the exact types are deserialized.
Thanks in advance.
If you want to convert the string back to the Uri, you can use custom converter attribute
The converter
And the use attribute
You should then be able to determine whether the underlying object is Uri like
You can also check out this article for more information Json.NET Uri (de)serialization errors