Package org.apache.wink.json4j

Examples of org.apache.wink.json4j.JSONArray


    public void test_EmptyAppend() throws Exception {
        Exception ex = null;
        try {
            JSONObject json = new JSONObject();
            json.append("test", "value");
            JSONArray arr = (JSONArray)json.get("test");
            assertTrue(arr.size() == 1);
            assertTrue(arr.get(0).equals("value"));
        }
        catch (Exception ex1) {
            ex = ex1;
            ex.printStackTrace();
       }
View Full Code Here


    public void test_ArrayRetrievalFromJavaArrayInsertion() throws Exception {
        JSONObject json = new JSONObject();
        String[] someArray = { "Hello","World!" };
        json.put("somearray", someArray);
        JSONArray array = json.getJSONArray("somearray");
        assertEquals(array.get(0), "Hello");
        assertEquals(array.get(1), "World!");
    }
View Full Code Here

     * *param array An array instance to populate instead of creating a new one.
     *
     * @throws JSONException Thrown if a parse error occurs, such as a malformed JSON array.
     */
    public JSONArray parseArray(boolean ordered, JSONArray array) throws JSONException {
        JSONArray result = null;
        if(array != null){
            result = array;
        } else {
            result = new JSONArray();
        }

        try {
            if (lastToken != Token.TokenBrackL) throw new JSONException("Expecting '[' " + tokenizer.onLineCol());
            lastToken = tokenizer.next();
            while (true) {
                if (lastToken == Token.TokenEOF) throw new JSONException("Unterminated array " + tokenizer.onLineCol());

                /**
                 * End of the array.
                 */
                if (lastToken == Token.TokenBrackR) {
                    lastToken = tokenizer.next();
                    break;
                }

                Object val = parseValue(ordered);
                result.add(val);

                if (lastToken == Token.TokenComma) {
                    lastToken = tokenizer.next();
                } else if (lastToken != Token.TokenBrackR) {
                    throw new JSONException("expecting either ',' or ']' " + tokenizer.onLineCol());
View Full Code Here

            JSONWriter jWriter = new JSONWriter(w);
            jWriter.array();
            jWriter.endArray();
            String str = w.toString();
            // Verify it parses.
            JSONArray test = new JSONArray(str);
            assertTrue(str.equals("[]"));
        }catch(Exception ex1){
            ex = ex1;
            ex.printStackTrace();
        }
View Full Code Here

            jWriter.array();
            jWriter.close();

            String str = w.toString();
            // Verify it parses.
            JSONArray test = new JSONArray(str);
            assertTrue(str.equals("[]"));
        }catch(Exception ex1){
            ex = ex1;
            ex.printStackTrace();
        }
View Full Code Here

            JSONWriter jWriter = new JSONWriter(w);
            jWriter.object();
            jWriter.key("foo");

            // Verify we can put a JSONObject into the stream!
            JSONArray jArray = new JSONArray();
            jArray.put(true);
            jWriter.value(jArray);

            jWriter.endObject();
            jWriter.close();
View Full Code Here

            jWriter.close();
            String str = w.toString();

            // Verify it parses.
            JSONArray test = new JSONArray(str);
            assertTrue(str.equals("[\"String1\",false,1,{\"string\":\"String2\"},[1,2.0,3]]"));
        }catch(Exception ex1){
            ex = ex1;
            ex.printStackTrace();
        }
View Full Code Here

            } 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);
            }
            else {
                //TODO:  Bean introspection time.
                ja = introspectBean(obj,includeSuperclass, new ArrayList());
            }
View Full Code Here

                                } 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));
                                    }
View Full Code Here

                                                            m = tM;
                                                            if(c != JSONArray.class){
                                                                // Convert it.  Whee.
                                                                List list = (List)c.newInstance();

                                                                JSONArray array = (JSONArray)val;
                                                                for (int j = 0; j < array.length(); j++) {
                                                                    // Convert each type as needed.
                                                                    Object aVal = array.get(j);
                                                                    if(aVal != null){
                                                                        Class aVClazz = aVal.getClass();
                                                                        if(Number.class.isAssignableFrom(aVClazz) ||
                                                                            Boolean.class.isAssignableFrom(aVClazz) ||
                                                                            String.class.isAssignableFrom(aVClazz)) {
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.