Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to deprecated method ObjectMapper.reader(Class)

Tags:

java

jackson

I am developing a JSON parsing application and want to use ObjectReader.

I get my instance of object reader as follows -

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final ObjectReader OBJECT_READER = OBJECT_MAPPER.reader(MyType.class);

however the OBJECT_MAPPER.reader(MyType.class); is showing as deprecated.

What alternative do I have to obtain an ObjectReader?

like image 833
Hector Avatar asked Nov 27 '15 12:11

Hector


People also ask

Is ObjectMapper deprecated?

ObjectMapper. reader(Class) was deprecated since Jackson 2.5. Starting with Jackson 2.6, you can use readerFor(Class) instead.

Can ObjectMapper be reused?

ObjectMapper class can be reused and we can initialize it once as Singleton object. There are so many overloaded versions of readValue() and writeValue() methods to work with byte array, File, input/output stream and Reader/Writer objects.

Should I declare Jackson's ObjectMapper as a static field?

Yes, that is safe and recommended.

What is ObjectMapper class in Java?

ObjectMapper is the main actor class of Jackson library. ObjectMapper class ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionality for performing conversions.


1 Answers

ObjectMapper.reader(Class) was deprecated since Jackson 2.5.

Starting with Jackson 2.6, you can use readerFor(Class) instead.

like image 90
Tunaki Avatar answered Oct 09 '22 00:10

Tunaki