使用jackson反序列化枚举

时间:2015-10-30 14:32:04

标签: java json enums jackson

花了一些时间搞清楚反序列化的问题但无济于事。

我试图通过以下代码从同一个类Movie创建的JSON文件中回读。但它正在崩溃这段代码。

List<Movie> myMovies = objectMapper.readValue(f,objectMapper.getTypeFactory().constructCollectionType(List.class, Movie.class));

JSON文件的示例为

[
    {
        "movieID": 0,
        "title": "asdad 1",
        "type": "DIGITAL",
        "synopsis": null,
        "director": null,
        "casts": [],
        "status": "COMING SOON",
        "showTimes": null,
        "cinema": null,
        "mRating": "G",
        "rRating": 0,
        "reviews": [],
        "totalSales": 0
    }
]

以下是我Movie

上的一堆属性声明
private int movieID;
private String title;
private MovieType type;
private String synopsis;
private String director;
private List<String> casts = new ArrayList<String>();
private MovieStatus status;
private List<ShowTime> showTimes;
private Cinema cinema;
private MovieRating mRating;
private ReviewersRating rRating;
private List<String> reviews = new ArrayList<String>();
private int totalSales;

堆栈跟踪

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
 at [Source: .\Database\Movies.json; line: 1, column: 81] (through reference chain: java.util.ArrayList[0]->entity.Movie["casts"])
    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:166)

我的枚举示例,以覆盖默认的JsonProperty

public enum ReviewersRating {
    _0(0),_1(1), _2(2), _3(3), _4(4), _5(5);

    private final int rating;
    private ReviewersRating(final int rating) {
        this.rating = rating;
    }

    @JsonValue
    public int toInt()
    {
        return this.rating;
    }
}
public enum MovieStatus {
    @JsonProperty("COMING SOON")COMING_SOON, PREVIEW, @JsonProperty("NOW SHOWING")NOW_SHOWING, @JsonProperty("END OF SHOWING")END_OF_SHOWING;
}
public enum MovieRating {
    G, PG, PG13, NC16, M18, R21;
}
public enum MovieType {
    DIGITAL, @JsonProperty("3D")_3D, BLOCKBUSTER;
}

我的Movie班级代码。写入似乎工作正常,因为我能够生成上面的JSON字符串。只是阅读中有问题我猜?     import java.util。;     import java.io。;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;



public class Movie {

    private int movieID;
    private String title;
    private MovieType type;
    private String synopsis;
    private String director;
    private List<String> casts = new ArrayList<String>();
    private MovieStatus status;
    private List<ShowTime> showTimes;
    private Cinema cinema;
    private MovieRating mRating;
    private ReviewersRating rRating;
    private List<String> reviews = new ArrayList<String>();
    private int totalSales;

    public Movie() {

    }

    public Movie(String title, MovieType type, MovieRating mRating,
            MovieStatus status) {
        this.title = title;
        this.type = type;
        this.mRating = mRating;
        this.status = status;
        rRating = ReviewersRating._0;
    }

    public int getMovieID() {
        return movieID;
    }

    public void setMovieID(int movieID) {
        this.movieID = movieID;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public MovieType getType() {
        return type;
    }

    public void setType(MovieType type) {
        this.type = type;
    }

    public String getSynopsis() {
        return synopsis;
    }

    public void setSynopsis(String synopsis) {
        this.synopsis = synopsis;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public List<String> getCasts() {
        return casts;
    }

    public void setCasts(String cast) {
        this.casts.add(cast);
    }

    public MovieStatus getStatus() {
        return status;
    }

    public void setStatus(MovieStatus status) {
        this.status = status;
    }

    public void addShowTime(ShowTime showTime) {
        showTimes.add(showTime);
    }

    public void removeShowTime(ShowTime showTime) {
        showTimes.remove(showTime);
    }

    public void setShowTimes(List<ShowTime> showTimes) {
        this.showTimes = showTimes;
    }

    public List<ShowTime> getShowTimes() {
        return showTimes;
    }

    public Cinema getCinema() {
        return cinema;
    }

    public void setCinema(Cinema cinema) {
        this.cinema = cinema;
    }

    public MovieRating getmRating() {
        return mRating;
    }

    public void setmRating(MovieRating mRating) {
        this.mRating = mRating;
    }

    public ReviewersRating getrRating() {
        return rRating;
    }

    public void setrRating(ReviewersRating rRating) {
        this.rRating = rRating;
    }

    public List<String> getReviews() {
        return reviews;
    }

    public void setReviews(String reviews) {
        this.reviews.add(reviews);
    }

    public float getTotalSales() {
        return totalSales;
    }

    public void setTotalSales(int totalSales) {
        this.totalSales = totalSales;
    }

    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
            try
            {
                List<Movie> myMovies = objectMapper.readValue(f,objectMapper.getTypeFactory().constructCollectionType(List.class, Movie.class));
                myMovies.add(this);
                objectMapper.writeValue(new FileOutputStream(f), myMovies);
            }
            catch (JsonMappingException e) {
                e.printStackTrace();
            } catch (JsonGenerationException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else
        {
            try {
                objectMapper.writeValue(new FileOutputStream(f), Collections.singleton(this));
            }
            catch (JsonMappingException e) {
                e.printStackTrace();
            } catch (JsonGenerationException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

0 个答案:

没有答案