-
-
Notifications
You must be signed in to change notification settings - Fork 120
Convert extra when extra is not serializable #185
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks pretty good. I have a couple of comments, but otherwise I give this 👍 .
{ | ||
result = JObject.FromObject(extra); | ||
} | ||
catch (ArgumentException e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for an instance e
here, is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed.
} | ||
catch (ArgumentException e) | ||
{ | ||
result = JObject.Parse(string.Format(@"{{""{0}"":""{1}""}}", extra.GetType(), extra)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems a bit sub-optimal. Can't we instantiate a new JObject
and give it a property somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the solution is either this or dynamic
object. I personally do not favor any usage of dynamic
, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this works just fine:
result = JObject.FromObject(new Dictionary<string, object>
{
{ extra.GetType().ToString(), extra }
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah there's Dictionary
... I'm too obsessed with the dynamic type thing in JSON and forgot about it. Should it be Dictionary<string, string>
, though? Or it's handled by Json.Net itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON.NET serializes object
just fine. 😃
@skyline75489: Excellent work, thanks! |
This fixed #180