0

I have the following JSON:

{
       "book":{
             "isbn" : "12356789",
             "title" : "Algorithm",
             "author" : [
                           "Cormen",
                           "Rivest",
                           "Stein"
             ],
             "price" : 45.78
       }
}

I need to convert this JSON string to a Book class. I don't want to set it property by property. Also, I don't want to use Gson.

I want to do something as:

Book book=jsonReader.readObject().toClass(Book.class);

How can I do it with javax.json.Json or Moxy?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
user3449953
  • 43
  • 1
  • 8

3 Answers3

1

I would recommend NOT using anything other than Jackson to process json.

A Jackson based solution for the JSON you have pasted would resemble this:-

First create a POJO for your book object

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class BookVO {

    private final String isbn;
    private final String title;
    private final String[] author;
    private final double price;

    @JsonCreator
    public BookVO(@JsonProperty("isbn") final String isbn, @JsonProperty("title") final String title, @JsonProperty("author") final String[] author, @JsonProperty("price") final double price) {
        super();
        this.isbn = isbn;
        this.title = title;
        this.author = author;
        this.price = price;
    }

    public String getIsbn() {
        return isbn;
    }

    public String getTitle() {
        return title;
    }

    public String[] getAuthor() {
        return author;
    }

    public double getPrice() {
        return price;
    }

}

You then need a Book container POJO

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Books {

    private final BookVO book;

    @JsonCreator
    public Books(@JsonProperty("book") final BookVO book) {
        super();
        this.book = book;
    }

    public BookVO getBook() {
        return book;
    }
}

Finally you need to convert JSON to Java Objects as follows:-

public static void main(final String[] args) throws JsonParseException, JsonMappingException, IOException {
    final ObjectMapper mapper = new ObjectMapper();
    final Books books = mapper.readValue(new File("book.json"), Books.class);

    System.out.println(books);

}

The content of book.json is

{
       "book":{
             "isbn" : "12356789",
             "title" : "Algorithm",
             "author" : [
                           "Cormen",
                           "Rivest",
                           "Stein"
             ],
             "price" : 45.78
       }
}
Hector
  • 4,016
  • 21
  • 112
  • 211
0

I've used Jackson parser to do this. Take a look at the ObjectMapper class.

public static <T> Object getObjectFromJsonString(String json,
        Class<T> className) throws JsonParseException,
        JsonMappingException, IOException {
    InputStream is = new ByteArrayInputStream(json.getBytes("UTF-8"));
    return objectMapper.readValue(is, className);
}
Farhad
  • 388
  • 2
  • 13
0

Thanks for your answers. If I use Jackson, I will have to add another library to my project (something that I don't want to do) I found the answer to my problem here http://blog.bdoughan.com/2013/07/eclipselink-moxy-and-java-api-for-json.html

user3449953
  • 43
  • 1
  • 8