### Question:
JsonWritable implements Writable { public byte[] serialize() throws IOException { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); DataOutputStream dataOut = new DataOutputStream(bytesOut); write(dataOut); return bytesOut.toByteArray(); } JsonWritable();  JsonWritable(String s); void readFields(DataInput in); void write(DataOutput out); byte[] serialize(); JsonObject getJsonObject(); @Override String toString(); static JsonWritable create(DataInput in); static JsonWritable create(byte[] bytes);  }### Answer:
@Test public void testSerialize() throws Exception { JsonWritable w = new JsonWritable(); JsonObject obj = w.getJsonObject(); obj.addProperty("firstName", "John"); obj.addProperty("lastName", "Smith"); JsonObject address = new JsonObject(); address.addProperty("streetAddress", "21 2nd Street"); address.addProperty("city", "New York"); address.addProperty("state", "NY"); address.addProperty("postalCode", 10021); JsonArray phoneNumbers = new JsonArray(); phoneNumbers.add(new JsonPrimitive("212 555-1234")); phoneNumbers.add(new JsonPrimitive("646 555-4567")); obj.add("address", address); obj.add("phoneNumbers", phoneNumbers); byte[] bytes = w.serialize(); JsonWritable w2 = JsonWritable.create(bytes); String s = "{\"firstName\":\"John\",\"lastName\":\"Smith\"," + "\"address\":{\"streetAddress\":\"21 2nd Street\",\"city\":\"New York\",\"state\":\"NY\",\"postalCode\":10021}," + "\"phoneNumbers\":[\"212 555-1234\",\"646 555-4567\"]}"; assertEquals(s, w2.getJsonObject().toString()); JsonObject obj2 = w2.getJsonObject(); assertEquals("John", obj2.get("firstName").getAsString()); assertEquals("Smith", obj2.get("lastName").getAsString()); assertEquals("New York", obj2.getAsJsonObject("address").get("city").getAsString()); assertEquals(2, obj2.getAsJsonArray("phoneNumbers").size()); assertEquals("212 555-1234", ((JsonPrimitive) obj2.getAsJsonArray("phoneNumbers").get(0)).getAsString()); }