How can i serialize Yaml comments into Java Type

21 views
Skip to first unread message

Rahul Kumar

unread,
Jul 3, 2020, 7:25:00 PM7/3/20
to jackson-user
Hi geeks,
I have a Yaml like below as below:
---
# mY yaml variable
var1: ok

I dont want to ignore comments while serialization and i wanted to take them into account ? How can i do that ?
My code is like below (which ignores call comments)

 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

 Object user = mapper.readValue(yamlfile,new TypeReference<Map<String, String>>() {});


I want to use this feature to read some meta data written on top of Yaml variables in the form of comments ? Does jackson allows this ?


Regards

Rahul

Tatu Saloranta

unread,
Jul 3, 2020, 7:58:56 PM7/3/20
to jackson-user
There is no way to currently add comments with Jackson YAML generator.
Comments are ignored when reading content (I think SnakeYAML automatically discards them) and there is no current mechanism to inject them on serialization side.

-+ Tatu +-

 

Rahul Kumar

unread,
Jul 6, 2020, 5:45:23 AM7/6/20
to jackso...@googlegroups.com
Thanks Tatu !
So in that case , is it possible to write custom yaml deserializer in jackson (to read the comments) just like we write custom Json deserializer ?
like
public class CustomDeserializer  extends JsonDeserializer<SortedMap<String, Object>> {}

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAGrxA241dJ7Oex5P86ddG-AeX_oY6h5sYLCxNYSK3LpEb3shCg%40mail.gmail.com.

Tatu Saloranta

unread,
Jul 6, 2020, 5:13:58 PM7/6/20
to jackson-user
On Mon, Jul 6, 2020 at 2:45 AM Rahul Kumar <rajput...@gmail.com> wrote:
>
> Thanks Tatu !
> So in that case , is it possible to write custom yaml deserializer in jackson (to read the comments) just like we write custom Json deserializer ?
> like
> public class CustomDeserializer extends JsonDeserializer<SortedMap<String, Object>> {}

No, that would require Jackson to actually get an somehow expose
comments: there is no token type that would match,
and I am not sure SnakeYAML even reports them in the first place.

You can possibly write comments if you hold on to instance to
underlying Writer, use generator.flush(),
but I don't think there is way through API to see comments.

-+ Tatu +-

>
> On Sat, 4 Jul 2020 at 05:28, Tatu Saloranta <tsalo...@gmail.com> wrote:
>>
>> On Fri, Jul 3, 2020 at 4:25 PM Rahul Kumar <rajput...@gmail.com> wrote:
>>>
>>> Hi geeks,
>>> I have a Yaml like below as below:
>>> ---
>>> # mY yaml variable
>>> var1: ok
>>>
>>> I dont want to ignore comments while serialization and i wanted to take them into account ? How can i do that ?
>>> My code is like below (which ignores call comments)
>>>
>>> ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
>>>
>>> Object user = mapper.readValue(yamlfile,new TypeReference<Map<String, String>>() {});
>>>
>>>
>>> I want to use this feature to read some meta data written on top of Yaml variables in the form of comments ? Does jackson allows this ?
>>>
>>>
>>
>> There is no way to currently add comments with Jackson YAML generator.
>> Comments are ignored when reading content (I think SnakeYAML automatically discards them) and there is no current mechanism to inject them on serialization side.
>>
>> -+ 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...@googlegroups.com.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAGrxA241dJ7Oex5P86ddG-AeX_oY6h5sYLCxNYSK3LpEb3shCg%40mail.gmail.com.
>
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/CAGH8rExm%3D8NwEsGNXVaU0bysTYOHtjrFQJ8%3DD390icQ0Ao5yPw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages