10

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?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Thelight
  • 359
  • 1
  • 5
  • 15

2 Answers2

21

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.

Kihats
  • 3,326
  • 5
  • 31
  • 46
1

You can use custom PropertyNamingStrategy http://fasterxml.github.io/jackson-databind/javadoc/2.1.0/com/fasterxml/jackson/databind/PropertyNamingStrategy.html

Just create one and configure.

PropertyNamingStrategy strategy = // ... init
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(strategy);

You can either extend PropertyNamingStrategy class or perhaps it's better to extend PropertyNamingStrategyBase, can be easier.

pkozlov
  • 746
  • 5
  • 17