Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson Library JSON Mapper to String

I have a class (A and B are my objects)

public A   {
    private List<B> b;
    private int c;
}

I have a String variable temp. So i would save in this variable the JSON String of my object. How?

I've tried with this 2 method, but without result.

public void save() {
    ObjectMapper mapper = new ObjectMapper();
    String temp = mapper.writeValueAsString(a);
}
public void read(String temp) {
    ObjectMapper mapper = new ObjectMapper();
    A a = mapper.readValue(temp, A.class);
}

How can I solve it?

like image 824
user2520969 Avatar asked Jun 26 '13 11:06

user2520969


People also ask

How to serialize a map<string> to JSON in Jackson?

For a simple case, let's create a Map<String, String> and serialize it to JSON: Map<String, String> map = new HashMap<> (); map.put ( "key", "value" ); ObjectMapper mapper = new ObjectMapper (); String jsonResult = mapper.writerWithDefaultPrettyPrinter () .writeValueAsString (map); ObjectMapper is Jackson's serialization mapper.

How to use Jackson Library with objectmapper?

To include Jackson library in our project, we should include jackson-databind dependency which internally pulls the other two needed dependencies i.e. jackson-annotations and jackson-core. We can find the latest version from the maven site. After importing Jackson, we can start using ObjectMapper class for marshalling and unmarshalling the JSON. 2.

How to work with JSON strings in Java?

1 For java applications, it is very difficult to work with Json strings. ... 2 Jackson is one such Java Json library used for parsing and generating Json files. ... 3 Jackson also has a Jackson Json Parser and Jackson Json Generator which parses and generates json one token at a time.

How to generate JSON from a Java object in objectmapper?

The methods writeValueAsString and writeValueAsBytes of ObjectMapper class generate a JSON from a Java object and return the generated JSON as a string or as a byte array: String carAsString = objectMapper.writeValueAsString(car);


1 Answers

The code should pass an instance of Type A to the writeValueAsString method not the actual class.

The ObjectMapper will be able to determine the instance's type via reflection, however if you don't pass an instance it cannot determine the field value's to place in the generated JSON.

Also be sure to catch or throw the appropriate exceptions.

public void save() {

try {
        A a = new A();
        ObjectMapper mapper = new ObjectMapper();
        String temp = mapper.writeValueAsString(a);

    } catch (JsonGenerationException e) {
       e.printStackTrace();
    } catch (JsonMappingException e) {
       e.printStackTrace();
    } catch (IOException e) {
       e.printStackTrace();
    }

}

You should also have acessors for the fields on class A and class B. NOTICE I included B in the previous sentence, since Jackson must be able to map all fields on the instance we provide to it.

public A   {
    private List<B> b;
    private int c;

    private B getB(){ return b;}
    private void setB(B b){this.b = b;}
    private int getC(){return c;}
    private void setC(int c){this.c = c;}
}

Here is a complete working example that I have tested.

import java.io.IOException;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class A {

    private List<B> b;
    private int c;

    public List<B> getB() {
        return b;
    }

    public void setB(List<B> b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }

    public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
        A a = new A();
        ObjectMapper mapper = new ObjectMapper();
        String temp = mapper.writeValueAsString(a);
        System.out.println(temp);   
    }
}

class B{


}

If this simple example does not work the jackson-mapper-asl.jar file is most likely not on the build path. Download the file from here and place it somewhere on your local machine.

Next via Eclipse, Right Click on your Project and select properties.

Then select Build Path > Libraries > Add External Jars. Locate the jar and then hit OK.

like image 78
Kevin Bowersox Avatar answered Oct 16 '22 23:10

Kevin Bowersox