Package org.apache.wink.json4j

Examples of org.apache.wink.json4j.JSONArray


                                null,
                                MediaType.APPLICATION_JSON_TYPE,
                                null,
                                os);
        InputStream is = new ByteArrayInputStream(os.toByteArray());
        JSONArray jsonArray =
            (JSONArray)json4jArrayProvider.readFrom(JSONArray.class,
                                                    JSONArray.class,
                                                    null,
                                                    MediaType.APPLICATION_JSON_TYPE,
                                                    null,
                                                    is);
        assertEquals(2, jsonArray.size());
        assertEquals("firstName", ((JSONObject)jsonArray.get(0)).get("first"));
        assertEquals("lastName", ((JSONObject)jsonArray.get(0)).get("last"));
        assertEquals(45, ((JSONObject)jsonArray.get(0)).get("age"));
        assertEquals("firstName2", ((JSONObject)jsonArray.get(1)).get("first"));
        assertEquals("lastName2", ((JSONObject)jsonArray.get(1)).get("last"));
        assertEquals(46, ((JSONObject)jsonArray.get(1)).get("age"));
    }
View Full Code Here


        assertEquals("lastName2", ((JSONObject)jsonArray.get(1)).get("last"));
        assertEquals(46, ((JSONObject)jsonArray.get(1)).get("age"));
    }

    public void testArrayInteroperabilityJSON4JToJackson() throws Exception {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        jsonObject1.put("first", "firstName");
        jsonObject1.put("last", "lastName");
        jsonObject1.put("age", 45);
        jsonObject2.put("first", "firstName2");
        jsonObject2.put("last", "lastName2");
        jsonObject2.put("age", 46);
        jsonArray.add(jsonObject1);
        jsonArray.add(jsonObject2);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        json4jArrayProvider.writeTo(jsonArray,
                                    JSONArray.class,
                                    JSONArray.class,
                                    null,
View Full Code Here

            MockRequestConstructor.constructMockRequest("GET",
                                                        "/test/jsonarray",
                                                        "application/json");
        MockHttpServletResponse response = invoke(request);
        assertEquals(200, response.getStatus());
        JSONArray result = new JSONArray(new StringReader(response.getContentAsString()));
        JSONArray want = new JSONArray(JSON_ARRAY);
        assertEquals(want.toString(), result.toString());
    }
View Full Code Here

                                                        "application/json",
                                                        MediaType.APPLICATION_JSON,
                                                        JSON_ARRAY.getBytes());
        MockHttpServletResponse response = invoke(request);
        assertEquals(200, response.getStatus());
        JSONArray result = new JSONArray(new StringReader(response.getContentAsString()));
        JSONArray want = new JSONArray(JSON_ARRAY).put(Collections.singletonMap("foo", "bar"));
        assertEquals(want.toString(), result.toString());
    }
View Full Code Here

        @GET
        @Path("jsonarray")
        @Produces(MediaType.APPLICATION_JSON)
        public JSONArray getJsonArray() throws Exception {
            return new JSONArray(JSON_ARRAY);
        }
View Full Code Here

    /**
     * Test the construction from a simple map.
     */
    public void test_newFromMap() {
        JSONObject jObject = null;
        JSONArray jArr = null;
        Exception ex = null;
        HashMap map = new HashMap();
        map.put("string", "This is a string");
        map.put("null", null);
        map.put("integer", new Integer(1));
        map.put("bool", new Boolean(true));
        map.put("strArr", new String[]{"first", "second", "third"});

        // Load a JSON object from a map with JSONable values.
        try {
            jObject = new JSONObject(map);
            jArr = (JSONArray)jObject.get("strArr");
        } catch (Exception ex1) {
            ex = ex1;
            ex.printStackTrace();
        }
        assertTrue(jObject != null);
        assertTrue(jObject.length() == 5);
        assertTrue("first".equals((String)jArr.get(0)));
        assertTrue("second".equals((String)jArr.get(1)));
        assertTrue("third".equals((String)jArr.get(2)));

        assertTrue(ex == null);
    }
View Full Code Here

     */
    public void test_putJSONArray() {
        Exception ex = null;
        try {
            JSONObject jObject = new JSONObject();
            jObject.put("array", new JSONArray());
            JSONArray obj = (JSONArray)jObject.get("array");
            assertTrue(obj != null);
            assertTrue(obj instanceof JSONArray);
            assertTrue(((JSONArray)jObject.get("array")).toString().equals("[]"));
        } catch (Exception ex1) {
            ex = ex1;
View Full Code Here

            jObject.put("string", "Hello World.");
            String s = (String)jObject.get("string");
            assertTrue(s != null);
            assertTrue(s instanceof java.lang.String);
            jObject.append("string", "Another string.");
            JSONArray array = (JSONArray)jObject.get("string");
            assertTrue(array != null);
            assertTrue(array instanceof JSONArray);
        } catch (Exception ex1) {
            ex = ex1;
            ex.printStackTrace();
View Full Code Here

            jObject.put("null", (Object)null);
            String s = (String)jObject.get("null");
            assertTrue(s == null);
            assertTrue(jObject.has("null"));
            jObject.append("null", "Another string.");
            JSONArray array = (JSONArray)jObject.get("null");
            assertTrue(array != null);
            assertTrue(array instanceof JSONArray);
            assertTrue(array.size() == 2);
            assertTrue(array.get(0) == null);
            assertTrue(array.get(1).equals("Another string."));
        } catch (Exception ex1) {
            ex = ex1;
            ex.printStackTrace();
        }
        assertTrue(ex == null);
View Full Code Here

     */
    public void test_appendArray() {
        Exception ex = null;
        try {
            JSONObject jObject = new JSONObject();
            JSONArray array = new JSONArray();
            array.add("Hello World.");
            jObject.put("array", array);
            JSONArray array1 = (JSONArray)jObject.get("array");
            assertTrue(array1 != null);
            assertTrue(array1 instanceof JSONArray);
            assertTrue(array1 == array);
            assertTrue(array1.size() == 1);
            jObject.append("array", "Another string.");
            JSONArray array2 = (JSONArray)jObject.get("array");
            assertTrue(array2 != null);
            assertTrue(array2 instanceof JSONArray);
            assertTrue(array1 == array2);
            assertTrue(array2.size() == 2);
        } catch (Exception ex1) {
            ex = ex1;
            ex.printStackTrace();
        }
        assertTrue(ex == null);
View Full Code Here

TOP

Related Classes of org.apache.wink.json4j.JSONArray

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.