Exemplo n.º 1
0
 public static void main3(String[] args) {
   String json =
       "{  \"key1\":333, \"arrayKey\":[444, \"array\"], \"key2\" :  {\"key3\" : \"hello\", \"key4\":\"world\" }, \"booleanKey\" : true }   ";
   JsonObject jsonObject = Json.toJsonObject(json);
   System.out.println(jsonObject.getJsonArray("arrayKey"));
   System.out.println(jsonObject.getJsonObject("key2").getString("key4"));
 }
Exemplo n.º 2
0
  @Test
  public void testRuntimeSerialization() {
    Book book = new Book();
    book.setPrice(10.0);
    book.setId(331);
    book.setText("very good");
    book.setSell(true);
    book.setTitle("gook book");
    System.out.println(Json.toJson(book));

    TestBook t = new TestBook();
    t.setObj(new Object());
    t.setBook(book);
    String t0 = Json.toJson(t);
    JsonObject o = Json.toJsonObject(t0);
    Assert.assertThat(o.getJsonObject("book").getInteger("id"), is(331));
    Assert.assertThat(o.getJsonObject("obj"), nullValue());

    t = new TestBook();
    t.setObj(book);
    t.setBook(book);
    String t1 = Json.toJson(t);
    o = Json.toJsonObject(t1);
    Assert.assertThat(o.getJsonObject("book").getInteger("id"), is(331));
    Assert.assertThat(o.getJsonObject("obj").getInteger("id"), is(331));

    t.setObj(new Object());
    String t2 = Json.toJson(t);
    o = Json.toJsonObject(t2);
    Assert.assertThat(o.getJsonObject("book").getInteger("id"), is(331));
    Assert.assertThat(o.getJsonObject("obj"), nullValue());

    t.setObj(book);
    String t3 = Json.toJson(t);
    o = Json.toJsonObject(t3);
    Assert.assertThat(o.getJsonObject("book").getInteger("id"), is(331));
    Assert.assertThat(o.getJsonObject("obj").getInteger("id"), is(331));

    TestBook2<Book> tb2 = new TestBook2<Book>();
    tb2.setObj(book);
    tb2.setBook(null);
    String t4 = Json.toJson(tb2);
    o = Json.toJsonObject(t4);
    Assert.assertThat(o.getJsonObject("book"), nullValue());
    Assert.assertThat(o.getJsonObject("obj").getInteger("id"), is(331));

    tb2.setObj(book);
    String t5 = Json.toJson(tb2);
    o = Json.toJsonObject(t5);
    Assert.assertThat(o.getJsonObject("book"), nullValue());
    Assert.assertThat(o.getJsonObject("obj").getInteger("id"), is(331));

    TestBook2<Object> tb3 = new TestBook2<Object>();
    tb3.setObj(book);
    tb3.setBook(book);
    String t6 = Json.toJson(tb3);
    o = Json.toJsonObject(t6);
    Assert.assertThat(o.getJsonObject("book").getInteger("id"), is(331));
    Assert.assertThat(o.getJsonObject("obj").getInteger("id"), is(331));
  }
Exemplo n.º 3
0
  @Test
  public void testGeneralJsonObject() {
    String json =
        "{\"key1\":333, \"key2\" : {\"key3\" : \"hello\", \"key4\":\"world\" }, \"booleanKey\" : true }   ";
    JsonObject jsonObject = Json.toJsonObject(json);

    Assert.assertThat(jsonObject.getInteger("key1"), is(333));
    Assert.assertThat(jsonObject.getJsonObject("key2").getString("key3"), is("hello"));
    Assert.assertThat(jsonObject.getJsonObject("key2").getString("key4"), is("world"));
    Assert.assertThat(jsonObject.getBoolean("booleanKey"), is(true));
  }
Exemplo n.º 4
0
  @Test
  public void testMixedGeneralJsonArrayAndJsonObject() {
    String json =
        "[333,444,{\"key\" : \"hello\", \"keyObject\" : [\"object0\",\"object1\"  ]},666]";
    JsonArray array = Json.toJsonArray(json);

    Assert.assertThat(array.getJsonObject(2).getJsonArray("keyObject").getString(0), is("object0"));
    Assert.assertThat(array.getJsonObject(2).getJsonArray("keyObject").getString(1), is("object1"));

    json =
        "{\"key1\":333, \"arrayKey\":[444, \"array\"], \"key2\" :  {\"key3\" : \"hello\", \"key4\":\"world\" }, \"booleanKey\" : true }   ";
    JsonObject jsonObject = Json.toJsonObject(json);
    Assert.assertThat(jsonObject.getJsonArray("arrayKey").getString(1), is("array"));
    Assert.assertThat(jsonObject.getJsonObject("key2").getString("key4"), is("world"));
  }