E.S.
E.S.

Reputation: 2841

ObjectMapper for multiple naming strategies

I am dealing with a dataset where both underscores and hyphens are being used between tokens in property names:

    {
      "id" : "116",
      "priority" : 3,
      "table_id" : 0,
      "hard-timeout" : 0,
      "match" : {
        "ethernet-match" : {
          "ethernet-type" : {
            "type" : 2048
          }
        },
        "ipv4-destination" : "10.0.0.25/32"
      },
      "strict" : false,
      "flow-name" : "port_X_to_8_ip",
      "instructions" : {
        "instruction" : [ {
          "order" : 0,
          "apply-actions" : {
            "action" : [ {
              "order" : 1,
              "output-action" : {
                "max-length" : 60,
                "output-node-connector" : "8"
              }
            } ]
          }
        } ]
      },

Notice most elements have hyphens, but a few use underscores, such as table_id.

On the Java side, I am using this code to create my mapper:

import com.fasterxml.jackson.databind.json.JsonMapper;
...
JsonMapper jsonMapper = JsonMapper.builder().configure(SerializationFeature.INDENT_OUTPUT, true).build();
jsonMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); // <-- combine this
jsonMapper.setPropertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE); // <-- with this?

The last two lines are my current crux. I want the naming strategy to work for either SNAKE_CASE or KEBAB_CASE however I don't see a way to or the properties and can't find much on google.

Upvotes: 0

Views: 2209

Answers (1)

Andreas
Andreas

Reputation: 159165

You pick the naming strategy that fits for most properties, then use the @JsonProperty() annotation to name the ones that don't follow the standard.

In your case, that would be PropertyNamingStrategy.KEBAB_CASE and @JsonProperty("table_id").

Upvotes: 4

Related Questions