Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObjectMapper append file JSON

Trying to learn about Jackson some, so I'm writing a simple program that reads a file/creates one to store some JSON in it. From the Jackson website I figured out how to read and write from the file, but in the case of my rudimentary program, i'd like to append as well. I'm basically trying to store a list of shopping lists. There is a shopping list object which has store name, amd items for that store.

The trouble is that I cannot figure a way to append another entry to the end of the file (in JSON format). Here is what I am working with so far, you can ignore the first bit it's just a silly console scanner asking for input:

    public class JacksonExample {

    static ObjectMapper mapper = new ObjectMapper();
    static File file = new File("C:/Users/stephen.protzman/Desktop/user.json");
    static List<ShoppingList> master = new ArrayList<ShoppingList>();

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        boolean running = true;
        while (running) {
            System.out.println("[ 1 ] Add a new shopping list");
            System.out.println("[ 2 ] View all shopping lists");
            System.out.println("[ 3 ] Save all shopping lists");
            int choice = Integer.parseInt(in.nextLine());
            switch (choice) {
            case 1:
                getNewList();
            case 2:
                display();
            case 3:
                running = false;
            }
        }
        in.close();
    }

    public static void getNewList() {
        boolean more = true;
        String store, temp;
        List<String> items = new ArrayList<String>();
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the store: ");
        store = s.nextLine();
        System.out.println("Enter each item [If done type 'DONE'] :");
        while (more) {

            temp = s.nextLine();
            if (temp != null) {
                if (temp.toUpperCase().equals("DONE")) {
                    more = false;
                } else {
                    items.add(temp);
                }
            }

        }
        save(store, items);
        s.close();

    }

    public static void display() {
        try {
            ShoppingList list = mapper.readValue(file, ShoppingList.class);
            System.out.println(mapper.defaultPrettyPrintingWriter()
                    .writeValueAsString(list));

        } 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();
        }
    }

    public static void save(String store, List<String> items) {
        //load in old one
        try {
            ShoppingList list = mapper.readValue(file, ShoppingList.class);
            System.out.println(mapper.defaultPrettyPrintingWriter()
                    .writeValueAsString(list));

        } 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();
        }
        //add to end of older list
        ShoppingList tempList = new ShoppingList();
        tempList.setStore(store);
        tempList.setItems(items);

        master.add(tempList);

        try {

            mapper.writeValue(file, master);


        } catch (JsonGenerationException e) {

            e.printStackTrace();

        } catch (JsonMappingException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
    }

}

I want to keep using ObjectMapper (considering im trying to learn Jackson) I just havent found a way to append yet is all. Any ideas?

like image 796
erp Avatar asked Mar 12 '15 19:03

erp


People also ask

How do I read JSON file with ObjectMapper?

Read Object From JSON via URL ObjectMapper objectMapper = new ObjectMapper(); URL url = new URL("file:data/car. json"); Car car = objectMapper. readValue(url, Car. class);

How do I add a JSON string to an existing JSON file in Java?

In the initial step, we can read a JSON file and parsing to a Java object then need to typecast the Java object to a JSonObject and parsing to a JsonArray. Then iterating this JSON array to print the JsonElement. We can create a JsonWriter class to write a JSON encoded value to a stream, one token at a time.

What is the use of Jackson ObjectMapper?

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.

What is ObjectMapper in Rest assured?

Interface ObjectMapperAn object mapper is used to serialize and deserialize a Java object to and from a String, byte[] or InputStream. REST Assured provides mappers for XML and JSON out of the box (see ObjectMapperType ) but you can implement this interface to roll your own mapper implementations for custom formats.


2 Answers

To append content, you need to use Streaming API to create JsonGenerator; and then you can give this generator to ObjectMapper to write to. So something like:

JsonGenerator g = mapper.getFactory().createGenerator(outputStream);
mapper.writeValue(g, valueToWrite);
// and more
g.close();
like image 80
StaxMan Avatar answered Sep 19 '22 04:09

StaxMan


Below method can be used to write objects into a json file in append mode. it first reads your existing json file and adds new java objects to JSON file.

public static void appendWriteToJson() {

    ObjectMapper mapper = new ObjectMapper();

    try {
        // Object to JSON in file
        JsonDaoImpl js = new JsonDaoImpl();
        URL resourceUrl = js.getClass().getResource("/data/actionbean.json");
        System.out.println(resourceUrl);
        File file = new File(resourceUrl.toURI());

        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true))); // append mode file writer

        mapper.writeValue(out, DummyBeanObject);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
like image 32
Dean Jain Avatar answered Sep 22 '22 04:09

Dean Jain