Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Deep" Object Graphs get serialized as "{}" #71

Closed
joergbattermann opened this issue Sep 20, 2014 · 8 comments
Closed

"Deep" Object Graphs get serialized as "{}" #71

joergbattermann opened this issue Sep 20, 2014 · 8 comments
Assignees

Comments

@joergbattermann
Copy link

I am not entirely sure whether this is a bug in Jil or me expecting a feature that is not there, but after exchaning Json.Net with Jil in a project, I got unit test errors stating that the returning Json was empty (as in "{}") but actual serialized objects were expected.

I've created a Gist to reproduce this with Jil 2.1.0 but basically what's going on there is an attempt to serialize an object that has 2 inheritance levels ('UserCredentialsUpdateResponse' > 'UserResponse' > 'Response') and that 'UserResponse' class has a '.User' property whose type also has one inheritance level (the types are simplified but those are levels of nestings actually going on).

When serializing an instance of 'UserCredentialsUpdateResponse' I'd expect it to serialize the graph, but I get '{}' back from the Jil.JSON.Serialize(..) call.. which is odd.

The sample Test / repro is here: https://gist.github.com/jbattermann/9589865db33742e2280b

Is my object graph simply unsupported or 'should' this work?

@kevin-montrose
Copy link
Owner

@jbattermann You need to tell Jil to include inherited members. You can do this by either passing Options.IncludeInherited or by creating your own Options object with includeInherited:true to pass.

With Options.IncludeInherited passed on line 32 your gist will serialize to:

{"User":{"CreatedAtUtc":"\/Date(1388581200000)\/","DateOfBirth":"\/Date(315550800000)\/","UserId":1,"Username":"SomeUsername"}}

By default inherited fields aren't included as, at least for the roles Jil was envisioned for in my projects, it's often unwanted behavior. It's there if you need it though.

@joergbattermann
Copy link
Author

Ah I see - thanks for the clarification. Good stuff. Thanks Kevin!

@joergbattermann
Copy link
Author

@kevin-montrose one question regarding the same 'area':

Options.IncludeInherited did in fact add the inherited properties in the serialized Json, but it doesn't work the other way around - if I pass in the same Options to the .Deserialize(..) call, the nested property is returned as 'null'.

I've read over the documentation but saw nothing that would indicate why it's leaving out that property (but it does mention the originally overlooked Optiones.IncludeInherited, sorry for asking what was clearly indicated :-/).

I've updated the gist and marked the line (see https://gist.github.com/jbattermann/9589865db33742e2280b#file-deepobjectgraphtests-cs-L45), maybe you can give me a push in the right direction. Thanks!

@kevin-montrose
Copy link
Owner

@jbattermann that may be an actual bug, I don't have time to dig in right this second but I'll try and set aside some time this week for it.

@kevin-montrose kevin-montrose self-assigned this Sep 22, 2014
@joergbattermann
Copy link
Author

Thanks @kevin-montrose , highly appreciated! Let me know if you need any more samples/input or whatsoever.

manofstick pushed a commit to manofstick/Jil that referenced this issue Sep 25, 2014
Removed BindingFlags.DeclaredOnly to find fields/properties. I don't see
the need to make this an option, because if the fields exist in both the
json and the object then they will be deserilized; otherwise they won't.
@manofstick
Copy link
Contributor

Fix is here

@kevin-montrose
Copy link
Owner

#76 merged in

@joergbattermann
Copy link
Author

Thanks Kevin & @manofstick !

manofstick pushed a commit to manofstick/Jil that referenced this issue Oct 11, 2014
Removed BindingFlags.DeclaredOnly to find fields/properties. I don't see
the need to make this an option, because if the fields exist in both the
json and the object then they will be deserilized; otherwise they won't.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants