Serializing/deserializing dot-separated property keys as nested objects

1,874 views
Skip to first unread message

Martin Ellis

unread,
Aug 26, 2015, 2:34:02 PM8/26/15
to jackson-user
Hello,

I was looking into the possibility of serializing/deserializing a Map<String, String> with dot-separated keys into a JSON structure.
I mention 'dot-separated keys' because I'd like the dots to denote nested objects in the JSON tree.

I wondered if there's already an implementation of this?

To clarify, I'd like the map:
    a.b=1
    a.c=2
    a.d.e=3
to be deserialised as:
{
    "a": {
        "b": 1,
        "c": 2,
        "d": {
            "e": 3
        }
    }
}

Error checking aside, I've put some draft code here:
  https://gist.github.com/martiell/e677c142518038f35379

Does anyone know if this has already been done?

Thanks
Martin

Ian Barfield

unread,
Aug 26, 2015, 5:30:20 PM8/26/15
to jackson-user

That syntax is a subset of hocon (which is a super set of json). Naturally, I'm partial to my own mash up at https://github.com/addthis/codec. However, there is another jackson-hocon module that has since changed its license from lgpl? to apache.


--
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 post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Martin Ellis

unread,
Aug 27, 2015, 9:29:39 AM8/27/15
to jackson-user
Thanks, Ian. I wasn't aware of hocon. Looks like a good format for configuration files.

My use case is also for application configuration, although I was also thinking about environment variables with underscore-separated names (in addition to dot-separated property keys).

I think I'll continue to experiment with the module I posted for now, and merging it with a JSON tree from a config file (which means it'd be straight-forward to switch from YAML config to hocon later…)

Cheers,
Martin

Tatu Saloranta

unread,
Sep 8, 2015, 2:46:42 PM9/8/15
to jackso...@googlegroups.com
Not sure if this is relevant, but I think this is what DropWizard does, to allow weaving in overrides from system properties into its main Configuration object.
The way DW does this is by first reading yaml/json content in a JsonNode tree, and then splitting dot-names into path, and adding/overriding nodes as necessary.
Code is not modular (I think), but as far as I remember it is relatively small chunk of code and possibly repurposable via cut'n pasting.

-+ Tatu +-




Martin Ellis

unread,
Sep 8, 2015, 3:03:49 PM9/8/15
to jackso...@googlegroups.com
Hi Tatu,

Yes, very relevant, thanks.

I hadn't looked at the DropWizard implementation because the
documentation suggests that configuration using environment variables
(not system properties) isn't so elegant: to use an environment
variable override you need something in the configuration YAML like:

defaultSetting: ${DW_DEFAULT_SETTING:-default value}

… according to this:
https://dropwizard.github.io/dropwizard/manual/core.html#environment-variables

I didn't realise that system properties could be used in the way I described.

Here's the magic:

https://github.com/dropwizard/dropwizard/blob/master/dropwizard-configuration/src/main/java/io/dropwizard/configuration/ConfigurationFactory.java#L183

Cheers,
Martin
> You received this message because you are subscribed to a topic in the
> Google Groups "jackson-user" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/jackson-user/du3Ulncyrao/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
Reply all
Reply to author
Forward
0 new messages