Jackson Library JSON Mapper to String
Asked Answered
I

1

10

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?

Ianthe answered 26/6, 2013 at 11:2 Comment(1)
"Without result" meaning... ? I am guessing that all you need is to add @JsonProperty annotations next to private fields (Jackson follows Bean convention, and private fields are ignored, only public fields serialized by default).Evidentiary
R
15

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.

Rebeckarebeka answered 26/6, 2013 at 11:6 Comment(4)
@Ianthe Did the example help?Rebeckarebeka
If you copied my complete example and it did not run, then its a build path issue. Meaning you do not have the appropriate jar file in your classpath. Are you using Eclipse?Rebeckarebeka
Also, make sure you have get/set methods for each field in Class BRebeckarebeka
I have change something in my Class A and the method save go. I have substitute List<B> b = new List<B>() with List<B> = new ArrayList<B>(). Now i have another problem.. I have the functions save and read in 2 activities, the save go alway but the read function the firts time it go and the second time no. Why?? The same function called in two activities..Ianthe

© 2022 - 2024 — McMap. All rights reserved.