I'm using Newtonsoft.Json to deserialize the output from my webservice to an object.
It worked fine until I added a Bitmap
property to my class (named User
) to hold an avatar.
The webservice is returning that propert as a Base64 string, which is as expected.
The problem is when I try to convert back the JSON from the WS to a List<User>
, a JsonSerializationException
is thrown in this block of code:
// T is IList<User>
response.Content.ReadAsStringAsync().Proceed(
(readTask) =>
{
var json = ((Task<string>)readTask).Result;
var result = JsonConvert.DeserializeObject<T>(json); //<-- it fails here
// do stuff!
});
Output from exception is:
Error converting value "System.Drawing.Bitmap" to type 'System.Drawing.Bitmap'. Path '[2].Avatar
and looking at the inner exception:
{"Could not cast or convert from System.String to System.Drawing.Bitmap."}
It's clear that it fails to parse the Base64 string, but it's not clear why.
Any ideas/workaround?
EDIT
I know I can use Convert.FromBase64String
do get a byte array and load a bitmap from that. Then I'd like to update my question to ask about how can I skip or manually parse only that field.
I would like to avoid, having to manually parse all the JSON.
Is this even possible?
EDIT 2
I found out the root problem: JSON is not being correctly serialized in webservice (and I fail to see why). I thought that this was a somewhat different issue, but no. My webservice is simply returning a string "System.Drawing.Bitmap"
instead of its base64 content. Hence the JsonSerializationException
.
I have been unable to solve that issue, the only solution I've found is to turn my field into a byte []
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…