Package org.apache.wink.json4j

Examples of org.apache.wink.json4j.JSONObject


        Assert.assertEquals("JSON-RPC", jsonResp.getJSONObject("result").getJSONObject("map").getString("Binding"));
    }

    @Test
    public void testBean() throws Exception {
        JSONObject jsonRequest = new JSONObject(
        "{ \"method\": \"echoBean\", \"params\": [ {\"javaClass\": \"bean.TestBean\", \"testString\": \"JSON-RPC\", \"testInt\":1234}], \"id\": 7}");

        WebConversation wc = new WebConversation();
        WebRequest request = new PostMethodWebRequest(SERVICE_URL,
                                                      new ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")), "application/json");
        WebResponse response = wc.getResource(request);

        Assert.assertEquals(200, response.getResponseCode());

        JSONObject jsonResp = new JSONObject(response.getText());

        Assert.assertEquals("JSON-RPC", jsonResp.getJSONObject("result").getString("testString"));
   
View Full Code Here


        Assert.assertEquals("JSON-RPC", jsonResp.getJSONObject("result").getString("testString"));
   

    @Test
    public void testList() throws Exception {
        JSONObject jsonRequest = new JSONObject(
        "{ \"method\": \"echoList\", \"params\": [[0,1,2,3,4]], \"id\": 8}");

        WebConversation wc = new WebConversation();
        WebRequest request = new PostMethodWebRequest(SERVICE_URL,
                                                      new ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")), "application/json");
        WebResponse response = wc.getResource(request);

        Assert.assertEquals(200, response.getResponseCode());

        JSONObject jsonResp = new JSONObject(response.getText());

        Assert.assertEquals(0, jsonResp.getJSONArray("result").get(0));
    }
View Full Code Here

        Assert.assertEquals(0, jsonResp.getJSONArray("result").get(0));
    }

    @Test
    public void testArrayString() throws Exception {
        JSONObject jsonRequest = new JSONObject(
        "{\"params\":[[\"1\",\"2\"]],\"method\":\"echoArrayString\",\"id\":9}");

        WebConversation wc = new WebConversation();
        WebRequest request = new PostMethodWebRequest(SERVICE_URL,
                                                      new ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")), "application/json");
        WebResponse response = wc.getResource(request);

        Assert.assertEquals(200, response.getResponseCode());

        JSONObject jsonResp = new JSONObject(response.getText());

        Assert.assertEquals("1", jsonResp.getJSONArray("result").getString(0));
   
View Full Code Here

   


    @Test
    public void testArrayInt() throws Exception {
        JSONObject jsonRequest = new JSONObject(
        "{\"params\":[[1,2]],\"method\":\"echoArrayInt\",\"id\":10}");

        WebConversation wc = new WebConversation();
        WebRequest request = new PostMethodWebRequest(SERVICE_URL,
                                                      new ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")), "application/json");
        WebResponse response = wc.getResource(request);

        Assert.assertEquals(200, response.getResponseCode());

        JSONObject jsonResp = new JSONObject(response.getText());

        Assert.assertEquals(1, jsonResp.getJSONArray("result").getInt(0));
   
View Full Code Here

   


    @Test
    public void testSet() throws Exception {
        JSONObject jsonRequest = new JSONObject(
        "{ \"method\": \"echoSet\", \"params\": [[\"red\", \"blue\"]],\"id\": 11}");

        WebConversation wc = new WebConversation();
        WebRequest request = new PostMethodWebRequest(SERVICE_URL,
                                                      new ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")), "application/json");
        WebResponse response = wc.getResource(request);

        Assert.assertEquals(200, response.getResponseCode());

        JSONObject jsonResp = new JSONObject(response.getText());

        Assert.assertEquals("red", jsonResp.getJSONArray("result").get(0));
    }
View Full Code Here

        Assert.assertEquals("red", jsonResp.getJSONArray("result").get(0));
    }
   
    @Test
    public void testBigDecimal() throws Exception {
        JSONObject jsonRequest = new JSONObject(
                "{ \"method\": \"echoBigDecimal\", \"params\": [\"12345.67\"], \"id\": 4}");

        WebConversation wc = new WebConversation();
        WebRequest request = new PostMethodWebRequest(SERVICE_URL,
                new ByteArrayInputStream(jsonRequest.toString().getBytes("UTF-8")), "application/json");
        WebResponse response = wc.getResource(request);

        Assert.assertEquals(200, response.getResponseCode());

        JSONObject jsonResp = new JSONObject(response.getText());

        Assert.assertEquals(12345.67, jsonResp.get("result"));
    }   
View Full Code Here

                logger.logp(Level.FINEST, className, "transform", "Parsing the JSON and a DOM builder.");
            }

            try {
                //Get the JSON from the stream.
                JSONObject jObject = new JSONObject(JSONStream);

                //Create a new document

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbf.newDocumentBuilder();
View Full Code Here

                               Annotation[] annotations,
                               MediaType mediaType,
                               MultivaluedMap<String, String> headers,
                               InputStream is) throws IOException, WebApplicationException {
        try {
            return new JSONObject(new InputStreamReader(is, ProviderUtils.getCharset(mediaType)));
        } catch (JSONException e) {
            throw new WebApplicationException(e, 400);
        }
    }
View Full Code Here

            } else if (JSONObject.class.isAssignableFrom(clazz)) {
                ja = (JSONObject)obj;
            } else if (JSONArray.class.isAssignableFrom(clazz)) {
                ja = (JSONArray)obj;
            } else if (Map.class.isAssignableFrom(clazz)) {
                ja = new JSONObject((Map)obj);
            } else if (Collection.class.isAssignableFrom(clazz)) {
                ja = new JSONArray((Collection)obj);
            } else if (clazz.isArray()) {
                ja = new JSONArray((Object[])obj);
            }
View Full Code Here

     * @param includeSuperclass Boolean indicating if superclass properties should be included in the output JSON.                      
     * @param parsedObjects An array list of objects traversed to try and avoid loops in graphs
     * @throws JSONException Thrown if a JSON conversion error occurs.
     */
    private static JSONArtifact introspectBean(Object obj, boolean includeSuperclass, ArrayList parsedObjects) throws JSONException {
        JSONObject ja = null;
        boolean found = false;
        for (int i = 0; i < parsedObjects.size(); i++) {
            // Check and try to avoid graphs by parsing the same
            // object multiple times, which may indicate a cycle.
            Object possibleObj = parsedObjects.get(i);
            if (possibleObj != null && obj == possibleObj) {
                found = true;
                break;
            }
        }

        if (!found) {
            parsedObjects.add(obj);
            ja = new JSONObject();

            Class clazz = obj.getClass();

            ja.put("_type", "JavaClass");
            ja.put("_classname", clazz.getName());

            // Fetch all the methods, based on including superclass or not.
            Method[] methods = null;
            if (includeSuperclass) {
                methods = clazz.getMethods();
            } else {
                methods = clazz.getDeclaredMethods();
            }

            if (methods != null && methods.length > 0) {
                for (int i = 0; i < methods.length; i++) {
                    Method m = methods[i];
                    // Include all superclass methods if requested,
                    // or only those that are part of the actual declaring class.
                    String mName = m.getName();
                    Class[] types = m.getParameterTypes();

                    // Getter, so we can assume this accesses a field.
                    if (mName.startsWith("get") && mName.length() > 3 && (types == null || types.length == 0)) {
                        String attr = mName.substring(3, mName.length());
                        attr = Character.toLowerCase(attr.charAt(0)) + attr.substring(1, attr.length());
                        try {
                            Object val = m.invoke(obj, null);
                            if (val == null) {
                                ja.put(attr, (Object)null);
                            } else {
                                Class vClazz = val.getClass();
                                if (String.class == vClazz) {
                                    ja.put(attr, val);
                                } else if (Boolean.class == vClazz) {
                                    ja.put(attr, val);
                                } else if (Class.class == vClazz) {
                                    ja.put(attr, ((Class)val).getName());
                                } else if (Number.class.isAssignableFrom(vClazz)) {
                                    ja.put(attr, val);
                                } else if (JSONObject.class.isAssignableFrom(vClazz)) {
                                    ja.put(attr, val);
                                } else if (JSONArray.class.isAssignableFrom(vClazz)) {
                                    ja.put(attr, val);
                                } else if (Map.class.isAssignableFrom(vClazz)) {
                                    ja.put(attr, new JSONObject((Map)val));
                                } else if (Collection.class.isAssignableFrom(vClazz)) {
                                    ja.put(attr, new JSONArray((Collection)obj));
                                } else {
                                    if (val != obj) {
                                        // Try to avoid processing references to itself.
                                        ja.put(attr, introspectBean(val, includeSuperclass, parsedObjects));
                                    }
                                }
                            }
                        } catch (Exception ex) {
                            ja.put(attr, (Object)null);
                        }
                    }
                }
            }
        }
View Full Code Here

TOP

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

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.