Alberto
Alberto

Reputation: 399

Java - Jackson to a file

I have a JSON file like this:

{
    "Product":
    {
        "ID": "08-17-96-71-D9-68",

        "Licences": 
    {
        "total": 40,
        "used": 0,
        "remain": 40
        }
    }
}

I used jackson to convert it to a Java Object and I get all the values (so far, so good). My problem is that I want to change these values and re-write the JSON file but when I do that, the result is like this:

"{\"Product\":{\"IaD\": \"08-17-96-71-D9-68\",\"Licences\":{\"total\": 40,\"used\": 1,\"remain\": 39}}}"

So when I tried to read it again it gives me an error because it cannot read the first and last character (") and also it reads the \ character.

This is my code:

public class UsingJason {
String theJsonString = "";
ObjectMapper mapper = new ObjectMapper();

public class Product{
    Licences lic;

    public class Licences{
        int total;
        int used;
        int remain;
    }
}


public void readJson(){
    if(new File("asset/testJson.json").exists()){
    theJsonString = "";     
    try {
        BufferedReader in = new BufferedReader(new FileReader("asset/testJson.json"));
        String line;
        while ((line = in.readLine()) != null){
            theJsonString += line;
        }
        in.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    System.out.println("JSON String: "+ theJsonString);
}else{
    System.out.println("NO FILE FOUND");
}
JsonNode rootNode = null;
try {
    rootNode = mapper.readValue(theJsonString, JsonNode.class);
} catch (JsonParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (JsonMappingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
JsonNode totalNode = rootNode.get("Product").get("Licences").get("total");
JsonNode usedNode = rootNode.get("Product").get("Licences").get("used");
JsonNode remainNode = rootNode.get("Product").get("Licences").get("remain");

JsonNode idStringNode = rootNode.get("Product").get("ID");
// Parse it into a Java object.
try {
    int totalObject = mapper.readValue(totalNode, Integer.class);
    System.out.println("INTEGER? HAS TO BE... 40: "+totalObject);
    String idString = mapper.readValue(idStringNode, String.class);
    System.out.println("String? Has to be 08-17-96-71-D9-68: "+idString + "  True? " 
        + idString.equals("08-17-96-71-D9-68")  );
    int usedObject = mapper.readValue(usedNode, int.class);
    int remainObject = mapper.readValue(remainNode, int.class);


    System.out.println("Going to rest 1");

    usedObject ++;
    remainObject = totalObject - usedObject;

    String toJackson = "{\"Product\":{\"I\\D\": \"08-17-96-71-D9-68\",\"Licences\":{\"total\": "+totalObject+",\"used\": "+usedObject+",\"remain\": "+remainObject+"}}}";



    System.out.println("String created: " +toJackson);
             // THIS  toJackson String returns the string without \ and without the " 
             // IT PRINT THIS: {"Product":{"ID": "08-17-96-71-D9-68","Licences":{"total": 40,"used": 1,"remain": 39}}}
             // EXACTLY WHAT I WANT TO Write in the Json file but it writes the \ ..

mapper.writeValue(new File("asset/testJson.json"), toJackson);
} catch (JsonParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (JsonMappingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

Can anyone tell me what I am doing wrong?

Upvotes: 1

Views: 8279

Answers (1)

DRCB
DRCB

Reputation: 2121

In your code here:

mapper.writeValue(new File("asset/testJson.json"), toJackson);

You are serializing not an object, but the string to the file. I suppose this is the reason why it gets escaped, like any string.

The input value should be an object with your structure.

Something like this:

// Initialize an object
Product myProduct = new Product();
myProduct.lic = new Procuct.Licences();
myProduct.lic.total = totalObject;
myProduct.lic.used = usedObject;
myProduct.lic.remain = remainObject;


// Serialize the object into JSON
mapper.writeValue(new File("asset/testJson.json"), myProduct);

Upvotes: 4

Related Questions