Closed bgorven closed 8 years ago
I am not sure why I follow why PrettyPrinter
would not work here: it gets all the necessary callbacks, so wouldn't your implementation be able to keep track of state changes to use whatever indentation you deem necessary?
The pretty printer doesn't know anything about the structure of the object that is being serialized. So if I wanted to serialize the array the makes up the jcard body with newlines, but serialize each jcard property array inline, there would need to be some way of letting the pretty printer know which type of array it was currently serializing.
@bgorven It does not directly know, but if needs to, it has to keep state. It also has access to JsonGenerator
, which has knowledge of at least JSON structure (via getOutputContext()
) as well as (with newer versions) even likely current value object at binding level (via getCurrentValue()
which delegates through output context object).
At this point I do not think I want to add more complexity to PrettyPrinter
: if the API is not sufficient it is possible that caller directly uses writeRaw()
methods to add indentation similar to how DefaultPrettyPrinter
does.
Looks like that worked. Awesome. Thanks.
This is related to https://github.com/mangstadt/ez-vcard/pull/48. I would like to be able to serialize JCards as
The PrettyPrinter interface doesn't have any methods to change the format of arrays or objects at generation time, so the only way I was able to get this to work was by creating a DefaultPrettyPrinter, setting it to the JsonGenerator, and switching out the DefaultPrettyPrinter.Indenter at the start and end of each property (see https://github.com/mangstadt/ez-vcard/pull/48/commits/63822e5d2260d3f95f932bf0777923836e6482b0). This only works when the JsonGenerator is entirely controlled by ezvcard code (i.e. when serializing VCards directly to json), and wouldn't work when an embedded VCard is serialized using a custom JsonSerializer.
I'm willing to write the code for any changes, but first I wanted to ask what the design should look like, because presumably whatever changes may be made to support this would affect anyone downstream who may have written a PrettyPrinter implementation.
My suggestion would be to add methods
setInlineObjects(boolean)
andsetInlineArrays(boolean)
to the PrettyPrinter interface. Implementations could choose to ignore them, but for DefaultPrettyPrinter, that could be used to change the indentation style for objects and arrays, respectively.