On Tue, May 21, 2019 at 3:57 PM Uzh Valin <blahblo...@gmail.com> wrote:
>
> Ok, but how does Jackson know which custom mapper to call if we are simply 
> putting obj.getFirstProperty() in the response map? I know need one value and 
> would want the serializer to ignore all other properties regardless whether 
> they have been initialized with default values.

It doesn't, but what I am saying is that your approach will not work as shown.
If you add String values, they will be JSON Strings and must be
escaped. That's how Strings are handled -- otherwise Jackson would
have to somehow re-parse String into JSON, and then go back to
serializing contents, adding significant overhead that is not usually
needed. Assuming that String was valid JSON; if not it would either
have throw exception, or quietly determine it has to be used as-is...
or something.

If you want to apply different rules there are a few ways you could
achieve that -- custom serializers are one way -- or you could
serialize-as-String-then-deserialize if you want to apply filtering.
Perhaps latter is the way to go.

In fact, you could probably use something like:

JsonNode node = mapper.valueToTree(inputValue);
results.put(key, node);

which would convert from POJO into JsonNode -- and this does use
serialize() methods, filtering, but with less overhead -- and then add
JsonNode as value to be serialized by "parent" mapper.

I think this might achieve what you are attempting here?

-+ Tatu +-

>
>
> On Tuesday, May 21, 2019 at 2:40:27 PM UTC-4, Tatu Saloranta wrote:
>>
>> Just one question:
>>
>>
>>>
>>>         responseBody.put("firstProperty", 
>>> serializeFirstProperty(obj.getFirstProperty()));
>>>         responseBody.put("secondProperty", 
>>> serializeSecondProperty(obj.getSecondProperty()));
>>>         responseBody.put("thirdProperty", 
>>> serializeThirdProperty(obj.getThirdProperty()));
>>
>>
>> Why do you serialize values? That is where "double-escaping" comes: you are 
>> adding JSON String within content to be JSON serialized. Just add values as 
>> is
>>
>>    responseBody.put("firstProperty", obj.getFirstProperty());
>>
>> and contents would get serialized just once.
>>
>> -+ 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/jackson-user/e9420cec-0ddb-4af4-bfc7-1383bede4920%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/CAL4a10iRNcK1fWix-S6H8gqiabAi5Bki%3DNujiDgEyqrcdRXc4g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to