Re: [jackson-user] Access objectMapper from custom JsonSerializer to call valueToTree

2018-04-16 Thread Tatu Saloranta
On Mon, Apr 16, 2018 at 11:02 AM, Marcel Overdijk  wrote:
> Thanks Tatu!
>
> Another option I was thinking of was
>
> 1) creating the object mapper
> 2) creating the serializer (and provide the created objectmapper to the 
> serializer e.g. via the serializers constructor)
> 3) create a module and register the serializer
> 4) register the module with the objectmapper
>
> Or do you see any problems with that approach?

No reason why it couldn't work.

You could also pass it using context attributes (set on
`ObjectWriter`, accessible through
`SerializerProvider.getAttribute()`).

-+ Tatu +-

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [jackson-user] Access objectMapper from custom JsonSerializer to call valueToTree

2018-04-16 Thread Marcel Overdijk
Thanks Tatu!

Another option I was thinking of was 

1) creating the object mapper
2) creating the serializer (and provide the created objectmapper to the 
serializer e.g. via the serializers constructor)
3) create a module and register the serializer
4) register the module with the objectmapper

Or do you see any problems with that approach?

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [jackson-user] Access objectMapper from custom JsonSerializer to call valueToTree

2018-04-16 Thread Tatu Saloranta
On Sun, Apr 15, 2018 at 11:01 AM, Marcel Overdijk  wrote:
> Hi,
>
> Is it possible to access the `objectMapper` from a custom `JsonSerializer`?
>
> Imagine a serializer like:
>
>
> class JsonapiDocumentSerializer :
> StdSerializer(JsonapiDocument::class.java) {
>
> @Throws(IOException::class)
> override fun serialize(jsonapiDocument: JsonapiDocument, json:
> JsonGenerator, provider: SerializerProvider) {
>
> json.writeStartObject()
>
> // Would like to do something like:
> val dataJson: ObjectNode =
> objectMapper.valueToTree(jsonapiDocument.data)
>
> // And then manipulate the dataJson like:
> // - removing fields
> // - extract particular nodes and add them to an included
> `ArrayNode`
> val includedJson = ArrayNode // create `ArrayNode` (how to
> create it from JsonSerializer?) and add data from dataJson
>
> json.writeObjectField("data", dataJson)
> json.writeObjectField("included", includedJson)
>
> // Do other stuff:
> jsonapiDocument.meta?.let { json.writeObjectField("meta", it) }
> jsonapiDocument.jsonapi?.let { json.writeObjectField("jsonapi",
> it) }
> jsonapiDocument.links?.let { json.writeObjectField("links", it)
> }
>
> json.writeEndObject()
> }
> }
>
> I would like to call the objectMapper valueToTree, manipulate the data and
> the write it in the object.

No, serializers do not have back-references to `ObjectMapper`. Closes
thing is `SerializerProvider`,
context object which has access to serialization-side functionality of
mapper. But that does not, unfortunately,
include parts you would need for conversion.

I think, however, that `SerializerProvider` does have enough
functionality to handle this case since what you need
is ability to serialize value as a tree; then you can manipulate tree;
and then further get serializer to write out tree.

But the tricky part is that since you overriding default POJO
serializer for type, there would be no way to find original
any more. Except if you register `BeanSerializerModifier` (via
Module), override `modifySerializer()`, you can store
reference to POJO serializer, create your own wrapper. To use that
serializer you will need a `JsonGenerator`, and for
conversion, `TokenBuffer` is used as it extends `JsonGenerator`, and
can later on provide `JsonParser` for contents.
And finally, to serialize a `JsonNode`, you'd need to call
`SerializerProvider` to find standard serializer (or cheat and
create one yourself).

Another possibly simpler approach would be to see if Converters would
work: both `@JsonSerialize` and `@JsonDeserialize`
allow specifying `converter` property. What that gives is it lets you
inject converter that starts with custom type (for serialization),
and lets you massage value into different value of different type
(usually, although may be same type), and then apply
default serialization.

I hope this helps,

-+ Tatu +-

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.