22

When I serialize/deserialize any object, all field names are converted to lower case. Is there any configuration to set that makes Jackson keep the field names exactly as they are? Both for serializing and deserializing?

(I know about @JsonProperty, but this does not seems to be right, since what I need is just for Jackson to respect what already exists)

My test code:

import java.io.Serializable;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;

public class Test {

    static class Example implements Serializable {
        private String Test;
        private String ABC;
        private String XyZ;

        public String getTest() { return Test; }
        public void setTest(String test) { Test = test; }

        public String getABC() { return ABC; }
        public void setABC(String abc) { ABC = abc; }

        public String getXyZ() { return XyZ; }
        public void setXyZ(String xyz) { XyZ = xyz; }
    }

    static class MyPropertyNamingStrategy extends PropertyNamingStrategy {
        @Override
        public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
            return convert(defaultName);
        }
        @Override
        public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
            return convert(defaultName);
        }
        @Override
        public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
            return convert(defaultName);
        }
        private String convert(String input) {
            return input;
        }
    }

    public static void main(String[] args) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper()
        .setPropertyNamingStrategy(new MyPropertyNamingStrategy())
        .enable(SerializationFeature.INDENT_OUTPUT)
        .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);      

        //From OBJECT to JSON
        Example ex = new Example();
        ex.setTest("1");
        ex.setABC("2");
        ex.setXyZ("3");
        System.out.println(objectMapper.writeValueAsString(ex));

        //FROM JSON to OBJECT
        String jsonString = "{ \"Test\":\"0\", \"ABC\":\"1\", \"XyZ\":\"2\" }";
        Example fEx = objectMapper.readValue(jsonString, Example.class);
    }   

}

Thanks to @BlueLettuce16, I have managed to build an 'improved' version of the PropertyNamingStrategy. Here it is:

import java.lang.reflect.Modifier;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;

public class CustomPropertyNamingStrategy extends PropertyNamingStrategy {

    @Override
    public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
        return convertForField(defaultName);
    }

    @Override
    public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return convertForMethod(method, defaultName);
    }

    @Override
    public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return convertForMethod(method, defaultName);
    }

    private String convertForField(String defaultName) {
        return defaultName;
    }

    private String convertForMethod(AnnotatedMethod method, String defaultName) {
        if (isGetter(method)) {
            return method.getName().substring(3);
        }
        if (isSetter(method)) {
            return method.getName().substring(3);
        }
        return defaultName;
    }

    private boolean isGetter(AnnotatedMethod method) {
        if (Modifier.isPublic(method.getModifiers()) && method.getGenericParameterTypes().length == 0) {
            if (method.getName().matches("^get[A-Z].*") && !method.getGenericReturnType().equals(void.class))
                return true;
            if (method.getName().matches("^is[A-Z].*") && method.getGenericReturnType().equals(boolean.class))
                return true;
        }
        return false;
    }

    private boolean isSetter(AnnotatedMethod method) {
        return Modifier.isPublic(method.getModifiers()) && method.getGenericReturnType().equals(void.class) && method.getGenericParameterTypes().length == 1
                && method.getName().matches("^set[A-Z].*");
    }

}
BBacon
  • 2,456
  • 5
  • 32
  • 52
  • 3
    implements a custom [`PropertyNamingStrategy`](https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/PropertyNamingStrategy.java), maybe –  Nov 04 '14 at 21:30
  • How are you using @JsonProperty? Are you passing it the String field name? – Ann Kilzer Nov 04 '14 at 21:45
  • I don't see a way to implement the PropertyNamingStrategy to match the field names properly. If a field is named "ABC", Another one is named XYz, they will NOT match to the JSON when serializing/deserializing. – BBacon Nov 05 '14 at 10:50
  • Can you use? new Gson().toJson( object) It is straightforward. – 0zkr PM Jun 02 '23 at 06:12

6 Answers6

15

Even though @JsonProperty doesn't work, I was able to use @JsonSetter and @JsonGetter to map capitalized json field names.

@JsonSetter("ABC")
public void setABC(String ABC) {
    this.ABC= ABC;
}

Spring will now serialize the object field as "ABC" and not "abc".

Ethan
  • 151
  • 1
  • 4
10

I've had the same problem.

This is my solution:

public class MyNamingStrategy extends PropertyNamingStrategy {

    @Override
    public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
        return field.getName();
    }

    @Override
    public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return convert(method, defaultName);
    }

    @Override
    public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return convert(method, defaultName);
    }

    private String convert(AnnotatedMethod method, String defaultName) {

        Class<?> clazz = method.getDeclaringClass();
        List<Field> flds = FieldUtils.getAllFieldsList(clazz);
        for (Field fld : flds) {
            if (fld.getName().equalsIgnoreCase(defaultName)) {
                return fld.getName();
            }
        }

        return defaultName;
    }
}

In this case you will get the exact name of the property, and will not have to depend on the correct names of the methods.

Bobby
  • 166
  • 1
  • 4
9

I think that this is the solution (using custom PropertyNamingStrategy):

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;

public class MyPropertyNamingStrategy extends PropertyNamingStrategy {
    @Override
    public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
        return convert(field.getName());
    }

    @Override
    public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return convert(method.getName().toString());
    }

    @Override
    public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return convert(method.getName().toString());
    }

    private String convert(String input) {
        return input.substring(3);
    }
}

Test

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.io.StringWriter;

public class MyPropertyNamingStrategyTest {
    public static void main(String[] args) {
        PrivatePerson privatePerson = new PrivatePerson();
        privatePerson.setFirstName("John");
        privatePerson.setLastName("Smith");

        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(new MyPropertyNamingStrategy());
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        StringWriter sw = new StringWriter();
        try {
            mapper.writeValue(sw, privatePerson);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(sw.toString());
    }
}

PrivatePerson

public class PrivatePerson {
    private String firstName;
    private String lastName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }
}
eis
  • 51,991
  • 13
  • 150
  • 199
BlueLettuce16
  • 2,013
  • 4
  • 20
  • 31
  • Now the fields are all uppercased. Serializing and Deserializing will only work if the class fields are all uppercased. – BBacon Nov 05 '14 at 10:48
  • Did you try to change in convert method to return input? private String convert(String input) { return input; } – BlueLettuce16 Nov 05 '14 at 10:50
  • Yes I did. The diffence is that they are all lowercased now. The case is never respected, or they are all Upper, all Lower, or Pascal (first letter uppercased)... But JUST RESPECTING the case is not an option found. – BBacon Nov 05 '14 at 11:04
  • I found the solution and edited the code. I have changed what is returned from nameForField, nameForGetterMethod and nameForSetterMethod. – BlueLettuce16 Nov 05 '14 at 11:19
  • Yes, I have to mannually convert JSON -> Object -> JSON for the inputs/outputs to be the same. This Jackson case insensitive design is flawed. – BBacon Nov 05 '14 at 11:20
  • I have updated my question. My test class sums is all up. The updated answer did not work... I think. – BBacon Nov 05 '14 at 11:25
  • I think that you havent seen the last version. Please take a look for example what is returned from nameForGetterMethod - now it is convert(method.getName().toString()) instead of convert(fieldName) and convert method also has changed. – BlueLettuce16 Nov 05 '14 at 11:26
  • I re-copied the whole MyPropertyNamingStrategy, It is not working. You can test it whit my test class. – BBacon Nov 05 '14 at 11:32
  • 1
    I have tested your class and I recieve for from object to json: { "Test" : "1", "ABC" : "2", "XyZ" : "3" } and from json to object: Example{Test='0', ABC='1', XyZ='2'} what do you expect? If you expect the same and your result is different then you didn't copy the latest solution. – BlueLettuce16 Nov 05 '14 at 11:48
  • I copied it but forgot to change in the objectmapper back to it. Sorry for my mistake and thanks a lot for you help. – BBacon Nov 05 '14 at 11:55
  • That's no problem. I'm glad that I could help :) – BlueLettuce16 Nov 05 '14 at 12:01
  • This doesn't work, at least with Kotlin data class. It's either Capitalized, or everything is lower case, or uses getters which have a different name than the fields (start with `get`). – User Jun 09 '19 at 13:24
6

You can configure Jackson to be case sensitivity tolerant:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

Cudos to https://stackoverflow.com/a/32842962/1639556

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
  • 1
    will that help for serializing, or just for deserializing? – eis Jun 28 '17 at 07:04
  • I tried this for deserializing incoming request. But it makes no sense for me that it would affect serialization. You already have the object and it must be serialized only one certain way every time. – Leos Literak Jun 28 '17 at 07:08
  • Indeed, so it does not solve the problem OP is having - he wants to have the original versions for both serializing and deserializing. – eis Jun 28 '17 at 07:10
  • You are right, I missed that part. Anyway my answer may be helpful to googlers in future. I found it earlier than the other solution. – Leos Literak Jun 28 '17 at 07:17
  • OP need Case SENSITIVE, why should he configure case sensitivity tolerant? I think he should config smth like case insensitive prohibit? – Pham X. Bach Dec 29 '22 at 03:16
6

Using @JsonProperty annotation. It work well

Example

@JsonProperty("Code")
private String Code;
@JsonProperty("Message")
private String Message;
Ninh
  • 61
  • 1
  • 1
0

Created a own class for PropertyNamingStrategy- As per Answer 7 Working fine

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.AnnotatedField;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;

public class MyPropertyNamingStrategy extends PropertyNamingStrategy {

    @Override
    public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
        return convert(field.getName());
    }
        
    @Override
    public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return convert(method.getName().toString());
    }
        
    @Override
    public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
        return convert(method.getName().toString());
    }
        
    private String convert(String input) {
        return input.substring(3);
    }
}

And I have my POJO class - the Payload Class:

package orderCreateAPI;
        
import java.util.ArrayList;
        
public class Payload {
    OrderInfo OrderInfo;
            
    ArrayList<orderCreateAPI.ShipmentInfo> ShipmentInfo;
            
    public Payload(OrderInfo order, ArrayList<orderCreateAPI.ShipmentInfo> shipInfo){
                
        this.OrderInfo =order;
        this.ShipmentInfo = shipInfo;
    }
        
    public OrderInfo getOrderInfo() {
        return OrderInfo;
    }
        
    public void setOrderInfo(OrderInfo orderInfo) {
        OrderInfo = orderInfo;
    }
        
    public ArrayList<orderCreateAPI.ShipmentInfo> getShipmentInfo() {
        return ShipmentInfo;
    }
        
    public void setShipmentInfo(ArrayList<orderCreateAPI.ShipmentInfo> shipmentInfo) {
        ShipmentInfo = shipmentInfo;
    }
        
}

The execution class:

public class TC1_CreateOrder extends orderCreateRequest{
    @Test
    public static void TC1_CreateOrder() throws JsonProcessingException,JsonMappingException,IOException {
        //throws JsonParseException,JsonMappingException,IOException
        //Data fetch
        ArrayList<OrderReferences> orRef = new ArrayList<OrderReferences>();
        orRef.add(new OrderReferences("BM","IFC"));
        ArrayList<OrderItem> orItem = new ArrayList<OrderItem>();
        orItem.add(new OrderItem("AOTEST1001","60111"));
        ShipperInfo ship = new ShipperInfo("URBN","URBN PA DC");
        ArrayList<ShipmentInfo> ShipInfo = new ArrayList<ShipmentInfo>();
        ShipInfo.add(new ShipmentInfo("ASTEST1001","RCVD"),ship, orItem));
        ConsigneeInfo ConsigneeInfo = new ConsigneeInfo("Green Mile","133 Avenue");  
        OrderInfo OrderInfo = new OrderInfo("AOTEST1001", "2021-09-03T",orRef, ConsigneeInfo);
        Payload p = new Payload(OrderInfo,ShipInfo);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(new MyPropertyNamingStrategy());
                
        StringWriter s = new StringWriter();
        try {
            mapper.writeValue(s, p);
        } catch (IOException e) {
            e.printStackTrace();
        }                
    }
}

Response body before: -Wrong properties

{
    "orderInfo": {
        "orderNumber": "AOTEST1010",
        "orderCreatedDate": "2021-09-03T00:00:00.000Z"
    }
}

Response body after: -correct properties

{
    "OrderInfo": {
        "OrderNumber": "AOTEST1010",
        "OrderCreatedDate": "2021-09-03T00:00:00.000Z"
    }
}
lprakashv
  • 1,121
  • 10
  • 19