jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1207059 - in /jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast: ./ Jsop.java JsopArray.java JsopObject.java JsopObjectTest.java
Date Mon, 28 Nov 2011 10:42:55 GMT
Author: thomasm
Date: Mon Nov 28 10:42:54 2011
New Revision: 1207059

URL: http://svn.apache.org/viewvc?rev=1207059&view=rev
Log:
Jsop object and array classes, to speed up sparse parsing of large jsop objects. Not used
in the code, so currently only in the test folder.

Added:
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/Jsop.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopArray.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObject.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObjectTest.java

Added: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/Jsop.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/Jsop.java?rev=1207059&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/Jsop.java
(added)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/Jsop.java
Mon Nov 28 10:42:54 2011
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mk.json.fast;
+
+import java.math.BigDecimal;
+import org.apache.jackrabbit.mk.json.JsopBuilder;
+import org.apache.jackrabbit.mk.json.JsopTokenizer;
+
+/**
+ * A Jsop object.
+ */
+public class Jsop {
+
+    protected String jsop;
+    protected int start, end;
+
+    Jsop(String jsop, int start, int end) {
+        this.jsop = jsop;
+        this.start = start;
+        this.end = end;
+    }
+
+    public static String toString(Object o) {
+        if (o == null) {
+            return "null";
+        } else if (o instanceof String) {
+            return JsopBuilder.encode((String) o);
+        }
+        return o.toString();
+    }
+
+    public static Object parse(String jsop) {
+        return parse(jsop, 0);
+    }
+
+    private static Object parse(String jsop, int start) {
+        if (jsop == null) {
+            return null;
+        }
+       char c = jsop.charAt(start);
+       switch(c) {
+       case '{':
+           return new JsopObject(jsop, start, Integer.MAX_VALUE);
+       case '[':
+           return new JsopArray(jsop, start, Integer.MAX_VALUE);
+       case 'n':
+           return null;
+       case 't':
+           return Boolean.TRUE;
+       case 'f':
+           return Boolean.FALSE;
+       case '\"':
+           return readString(jsop, start + 1);
+       case '-':
+           return readNumber(jsop, start);
+       default:
+           if (c >= '0' && c <= '9') {
+               return readNumber(jsop, start);
+           }
+       }
+       throw new IllegalArgumentException("Invalid jsop: " + jsop.substring(start));
+    }
+
+    private static String readString(String j, int start) {
+        int pos = start;
+        int length = j.length();
+        boolean escaped = false;
+        while (pos < length) {
+            char c = j.charAt(pos++);
+            if (c == '\\') {
+                escaped = true;
+                pos++;
+            } else if (c == '"') {
+                break;
+            }
+        }
+        if (escaped) {
+            return JsopTokenizer.decode(j.substring(start, pos - 1));
+        }
+        return j.substring(start, pos - 1);
+    }
+
+    private static BigDecimal readNumber(String j, int start) {
+        int pos = start;
+        int length = j.length();
+        char ch = j.charAt(pos);
+        if (ch == '-') {
+            pos++;
+        }
+        while (true) {
+            ch = j.charAt(pos);
+            if (ch >= '0' && ch <= '9') {
+                while (pos < length) {
+                    ch = j.charAt(pos);
+                    if (ch < '0' || ch > '9') {
+                        break;
+                    }
+                    pos++;
+                }
+                if (ch == '.') {
+                    pos++;
+                    while (pos < length) {
+                        ch = j.charAt(pos);
+                        if (ch < '0' || ch > '9') {
+                            break;
+                        }
+                        pos++;
+                    }
+                }
+                if (ch == 'e' || ch == 'E') {
+                    ch = j.charAt(++pos);
+                    if (ch == '+' || ch == '-') {
+                        ch = j.charAt(++pos);
+                    }
+                    while (pos < length) {
+                        ch = j.charAt(pos);
+                        if (ch < '0' || ch > '9') {
+                            break;
+                        }
+                        pos++;
+                    }
+                }
+                break;
+            }
+        }
+        return new BigDecimal(j.substring(start, pos));
+    }
+
+}

Added: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopArray.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopArray.java?rev=1207059&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopArray.java
(added)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopArray.java
Mon Nov 28 10:42:54 2011
@@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mk.json.fast;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import org.apache.jackrabbit.mk.json.JsopBuilder;
+import org.apache.jackrabbit.mk.json.JsopTokenizer;
+
+/**
+ * An array of objects.
+ */
+public class JsopArray extends Jsop implements List<Object> {
+
+    private static final ArrayList<String> EMPTY_LIST = new ArrayList<String>();
+
+    private ArrayList<String> list;
+    private JsopTokenizer tokenizer;
+
+    public JsopArray(String jsop, int start, int end) {
+        super(jsop, start, end);
+    }
+
+    public JsopArray() {
+        super(null, 0, 0);
+        list = new ArrayList<String>();
+    }
+
+    public Object get(int index) {
+        init();
+        String s = load(index);
+        if (s == null) {
+            throw new IndexOutOfBoundsException(index + " (size: " + list.size() + ")");
+        }
+        return Jsop.parse(s);
+    }
+
+    private String load(int index) {
+        if (index < list.size()) {
+            return list.get(index);
+        }
+        while (tokenizer != null) {
+            String v = tokenizer.readRawValue();
+            list.add(v);
+            if (tokenizer.matches(']')) {
+                tokenizer = null;
+            } else {
+                tokenizer.read(',');
+            }
+            if (index < list.size()) {
+                return v;
+            }
+        }
+        return null;
+    }
+
+    private void init() {
+        if (list != null) {
+            return;
+        }
+        tokenizer = new JsopTokenizer(jsop, start);
+        tokenizer.read('[');
+        if (tokenizer.matches(']')) {
+            list = EMPTY_LIST;
+            tokenizer = null;
+        } else {
+            list = new ArrayList<String>();
+        }
+    }
+
+    public boolean isEmpty() {
+        init();
+        return list == EMPTY_LIST;
+    }
+
+    public int size() {
+        init();
+        load(Integer.MAX_VALUE);
+        return list.size();
+    }
+
+    public String toString() {
+        if (jsop == null) {
+            JsopBuilder w = new JsopBuilder();
+            w.array();
+            for (String e : list) {
+                w.encodedValue(e);
+            }
+            w.endArray();
+            jsop = w.toString();
+            start = 0;
+        }
+        return jsop.substring(start);
+    }
+
+    public boolean add(Object e) {
+        initWrite();
+        list.add(toString(e));
+        return true;
+    }
+
+    public void clear() {
+        initWrite();
+        list.clear();
+    }
+
+    private void initWrite() {
+        readAll();
+        jsop = null;
+    }
+
+    private void readAll() {
+        load(Integer.MAX_VALUE);
+    }
+
+    public void add(int index, Object element) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean addAll(Collection<? extends Object> c) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean addAll(int index, Collection<? extends Object> c) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean contains(Object o) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean containsAll(Collection<?> c) {
+        throw new UnsupportedOperationException();
+    }
+
+    public int indexOf(Object o) {
+        throw new UnsupportedOperationException();
+    }
+
+    public Iterator<Object> iterator() {
+        throw new UnsupportedOperationException();
+    }
+
+    public int lastIndexOf(Object o) {
+        throw new UnsupportedOperationException();
+    }
+
+    public ListIterator<Object> listIterator() {
+        throw new UnsupportedOperationException();
+    }
+
+    public ListIterator<Object> listIterator(int index) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean remove(Object o) {
+        throw new UnsupportedOperationException();
+    }
+
+    public Object remove(int index) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean removeAll(Collection<?> c) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean retainAll(Collection<?> c) {
+        throw new UnsupportedOperationException();
+    }
+
+    public Object set(int index, Object element) {
+        throw new UnsupportedOperationException();
+    }
+
+    public List<Object> subList(int fromIndex, int toIndex) {
+        throw new UnsupportedOperationException();
+    }
+
+    public Object[] toArray() {
+        throw new UnsupportedOperationException();
+    }
+
+    public <T> T[] toArray(T[] a) {
+        throw new UnsupportedOperationException();
+    }
+
+}

Added: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObject.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObject.java?rev=1207059&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObject.java
(added)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObject.java
Mon Nov 28 10:42:54 2011
@@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mk.json.fast;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+import org.apache.jackrabbit.mk.json.JsopBuilder;
+import org.apache.jackrabbit.mk.json.JsopTokenizer;
+
+/**
+ * A map.
+ *
+ * This implementation supports an options 'lengthIndex' to speed up
+ * sparse parsing of an object (only the requested values are parsed). The
+ * speedup depends on the use case.
+ */
+public class JsopObject extends Jsop implements Map<String, Object> {
+
+    private static final String LENGTHS_KEY = ":lengths:";
+    private static final HashMap<String, String> EMPTY_MAP = new HashMap<String,
String>();
+
+    private HashMap<String, String> map;
+    private JsopTokenizer tokenizer;
+    private boolean lengthIndex;
+    private int[] length;
+
+    JsopObject(String jsop, int start, int end) {
+        super(jsop, start, end);
+    }
+
+    public JsopObject() {
+        super(null, 0, 0);
+        map = new LinkedHashMap<String, String>();
+    }
+
+    private void init() {
+        if (map != null) {
+            return;
+        }
+        tokenizer = new JsopTokenizer(jsop, start);
+        tokenizer.read('{');
+        if (tokenizer.matches('}')) {
+            map = EMPTY_MAP;
+            tokenizer = null;
+        } else {
+            map = new LinkedHashMap<String, String>();
+        }
+    }
+
+    public Object get(Object key) {
+        init();
+        String v = map.get(key);
+        if (tokenizer == null) {
+            return v == null ? null : Jsop.parse(v);
+        }
+        if (v == null) {
+            while (tokenizer != null) {
+                String k = tokenizer.readString();
+                if (lengthIndex) {
+                    int len = length[map.size()];
+                    int p = tokenizer.getPos();
+                    v = jsop.substring(p, p + len);
+                    tokenizer.setPos(p + len);
+                    tokenizer.read();
+                } else {
+                    tokenizer.read(':');
+                    v = tokenizer.readRawValue();
+                }
+                if (LENGTHS_KEY.equals(k)) {
+                    v = JsopTokenizer.decodeQuoted(v);
+                    String[] lengthsStrings = v.split(",");
+                    length = new int[lengthsStrings.length];
+                    for (int i = 0; i < lengthsStrings.length; i++) {
+                        length[i] = Integer.parseInt(lengthsStrings[i]);
+                    }
+                    lengthIndex = true;
+                } else {
+                    map.put(k, v);
+                }
+                if (tokenizer.matches('}')) {
+                    tokenizer = null;
+                } else {
+                    tokenizer.read(',');
+                }
+                if (k.equals(key)) {
+                    return Jsop.parse(v);
+                }
+            }
+        }
+        return null;
+    }
+
+    public boolean containsKey(Object key) {
+        get(key);
+        return map.containsKey(key);
+    }
+
+    public boolean isEmpty() {
+        init();
+        return map == EMPTY_MAP;
+    }
+
+    public int size() {
+        readAll();
+        return map.size();
+    }
+
+    private void readAll() {
+        get(null);
+    }
+
+    private void initWrite() {
+        readAll();
+        jsop = null;
+    }
+
+    public String toString() {
+        if (jsop == null) {
+            JsopBuilder w = new JsopBuilder();
+            w.object();
+            if (lengthIndex) {
+                if (map.containsKey(LENGTHS_KEY)) {
+                    throw new IllegalStateException("Object already contains the key " +
LENGTHS_KEY);
+                }
+                StringBuilder buff = new StringBuilder();
+                for (Entry<String, String> e : map.entrySet()) {
+                    if (buff.length() > 0) {
+                        buff.append(',');
+                    }
+                    buff.append(e.getValue().length());
+                }
+                w.key(LENGTHS_KEY).value(buff.toString());
+            }
+            for (Entry<String, String> e : map.entrySet()) {
+                w.key(e.getKey()).encodedValue(e.getValue());
+            }
+            w.endObject();
+            jsop = w.toString();
+            start = 0;
+        }
+        return jsop.substring(start);
+    }
+
+    public void clear() {
+        initWrite();
+        map.clear();
+    }
+
+    public Object put(String key, Object value) {
+        initWrite();
+        String old = map.put(key, toString(value));
+        return Jsop.parse(old);
+    }
+
+    public boolean containsValue(Object value) {
+        throw new UnsupportedOperationException();
+    }
+
+    public Set<Entry<String, Object>> entrySet() {
+        throw new UnsupportedOperationException();
+    }
+
+    public Set<String> keySet() {
+        throw new UnsupportedOperationException();
+    }
+
+    public void putAll(Map<? extends String, ? extends Object> m) {
+        throw new UnsupportedOperationException();
+    }
+
+    public Object remove(Object key) {
+        throw new UnsupportedOperationException();
+    }
+
+    public Collection<Object> values() {
+        throw new UnsupportedOperationException();
+    }
+
+    public void setLengthIndex(boolean lengthIndex) {
+        this.lengthIndex = lengthIndex;
+    }
+
+}

Added: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObjectTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObjectTest.java?rev=1207059&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObjectTest.java
(added)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/fast/JsopObjectTest.java
Mon Nov 28 10:42:54 2011
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mk.json.fast;
+
+import java.math.BigDecimal;
+import junit.framework.TestCase;
+
+/**
+ * Test the Jsop class.
+ */
+public class JsopObjectTest extends TestCase {
+
+    public static void main(String... args) {
+        // 1000 times 'x'
+        String data = new String(new char[1000]).replace((char) 0, 'x');
+        for (int i = 0; i < 6; i++) {
+            boolean lengthIndex = i % 2 == 0;
+            JsopObject w = new JsopObject();
+            w.setLengthIndex(lengthIndex);
+            // 100 children with the dummy data: 100'000 characters
+            for (int j = 0; j < 100; j++) {
+                w.put("child" + j, data);
+            }
+            String jsop = w.toString();
+            long t = System.currentTimeMillis();
+            for (int j = 0; j < 10000; j++) {
+                JsopObject o = (JsopObject) Jsop.parse(jsop);
+                assertEquals(data, o.get("child99"));
+            }
+            System.out.println((System.currentTimeMillis() - t) + " lengthIndex=" + lengthIndex);
+        }
+    }
+
+    public void testDataType() {
+        String s = (String) Jsop.parse("\"Hello\"");
+        assertEquals("Hello", s);
+
+        s = (String) Jsop.parse("\"Line 1\\nLine 2\"");
+        assertEquals("Line 1\nLine 2", s);
+
+        BigDecimal db = (BigDecimal) Jsop.parse("1");
+        assertEquals("1", db.toString());
+
+        db = (BigDecimal) Jsop.parse("1.3e-1");
+        assertEquals("0.13", db.toString());
+
+        db = (BigDecimal) Jsop.parse("-1");
+        assertEquals("-1", db.toString());
+
+        db = (BigDecimal) Jsop.parse("10.3");
+        assertEquals("10.3", db.toString());
+
+        assertNull(Jsop.parse("null"));
+
+        assertEquals(Boolean.TRUE, Jsop.parse("true"));
+        assertEquals(Boolean.FALSE, Jsop.parse("false"));
+
+        JsopObject o = (JsopObject) Jsop.parse("{}");
+        assertEquals(0, o.size());
+        assertTrue(o.isEmpty());
+        assertEquals("{}", o.toString());
+
+        JsopArray a = (JsopArray) Jsop.parse("[]");
+        assertEquals(0, a.size());
+        assertTrue(a.isEmpty());
+        assertEquals("[]", a.toString());
+    }
+
+    public void testArray() {
+        JsopArray a = (JsopArray) Jsop.parse("[1, null, \"Hello\", [], {}]");
+        // this will force everything is parsed
+        assertEquals(5, a.size());
+        assertFalse(a.isEmpty());
+        assertEquals("1", a.get(0).toString());
+        assertNull(a.get(1));
+        assertEquals("Hello", a.get(2));
+        JsopArray a1 = (JsopArray) a.get(3);
+        assertEquals(0, a1.size());
+        JsopObject o1 = (JsopObject) a.get(4);
+        assertEquals(0, o1.size());
+
+        try {
+            a.get(6);
+            fail();
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+    }
+
+    public void testArrayLazyInit() {
+        JsopArray a = (JsopArray) Jsop.parse("[1, null, \"Hello\", [], {}]");
+        assertEquals("1", a.get(0).toString());
+        assertNull(a.get(1));
+        assertEquals("Hello", a.get(2));
+        JsopArray a1 = (JsopArray) a.get(3);
+        assertEquals(0, a1.size());
+        JsopObject o1 = (JsopObject) a.get(4);
+        assertEquals(0, o1.size());
+        assertEquals(5, a.size());
+        assertFalse(a.isEmpty());
+    }
+
+    public void testArrayCreate() {
+        JsopArray array = new JsopArray();
+        array.add("test");
+        array.add(1);
+        assertEquals("[\"test\",1]", array.toString());
+        array.clear();
+        array.add(null);
+        array.add(true);
+        array.add(false);
+        assertEquals("[null,true,false]", array.toString());
+    }
+
+    public void testObject() {
+        JsopObject o = (JsopObject) Jsop.parse("{\"a\": 1, \"b\": null, \"c\": true, \"d\":
{}, \"e\": []}");
+        // this will force everything is parsed
+        assertEquals(5, o.size());
+        assertFalse(o.isEmpty());
+        assertEquals("1", o.get("a").toString());
+        assertNull(o.get("b"));
+        assertEquals(true, o.get("c"));
+        JsopObject o1 = (JsopObject) o.get("d");
+        assertEquals(0, o1.size());
+        JsopArray a1 = (JsopArray) o.get("e");
+        assertEquals(0, a1.size());
+        assertTrue(o.containsKey("a"));
+        assertTrue(o.containsKey("b"));
+        assertFalse(o.containsKey("x"));
+    }
+
+    public void testObjectLazyInit() {
+        JsopObject a = (JsopObject) Jsop.parse("{\"a\": 1, \"b\": null, \"c\": true, \"d\":
{}, \"e\": []}");
+        assertEquals("1", a.get("a").toString());
+        assertNull(a.get("b"));
+        assertEquals(true, a.get("c"));
+        JsopObject a1 = (JsopObject) a.get("d");
+        assertEquals(0, a1.size());
+        JsopArray o1 = (JsopArray) a.get("e");
+        assertEquals(0, o1.size());
+    }
+
+    public void testObjectCreate() {
+        JsopObject o = new JsopObject();
+        o.setLengthIndex(true);
+        o.put("a", true);
+        o.put("b", false);
+        o.put("c", null);
+        o.put("d", "Hello");
+        o.put("e", new JsopObject());
+        o.put("f", new JsopArray());
+        String s = o.toString();
+        assertEquals("{\":lengths:\":\"4,5,4,7,2,2\",\"a\":true,\"b\":false,\"c\":null,\"d\":\"Hello\",\"e\":{},\"f\":[]}",
s);
+        o = (JsopObject) Jsop.parse(s);
+        assertEquals(Boolean.TRUE, o.get("a"));
+        assertEquals(Boolean.FALSE, o.get("b"));
+        assertNull(o.get("c"));
+        assertEquals("Hello", o.get("d"));
+        assertEquals("{}", o.get("e").toString());
+        assertEquals("[]", o.get("f").toString());
+    }
+
+    public void testObjectCreateClear() {
+        JsopObject o = new JsopObject();
+        o.put("a", true);
+        o.put("b", false);
+        assertEquals(true, o.get("a"));
+        assertEquals(false, o.get("b"));
+        assertEquals(2, o.size());
+        o.clear();
+        assertEquals(0, o.size());
+    }
+
+}



Mime
View raw message