-
Notifications
You must be signed in to change notification settings - Fork 76
Add support for serialization of object members (using prefixing of parent name) #9
Comments
Limitation was added so we can figure out what would be the best way to deal with embedded values. |
Thank you! |
Yes, understood. Sorry for long delay, but it is possible that I could tackle this for next release (2.3). |
Oh. Forgot to add one obvious related thing: use of |
using @JsonUnwrapped didn't work for me. Wondering when this support will be added? |
Did not work in what way? |
My initial purpose was to serialize the following POJO class public class Application { |
Ah. The problem is with Could you add a new issue entry, asking for support to handle |
I would also like to give some input ;-) class Customer { Integer id; Address billing; Address shipping; } class Address { String first_name; String last_name; Contact contact; } class Contact { String phone; } Why not just always use the name of the parent-element as prefix in the CSV. So the Pojo would be serialized like this (first line is csv-schema): id|billing:first_name|billing:last_name|billing:contact:phone|shipping:first_name|shipping:last_name|shipping:contact:phone| 7|max|headroom|01648288383|julia|headroom|0164828383 |
@5kilodope At conceptual level that would make sense I think. The challenge is just the interaction between data-binding and streaming parser, whether that could be made to work considering that data-binding is format agnostic. Come to think of this, it might well be possible if streaming parser and generator handled this transparently. If done by streaming layer, this would simply mean that generator would "output" If done via schema, it could work more similar to There are some drawbacks to both approaches, but I think former (handle it at CSV-specific streaming) might be better all around. One challenge there would be need for heuristics, to assume such naming convention, but it seems like perhaps lesser evil. It would also be more performant, and aside from theoretical possibility of name collision (user explicitly uses prefix notation), would probably be more robust. |
thanks for your feedback. I see it is not that easy, but we already use JSON/XML Databinding for our little Import/Export Tool. And it would therefore be nice if CSV could be also used, and could be equally powerful for pojo-mapping. JSON/XML already provide a complete schema within their data. So theoretically CSV could be equally powerful, if the schema in the first line is used. Hope you find a good solution on that ;-) |
@5kilodope Yes, first line can be optionally used as the source for schema already (if not, caller must provide it). I agree in that there's much more that could be done with CSV, using little bit of naming convention, at least for nested Objects. Arrays are bit trickier, but even there it might be possible to use in-field separators. |
Is there any pseudo-easy way to achieve this or similar nowadays by using a list of JsonPointer paths for colums and let the csvmapper write those nodes to the csv, provided that the resulting node of the pointer is of a serializable type and not an object. |
No. Jackson stays out of transformation business as a boundary. You can achieve this outside of core databind by creating a JsonNode, transforming it, but there is nothing automated and I have no plans to add anything that requires building of intermediate tree structures. But as I said |
this is going to be in next release ? |
@zadeswapnilzade There is no active development for this feature at this point in time. |
@JsonUnwrapped doesn't work if we are using the same POJO for deserializing from JSON and serializing to CSV as @5kilodope pointed out. It seems like mix-ins might be able to solve this problem, but given that the conversion between JSON and CSV are probably the main use for this library, I think this would be a very valuable feature. |
@don-han Certainly would be valuable, if someone had time to work on this. |
One simple, but manual solution might be to allow dot notation in the Schema Builder
That would allow for writing CSV from complex objects |
@jmatthewpryor there are couple of related challenges: but perhaps something like this could work, similar to how |
@don-han mixins seems deprecated. |
@alianos- Mix-ins are certainly not deprecated as a feature. But maybe you mean something else? |
If I write |
+1 for this :) |
Guys, I have created a pull-request for this here: FasterXML/jackson-dataformats-text#97 however I have one test failing. I feel like I'm almost there, perhaps @cowtowncoder can help me out :) |
I have updated the PR and now all the tests are passing. However, I am not yet 100% sure what in what I have implemented. @cowtowncoder can you please have a look at it and if there's something else to be done, please let me know. |
Let's assume we have two classes:
If we'd serialize an instance of
A
the result isA_string1,A_string2
The serialization of an instance of B would look like this
B_string1,B_string2
Now If we'd remove the
@JsonIgnore
from class A, we'd get the Exception:com.fasterxml.jackson.core.JsonGenerationException: CSV generator does not support Object values for properties
Why couldn't we just serialize b in a -- the result would be quite intuitive:
A_string1,A_string2,B_string1,B_string2
This would enable jackson-csv to serialize object members as it's possible in XML and JSON.
The text was updated successfully, but these errors were encountered: