简体   繁体   中英

How to map snake case yaml to camelcase java fields with Jackson

I have a yaml file with keys in camel case, like

---
start_date: "2018-09-01"
day_date: "2018-09-01"

userProduct:
    sales_channel: "1"
    user_group: "1" 

And I have the following Java PoJos

public class Input{
    private String startDate = "";
    private String dayDate = "";

    @JsonUnwrapped
    private Product userProduct;

   // getters/setters
}

public class Product {
   private salesChannel = "";
   private userGroup = "";

   // getters/setters
}

Now I want to read this with Jackson and I thought it would be sufficient to use

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
return objectMapper.readValue(inputData, Input.class); // inputData is the yaml as string

but this does not fill the nested objects. The startDate and dayDate from the Input object is correct, but input.getUserProduct().getSalesChannel() for example returns an empty string.

I also tried to use user_product instead of userProduct in the yaml file, but this does not change anything.

What is wrong here ? How can I define snake case in the yaml and camelcase in java ?

I have found a solution to my problem, which is to remove the nested objects. When having all fields inside the Input class and remove the Product , it works as expected.

I keep this question open nevertheless, as I'm still interested, whether this can be achieved with nested objects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM