Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot work with Jackson

I am wondering why there is not a determined way to work with Jackson. I just want to parse JSON string:

ObjectMapper mapper = new ObjectMapper();
Customer[] myObjects = mapper.readValue(file, Customer[].class);

But I really confused what should I import to do that. According to this link, I tried to import mapper-asl.jar. But I get this compile error:

The type org.codehaus.jackson.JsonParser cannot be resolved. It is indirectly referenced from required .class files

Then I try to import jackson-core-2.4.2 and jackson-databind-2.4.2. So there was no compile error but I got this runtime exception instead (in mapper definition line):

java.lang.NoClassDefFoundError: com.fasterxml.jackson.annotation.JsonAutoDetect

Guide me please what should I import to work with Jackson. Thanks

like image 497
Misagh Emamverdi Avatar asked Sep 20 '14 09:09

Misagh Emamverdi


People also ask

Does Lombok work with Jackson?

Lombok @Jacksonized. Using @Jacksonized annotation is the simplest solution if you are using Jackson for deserialization purposes. Just annotate the @Builder class with @Jacksonized annotation and we are good for converting the JSON string to Java object.

What is meant by @JsonProperty?

The @JsonProperty annotation is used to map property names with JSON keys during serialization and deserialization. By default, if you try to serialize a POJO, the generated JSON will have keys mapped to the fields of the POJO.

Does Jackson need default constructor?

Jackson uses default (no argument) constructor to create object and then sets value using setters. so you only need @NoArgsConstructor and @Setter.

Can Jackson use JAXB annotations?

Supporting JAXB AnnotationsThe Jackson XML module also has the ability to support the standard JAXB annotations on our beans – instead of needing the Jackson specific ones.


2 Answers

use these dependencies jackson-databind
jackson-annotations
jackson-core

public class JsonTest {
    public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper mapper=new ObjectMapper();
    Map<String,String> dt=new Hashtable();
    dt.put("1", "welcome");
    dt.put("2", "bye");
    String jsonString = mapper.writeValueAsString(dt)
    System.out.println(jsonString);
    }    
}
like image 178
Kumar Avatar answered Sep 20 '22 06:09

Kumar


Looks like mixed up references.

You might be using a library that uses an old version of Jackson itself (i.e. the org.codehaus package)...

I usually just reference Jackson through Maven.

Something like:

<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>LATEST</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>LATEST</version>
    </dependency>
</dependencies>
like image 31
Mena Avatar answered Sep 19 '22 06:09

Mena