JSON file is case insensitive in Jackson

I have a JSON properties file that is manually updated by the user.

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 manually updated by the user, it can use any enclosure; mixed, top, bottom, etc. The solution I found, when reading the file, I convert to lower case, like this:

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

      

After that, I map my object using Jackson, which works. I am using lowercase member names for the mapped classes. Is this the correct approach or is there another way?

+3


source to share


2 answers


you can use

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

      



This feature is found in Jackson 2.5.0 and later.

+8


source


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

Just create and customize.



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

      

You can either extend the PropertyNamingStrategy class, or it might be better to extend PropertyNamingStrategyBase, it might be easier.

0


source







All Articles