Gavin
Gavin

Reputation: 2834

Reading from JSON File

I have a Movie class and I have to store a database in the form of JSON File on directory Database\Movies.JSON The movie class has a method for admin to add new movie list to the database.

The thing is, when the first movie is created, the JSON is not an array of movies object. When I try to add subsequent movies I need to read from it and create it as an array of movie objects. Is there a neat way to do so on Jackson JSON API?

I am using the latest Jackson 2.6.3

public void createMovie() {
    ObjectMapper objectMapper = new ObjectMapper();
    File f = new File("./Database/Movies.json");
    if (f.exists() && !f.isDirectory()) { // if existing data exist, read
                                            // from it and append new movie
                                            // data
    } else {
        try {
            objectMapper.writeValue(new FileOutputStream(f), this);
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I have since created it as an array of movie objects.

objectMapper.writeValue(new FileOutputStream(f), Collections.singleton(this));

But when I try to use the below for handling exisiting datas, it crashes on the first line

List<Movie> myMovies = objectMapper.readValue(f,objectMapper.getTypeFactory().constructCollectionType(List.class, Movie.class));
myMovies.add(this);
objectMapper.writeValue(new FileOutputStream(f), myMovies);

Error trace

at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:857)
    at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:62)
    at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2658)
    at entity.Movie.createMovie(Movie.java:167)

Sample JSON Data

   [
    {
        "movieID": 0,
        "title": "213131 13213 axda",
        "type": "DIGITAL",
        "synopsis": null,
        "director": null,
        "casts": [],
        "status": "COMING_SOON",
        "showTimes": null,
        "cinema": null,
        "mRating": "G",
        "rRating": "_0",
        "reviews": [],
        "totalSales": 0
    }
]

Upvotes: 0

Views: 1038

Answers (1)

R&#233;my Saintobert
R&#233;my Saintobert

Reputation: 11

To avoid dealing with this array/non array problem, you should always store a json array in your file. You can add your first movie as an array containing this single element. You can do it with :

objectMapper.writeValue(new FileOutputStream(f), Collections.singleton(this));

Upvotes: 1

Related Questions