PhantomRow
❔ Deserialize Object that can have both Array and Object structure
JToken token = JToken.Load(reader);
if (token.Type == JTokenType.Array)
{
// If it's an array, deserialize as a LinkArray.
var linkArray = new LinkArray
{
Elements = token.ToObject<List<Link>>()
};
return linkArray;
}
else if (token.Type == JTokenType.Object)
{
// If it's an object, deserialize as a LinkElement.
return token.ToObject<LinkElement>();
}
16 replies
❔ Deserialize Object that can have both Array and Object structure
When I do return Json(result), I get link:{} -and I should get { atr1:value1} or [{atr1:val1,atr2:val2}], Values are there in result, and I can serializeObject to string and have attributes there
Any idea why am I missing link properties values? -how to get them?
16 replies
❔ Deserialize Object that can have both Array and Object structure
I have Entry class, I want to deserialize Entry class, Entry class has attribute link that can be both type Link and List<Link>, I provided in the fiddle best I could do.
Only workaround that I can think of is using try catch where I create class Entry1 that has Link link attribute and Entry2 that has List<Link> link attribute.
Any other suggestion.
I've read your link it somehow didn't help me.
16 replies
❔ Deserialize Object that can have both Array and Object structure
if (reader.TokenType == JsonToken.StartArray)
{
return serializer.Deserialize<LinkArray>(reader);
}
else if (reader.TokenType == JsonToken.StartObject)
{
return serializer.Deserialize<LinkElement>(reader);
}
else
{
throw new JsonSerializationException("Unexpected token type when deserializing 'link'.");
}
this doesn't work either
16 replies