Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON property file case insensitive in Jackson [duplicate]

Tags:

java

jackson

I have a JSON property file, which is updated by a user manually.

I am mapping it to objects, using Jackson Object mapper:

[ 
   {  "id":"01",
      "name":"Joe",
      "Children" : [ {"Name" : "Alex",
                       "Age" : "21"},
                     {"name" : "David",
                      "Age" : "1"}
                   ]
    },
    {  "id":"02",
       "name":"Jackson",
       "Children" : [ {"Name" : "Mercy",
                       "Age" : "10"},
                      {"name" : "Mary",
                       "Age" : "21"}
                    ]
    }
]

Since it is updated manually by a user, they can use any casing; mixed, upper, lower, etc. The solution I have found is, while reading the file I am converting to lower case, like this :

 String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();

After this I am mapping to my object using Jackson, which works. I am using lower case member names for the mapped classes. Is it the right approach or is there any other way?

like image 436
Thelight Avatar asked Jun 12 '15 14:06

Thelight


1 Answers

You can use

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

This feature is in Jackson version 2.5.0 onwards.

like image 152
Kihats Avatar answered Oct 04 '22 00:10

Kihats