Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a simple test I used at some point to understand how Jackson serializes/deserializes JSON arrays and objects:</p> <pre><code>import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.TypeReference; import org.junit.Test; import static org.junit.Assert.assertEquals; public class JacksonTest { private final ObjectMapper mapper = new ObjectMapper(); public static class Book { public String title; public String author; } @Test public void readWriteJsonObject() throws JsonGenerationException, JsonMappingException, IOException { String json = mapper.writeValueAsString(new Book() {{ title = "Real World Haskell"; author = "Don Stewart"; }}); Book book = mapper.readValue(json, Book.class); assertEquals("Real World Haskell", book.title); assertEquals("Don Stewart", book.author); } @Test @SuppressWarnings("serial") public void readWriteJsonArray() throws JsonGenerationException, JsonMappingException, IOException { List&lt;Book&gt; books = new ArrayList&lt;Book&gt;() {{ add(new Book() {{ title = "Real World Haskell"; author = "Don Stewart"; }}); add(new Book() {{ title = "Learn You Some Erlang"; author = "Fred T. Herbert"; }}); }}; String json = mapper.writeValueAsString(books); books = mapper.readValue(json, new TypeReference&lt;List&lt;Book&gt;&gt;() {}); assertEquals("Real World Haskell", books.get(0).title); assertEquals("Don Stewart", books.get(0).author); assertEquals("Learn You Some Erlang", books.get(1).title); assertEquals("Fred T. Herbert", books.get(1).author); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload