wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ngalla...@apache.org
Subject svn commit: r1021933 [3/9] - in /incubator/wink/trunk: ./ wink-json4j/ wink-json4j/src/ wink-json4j/src/main/ wink-json4j/src/main/java/ wink-json4j/src/main/java/org/ wink-json4j/src/main/java/org/apache/ wink-json4j/src/main/java/org/apache/wink/ win...
Date Tue, 12 Oct 2010 21:30:32 GMT
Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/JSONWriter.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/JSONWriter.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/JSONWriter.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/JSONWriter.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,584 @@
+/*
+ * 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.wink.json4j;
+
+import java.io.Writer;
+import java.io.BufferedWriter;
+import java.io.StringWriter;
+import java.io.CharArrayWriter;
+import java.io.IOException;
+import java.util.Stack;
+
+import org.apache.wink.json4j.internal.BeanSerializer;
+
+/**
+ * This class implements a JSONWrier, a convenience function for writing out JSON
+ * to a writer or underlying stream.
+ */
+public class JSONWriter {
+
+    /**
+     * The writer to use to output JSON in a semi-streaming fashion.
+     */
+    protected Writer writer = null;
+
+    /**
+     * Flag to denote that the writer is in an object.  
+     */
+    private boolean inObject = false;
+
+    /**
+     * Flag to denote that the writer is in an array.  
+     */
+    private boolean inArray = false;
+
+    /**
+     * Flag for state checking that a key was placed (if inside an object)
+     * Required to be true for a value to be placed in that situation
+     */
+    private boolean keyPlaced = false;
+
+    /**
+     * Flag denoting if in an array or object, if the first entry has been placed or not.
+     */
+    private boolean firstEntry = false;
+
+    /**
+     * A stack to keep track of all the closures.
+     */
+    private Stack closures = null;
+
+    /** 
+     * Flag used to check the state of this writer, if it has been closed, all
+     * operations will throw an IllegalStateException.
+     */
+    private boolean closed = false;
+
+    /**
+     * Constructor.
+     * @param writer The writer to use to do 'streaming' JSON writing.
+     * @throws NullPointerException Thrown if writer is null.
+     */
+    public JSONWriter(Writer writer) throws NullPointerException {
+        //Try to avoid double-buffering or buffering in-memory writers.
+        Class writerClass = writer.getClass();
+        if (!StringWriter.class.isAssignableFrom(writerClass) &&
+            !CharArrayWriter.class.isAssignableFrom(writerClass) &&
+            !BufferedWriter.class.isAssignableFrom(writerClass)) {
+            writer = new BufferedWriter(writer);
+        }
+        this.writer = writer;
+        this.closures = new Stack();
+    }
+
+    /**
+     * Open a new JSON Array in the output stream.
+     * @throws IOException Thrown if an error occurs on the underlying writer.
+     * @throws IllegalstateException Thrown if the current writer position does not permit an array.
+     * @return A reference to this writer.
+     */
+    public JSONWriter array() throws IOException, IllegalStateException {
+        if (closed) {
+            throw new IllegalStateException("The writer has been closed.  No further operations allowed.");
+        }
+        if (inObject) {
+            if (!keyPlaced) {
+                throw new IllegalStateException("Current containment is a JSONObject, but a key has not been specified to contain a new array");
+            }
+        } else if (inArray) {
+            if (!firstEntry) {
+                writer.write(",");
+            }
+        }
+        writer.write("[");
+        inArray = true;
+        inObject = false;
+        keyPlaced = false;
+        firstEntry = true;
+        closures.push("]");
+        return this;
+    }
+
+    /**
+     * Method to close the current JSON Array in the stream.  
+     * @throws IOException Thrown if an IO error occurs on the underlying writer.
+     * @throws IllegalStateException Thrown if the writer position is not inside an array.
+     * @return A reference to this writer.
+     */
+    public JSONWriter endArray() throws IOException {
+        if (closed) {
+            throw new IllegalStateException("The writer has been closed.  No further operations allowed.");
+        }
+        if (!inArray) {
+            throw new IllegalStateException("Current writer position is not within a JSON array");
+        } else {
+            writer.write(((String)closures.pop()));
+            // Set our current positional/control state.
+            if (!closures.isEmpty()) {
+                String nextClosure = (String)closures.peek();
+                if (nextClosure.equals("}")) {
+                    inObject = true;
+                    inArray = false;
+                } else {
+                    inObject = false;
+                    inArray = true;
+                }
+                firstEntry = false;
+            } else {
+                inArray = false;
+                inObject = false;
+                firstEntry = true;
+            }
+        }
+        return this;
+    }
+
+    /**
+     * Method to close a current JSON object in the stream.  
+     * @throws IOException Thrown if an IO error occurs on the underlying writer.
+     * @throws IllegalStateException Thrown if the writer position is not inside an object, or if the object has a key placed, but no value.
+     * @return A reference to this writer.
+     */
+    public JSONWriter endObject() throws IOException, IllegalStateException {
+        if (closed) {
+            throw new IllegalStateException("The writer has been closed.  No further operations allowed.");
+        }
+        if (!inObject) {
+            throw new IllegalStateException("Current writer position is not within a JSON object");
+        } else {
+            if (keyPlaced) {
+                throw new IllegalStateException("Current writer position in an object and has a key placed, but no value has been assigned to the key.  Cannot end.");
+            } else {
+                writer.write((String)closures.pop());
+                // Set our current positional/control state.
+                if (!closures.isEmpty()) {
+                    String nextClosure = (String)closures.peek();
+                    if (nextClosure.equals("}")) {
+                        inObject = true;
+                        inArray = false;
+                    } else {
+                        inObject = false;
+                        inArray = true;
+                    }
+                    firstEntry = false;
+                } else {
+                    inArray = false;
+                    inObject = false;
+                    firstEntry = true;
+                }
+            }
+        }
+        return this;
+    }
+
+    /**
+     * Place a key in the current JSON Object.
+     * @throws IOException Thrown if an IO error occurs on the underlying writer.
+     * @throws IllegalStateException Thrown if the current writer position is not within an object.
+     * @return A reference to this writer.
+     */
+    public JSONWriter key(String s) throws IOException, IllegalStateException, NullPointerException {
+        if (closed) {
+            throw new IllegalStateException("The writer has been closed.  No further operations allowed.");
+        }
+        if (s == null) {
+            throw new NullPointerException("Key cannot be null");
+        } else {
+            if (!inObject) {
+                throw new IllegalStateException("Current writer position is not inside a JSON Object, a key cannot be placed.");
+            } else {
+                if (!keyPlaced) {
+                    if (firstEntry) {
+                        firstEntry = false;
+                    } else {
+                        writer.write(",");
+                    }
+                    keyPlaced = true;
+                    writeString(s);
+                    writer.write(":");
+                } else {
+                    throw new IllegalStateException("Current writer position is inside a JSON Object an with an open key waiting for a value.  Another key cannot be placed.");
+                }
+            }
+        }
+        return this;
+    }
+
+    /**
+     * Open a new JSON Object in the output stream.
+     * @throws IllegalStateException Thrown if an object cannot currently be created in the stream.
+     * @throws IOException Thrown if an IO error occurs in the underlying writer.
+     * @return A reference to this writer.
+     */
+    public JSONWriter object() throws IOException, IllegalStateException {
+        if (closed) {
+            throw new IllegalStateException("The writer has been closed.  No further operations allowed.");
+        }
+        if (inObject) {
+            if (!keyPlaced) {
+                throw new IllegalStateException("Current containment is a JSONObject, but a key has not been specified to contain a new object");
+            }
+        } else if (inArray) {
+            if (!firstEntry) {
+                writer.write(",");
+            }
+        }
+        writer.write("{");
+        inObject = true;
+        inArray = false;
+        keyPlaced = false;
+        firstEntry = true;
+        closures.push("}");
+        return this;
+    }
+
+    /**
+     * Method to write a boolean to the current writer position.
+     * @throws IOException Thrown if an IO error occurs on the underlying writer.
+     * @throws IllegalStateException Thrown if the current writer position will not accept a boolean value.
+     * @return A reference to this writer.
+     */
+    public JSONWriter value(boolean b) throws IOException, IllegalStateException {
+        if (closed) {
+            throw new IllegalStateException("The writer has been closed.  No further operations allowed.");
+        }
+        if (inArray) {
+            if (firstEntry) {
+                firstEntry = false;
+            } else {
+                writer.write(",");
+            }
+            writer.write(Boolean.toString(b));
+        } else if (inObject) {
+            if (keyPlaced) {
+                writer.write(Boolean.toString(b));
+                keyPlaced = false;
+            } else {
+                throw new IllegalStateException("Current containment is a JSONObject, but a key has not been specified for the boolean value.");
+            }
+        } else {
+            throw new IllegalStateException("Writer is currently not in an array or object, cannot write value");
+        }
+        return this;
+    }
+
+    /**
+     * Method to write a double to the current writer position.
+     * @param d The Double to write.
+     * @throws IOException Thrown if an IO error occurs on the underlying writer.
+     * @throws IllegalStateException Thrown if the current writer position will not accept a double value.
+     * @return A reference to this writer.
+     */
+    public JSONWriter value(double d) throws IOException, IllegalStateException {
+        if (closed) {
+            throw new IllegalStateException("The writer has been closed.  No further operations allowed.");
+        }
+        if (inArray) {
+            if (firstEntry) {
+                firstEntry = false;
+            } else {
+                writer.write(",");
+            }
+            writer.write(Double.toString(d));
+        } else if (inObject) {
+            if (keyPlaced) {
+                writer.write(Double.toString(d));
+                keyPlaced = false;
+            } else {
+                throw new IllegalStateException("Current containment is a JSONObject, but a key has not been specified for the double value.");
+            }
+        } else {
+            throw new IllegalStateException("Writer is currently not in an array or object, cannot write value");
+        }
+        return this;
+    }
+
+    /**
+     * Method to write a double to the current writer position.
+     * @param l The long to write.
+     * @throws IOException Thrown if an IO error occurs on the underlying writer.
+     * @throws IllegalStateException Thrown if the current writer position will not accept a double value.
+     * @return A reference to this writer.
+     */
+    public JSONWriter value(long l) throws IOException, IllegalStateException {
+        if (closed) {
+            throw new IllegalStateException("The writer has been closed.  No further operations allowed.");
+        }
+        if (inArray) {
+            if (firstEntry) {
+                firstEntry = false;
+            } else {
+                writer.write(",");
+            }
+            writer.write(Long.toString(l));
+        } else if (inObject) {
+            if (keyPlaced) {
+                writer.write(Long.toString(l));
+                keyPlaced = false;
+            } else {
+                throw new IllegalStateException("Current containment is a JSONObject, but a key has not been specified for the long value.");
+            }
+        } else {
+            throw new IllegalStateException("Writer is currently not in an array or object, cannot write value");
+        }
+        return this;
+    }
+
+    /**
+     * Method to write an int to the current writer position.
+     * @param i The int to write.
+     * @throws IOException Thrown if an IO error occurs on the underlying writer.
+     * @throws IllegalStateException Thrown if the current writer position will not accept a double value.
+     * @return A reference to this writer.
+     */
+    public JSONWriter value(int i) throws IOException, IllegalStateException {
+        if (closed) {
+            throw new IllegalStateException("The writer has been closed.  No further operations allowed.");
+        }
+        if (inArray) {
+            if (firstEntry) {
+                firstEntry = false;
+            } else {
+                writer.write(",");
+            }
+            writer.write(Integer.toString(i));
+        } else if (inObject) {
+            if (keyPlaced) {
+                writer.write(Integer.toString(i));
+                keyPlaced = false;
+            } else {
+                throw new IllegalStateException("Current containment is a JSONObject, but a key has not been specified for the int value.");
+            }
+        } else {
+            throw new IllegalStateException("Writer is currently not in an array or object, cannot write value");
+        }
+        return this;
+    }
+
+    /**
+     * Method to write a short to the current writer position.
+     * @param s The short to write.
+     * @throws IOException Thrown if an IO error occurs on the underlying writer.
+     * @throws IllegalStateException Thrown if the current writer position will not accept a double value.
+     * @return A reference to this writer.
+     */
+    public JSONWriter value(short s) throws IOException, IllegalStateException {
+        if (closed) {
+            throw new IllegalStateException("The writer has been closed.  No further operations allowed.");
+        }
+        if (inArray) {
+            if (firstEntry) {
+                firstEntry = false;
+            } else {
+                writer.write(",");
+            }
+            writer.write(Short.toString(s));
+        } else if (inObject) {
+            if (keyPlaced) {
+                writer.write(Short.toString(s));
+                keyPlaced = false;
+            } else {
+                throw new IllegalStateException("Current containment is a JSONObject, but a key has not been specified for the short value.");
+            }
+        } else {
+            throw new IllegalStateException("Writer is currently not in an array or object, cannot write value");
+        }
+        return this;
+    }
+
+    /**
+     * Method to write an Object to the current writer position.
+     * @param o The object to write.
+     * @throws IOException Thrown if an IO error occurs on the underlying writer.
+     * @throws JSONException Thrown if the object is not JSONAble.
+     * @return A reference to this writer.
+     */
+    public JSONWriter value(Object o) throws IOException, IllegalStateException, JSONException {
+        if (closed) {
+            throw new IllegalStateException("The writer has been closed.  No further operations allowed.");
+        }
+        if (inArray) {
+            if (firstEntry) {
+                firstEntry = false;
+            } else {
+                writer.write(",");
+            }
+            writeObject(o);
+        } else if (inObject) {
+            if (keyPlaced) {
+                writeObject(o);
+                keyPlaced = false;
+            } else {
+                throw new IllegalStateException("Current containment is a JSONObject, but a key has not been specified for the boolean value.");
+            }
+        } else {
+            throw new IllegalStateException("Writer is currently not in an array or object, cannot write value");
+        }
+        return this;
+    }
+
+
+    /**
+     * Method to close the JSON Writer.  All current object depths will be closed out and the writer closed.
+     * @throws IOException Thrown if an IO error occurs on the underlying writer.
+     * @throws IllegalStateException Thrown if the writer position is in an object and a key has been placed, but a value has not been assigned or if the writer was already closed.
+     */
+    public void close() throws IOException, IllegalStateException {
+        if (!closed) {
+            if (inObject && keyPlaced) {
+                throw new IllegalStateException("Object has key without value.  Cannot close.");
+            } else {
+                while (!closures.isEmpty()) {
+                    writer.write((String)closures.pop());
+                }
+                writer.flush();
+                writer.close();
+                closed = true;
+            }
+        }
+    }
+
+    /**
+     * Method to flush the underlying writer so that all buffered content, if any, is written out.
+     * @return A reference to this writer.
+     */
+    public JSONWriter flush() throws IOException {
+        writer.flush();
+        return this;
+    }
+
+    /**
+     * Method to write a String out to the writer, encoding special characters and unicode characters properly.
+     * @param value The string to write out.
+     * @throws IOException Thrown if an error occurs during write.
+     */
+    private void writeString(String value) throws IOException {
+        writer.write('"');
+        char[] chars = value.toCharArray();
+        for (int i=0; i<chars.length; i++) {
+            char c = chars[i];
+            switch (c) {
+                case  '"': writer.write("\\\""); break;
+                case '\\': writer.write("\\\\"); break;
+                case    0: writer.write("\\0"); break;
+                case '\b': writer.write("\\b"); break;
+                case '\t': writer.write("\\t"); break;
+                case '\n': writer.write("\\n"); break;
+                case '\f': writer.write("\\f"); break;
+                case '\r': writer.write("\\r"); break;
+                case '/': writer.write("\\/"); break;
+                default:
+                    if ((c >= 32) && (c <= 126)) {
+                        writer.write(c);
+                    } else {
+                        writer.write("\\u");
+                        writer.write(rightAlignedZero(Integer.toHexString(c),4));
+                    }
+            }
+        }
+        writer.write('"');
+    }
+
+    /**
+     * Method to generate a string with a particular width.  Alignment is done using zeroes if it does not meet the width requirements.
+     * @param s The string to write
+     * @param len The minimum length it should be, and to align with zeroes if length is smaller.
+     * @return A string properly aligned/correct width.
+     */
+    private String rightAlignedZero(String s, int len) {
+        if (len == s.length()) return s;
+        StringBuffer sb = new StringBuffer(s);
+        while (sb.length() < len) {
+            sb.insert(0, '0');
+        }
+        return sb.toString();
+    }
+
+    /**
+     * Method to write a number to the current writer.
+     * @param value The number to write to the JSON output string.
+     * @throws IOException Thrown if an error occurs during write.
+     */
+    private void writeNumber(Number value) throws IOException {
+        if (null == value) {
+            writeNull();
+        }
+        if (value instanceof Float) {
+            if (((Float)value).isNaN()) {
+                writeNull();
+            }
+            if (Float.NEGATIVE_INFINITY == value.floatValue()) {
+                writeNull();
+            }
+            if (Float.POSITIVE_INFINITY == value.floatValue()) {
+                writeNull();
+            }
+        }
+        if (value instanceof Double) {
+            if (((Double)value).isNaN()) {
+                writeNull();
+            }
+            if (Double.NEGATIVE_INFINITY == value.doubleValue()) {
+                writeNull();
+            }
+            if (Double.POSITIVE_INFINITY == value.doubleValue()) {
+                writeNull();
+            }
+        }
+        writer.write(value.toString());
+    }
+
+    /**
+     * Method to write an object to the current writer.
+     * @param o The object to write.
+     * @throws IOException Thrown if an IO error occurs on the underlying writer.
+     * @throws JSONException Thrown if the specified object is not JSONAble.
+     */
+    private void writeObject(Object o) throws IOException, JSONException {
+        // Handle the object!
+        if (o == null) {
+            writeNull();
+        } else {
+            Class clazz = o.getClass();
+            if (JSONArtifact.class.isAssignableFrom(clazz)) {
+                writer.write(((JSONArtifact)o).toString());
+            } else if (Number.class.isAssignableFrom(clazz)) {
+                writeNumber((Number)o);
+            } else if (Boolean.class.isAssignableFrom(clazz)) {
+                writer.write(((Boolean)o).toString());
+            } else if (String.class.isAssignableFrom(clazz)) {
+                writeString((String)o);
+            } else if (JSONString.class.isAssignableFrom(clazz)) {
+                writer.write(((JSONString)o).toJSONString());
+            } else {
+                // Unknown type, we'll just try to serialize it like a Java Bean.
+                writer.write(BeanSerializer.toJson(o, true).write());
+            }
+        }
+    }
+
+    /**
+     * Method to write the text string 'null' to the output stream (null JSON object).
+     * @throws IOException Thrown if an error occurs during write.
+     */
+    private void writeNull() throws IOException {
+        writer.write("null");
+    }
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/OrderedJSONObject.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/OrderedJSONObject.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/OrderedJSONObject.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/OrderedJSONObject.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,268 @@
+/*
+ * 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.wink.json4j;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.InputStreamReader;
+import java.io.InputStream;
+import java.io.Writer;
+import java.io.OutputStreamWriter;
+import java.io.OutputStream;
+import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
+import java.util.HashMap;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.wink.json4j.internal.Parser;
+import org.apache.wink.json4j.internal.Serializer;
+import org.apache.wink.json4j.internal.SerializerVerbose;
+
+/**
+ * Extension of the basic JSONObject.  This class allows control of the serialization order of attributes.  
+ * The order in which items are put into the instance controls the order in which they are serialized out.  For example, the
+ * last item put is the last item serialized.  
+ * <BR><BR>
+ * JSON-able values are: null, and instances of String, Boolean, Number, JSONObject and JSONArray.
+ * <BR><BR>
+ * Instances of this class are not thread-safe.
+ */
+public class OrderedJSONObject extends JSONObject {
+
+    private static final long serialVersionUID = -3269263069889337299L;
+    private ArrayList order                    = null;
+
+    /**
+     * Create a new instance of this class. 
+     */
+    public OrderedJSONObject() {
+        super();
+        this.order = new ArrayList();
+    }
+
+    /**
+     * Create a new instance of this class from the provided JSON object string.
+     * Note:  This is the same as calling new OrderedJSONObject(str, false).
+     * @param str The String of JSON to parse
+     * @throws JSONException Thrown when the string passed is null, or malformed JSON.. 
+     */
+    public OrderedJSONObject(String str) throws JSONException {
+        super();
+        this.order = new ArrayList();
+        StringReader reader = new StringReader(str);
+        (new Parser(reader)).parse(true, this);
+    }
+
+    /**
+     * Create a new instance of this class from the provided JSON object string.
+     * @param str The String of JSON to parse
+     * @param strict Boolean flag indicating if strict mode should be used.  Strict mode means comments and unquoted strings are not allowed.
+     * @throws JSONException Thrown when the string passed is null, or malformed JSON.. 
+     */
+    public OrderedJSONObject(String str, boolean strict) throws JSONException {
+        super();
+        this.order = new ArrayList();
+        StringReader reader = new StringReader(str);
+        (new Parser(reader, strict)).parse(true, this);
+    }
+
+    /**
+     * Create a new instance of this class from the data provided from the reader.  The reader content must be a JSON object string.
+     * Note:  The reader will not be closed, that is left to the caller.
+     * Note:  This is the same as calling new OrderedJSONObject(rdr, false).
+     * @param rdr The reader from which to read the JSON to parse
+     * @throws JSONException Thrown when the string passed is null, or malformed JSON.. 
+     */
+    public OrderedJSONObject(Reader rdr) throws JSONException {
+        super();
+        this.order = new ArrayList();
+        (new Parser(rdr)).parse(true, this);
+    }
+
+    /**
+     * Create a new instance of this class from the data provided from the reader.  The reader content must be a JSON object string.
+     * Note:  The reader will not be closed, that is left to the caller.
+     * @param rdr The reader from which to read the JSON to parse
+     * @param strict Boolean flag indicating if strict mode should be used.  Strict mode means comments and unquoted strings are not allowed.
+     * @throws JSONException Thrown when the string passed is null, or malformed JSON.. 
+     */
+    public OrderedJSONObject(Reader rdr, boolean strict) throws JSONException {
+        super();
+        this.order = new ArrayList();
+        (new Parser(rdr, strict)).parse(true, this);
+    }
+
+    /**
+     * Create a new instance of this class from the data provided from the input stream.  The stream content must be a JSON object string.
+     * Note:  The input stream content is assumed to be UTF-8 encoded.
+     * Note:  The InputStream will not be closed, that is left to the caller.
+     * Note:  This is the same as calling new OrderedJSONObject(is, false).
+     * @param is The InputStream from which to read the JSON to parse
+     * @throws JSONException Thrown when the string passed is null, or malformed JSON.. 
+     */
+    public OrderedJSONObject (InputStream is) throws JSONException {
+        super();
+        this.order = new ArrayList();
+        InputStreamReader isr = null;
+        if (is != null) {
+            try {
+                isr = new InputStreamReader(is, "UTF-8");
+            } catch (Exception ex) {
+                isr = new InputStreamReader(is);
+            }
+        } else {
+            throw new JSONException("Inputstream cannot be null");
+        }
+        (new Parser(isr)).parse(true, this);
+    }
+
+    /**
+     * Create a new instance of this class from the data provided from the input stream.  The stream content must be a JSON object string.
+     * Note:  The input stream content is assumed to be UTF-8 encoded.
+     * Note:  The InputStream will not be closed, that is left to the caller.
+     * @param is The InputStream from which to read the JSON to parse
+     * @param strict Boolean flag indicating if strict mode should be used.  Strict mode means comments and unquoted strings are not allowed.
+     * @throws JSONException Thrown when the string passed is null, or malformed JSON.. 
+     */
+    public OrderedJSONObject (InputStream is, boolean strict) throws JSONException {
+        super();
+        this.order = new ArrayList();
+        InputStreamReader isr = null;
+        if (is != null) {
+            try {
+                isr = new InputStreamReader(is, "UTF-8");
+            } catch (Exception ex) {
+                isr = new InputStreamReader(is);
+            }
+        } else {
+            throw new JSONException("Inputstream cannot be null");
+        }
+        (new Parser(isr, strict)).parse(true, this);
+    }
+
+    /**
+     * Create a new instance of this class using the contents of the provided map.  
+     * The contents of the map should be values considered JSONable.
+     * @param map The map of key/value pairs to insert into this JSON object
+     * @throws JSONException Thrown when contents in the map cannot be made JSONable.
+     * @throws NullPointerException Thrown if the map is null, or a key in the map is null..
+     */
+    public OrderedJSONObject(Map map) throws JSONException {
+        super();
+        this.order = new ArrayList();
+        Set set = map.keySet();
+        if (set != null) {
+            Iterator itr = set.iterator();
+            if (itr != null) {
+                while (itr.hasNext()) {
+                    Object key = itr.next();
+                    String sKey = key.toString();
+                    this.put(sKey, map.get(key));
+                }
+            }
+        }
+    }
+
+    /**
+     * Method to put a JSON'able object into the instance.  Note that the order of initial puts controls the order of serialization.  
+     * Meaning that the first time an item is put into the object determines is position of serialization.  Subsequent puts with the same
+     * key replace the existing entry value and leave serialization position alone.  For moving the position, the object must be removed, 
+     * then re-put.
+     * @see java.util.HashMap#put(java.lang.Object, java.lang.Object)
+     */
+    public Object put(Object key, Object value) {
+        if (null == key) throw new IllegalArgumentException("key must not be null");
+        if (!(key instanceof String)) throw new IllegalArgumentException("key must be a String");
+
+        if (!isValidObject(value)) {
+            if (value != null) {
+                throw new IllegalArgumentException("Invalid type of value.  Type: [" + value.getClass().getName() + "] with value: [" + value.toString() + "]");
+            } else {
+                throw new IllegalArgumentException("Invalid type of value.");
+            }
+        }
+
+        /**
+         * Only put it in the ordering list if it isn't already present.
+         */
+        if (!this.containsKey(key)) {
+            this.order.add(key);
+        }
+        return super.put(key, value);
+    }
+
+    /**
+     * Method to remove an entry from the OrderedJSONObject instance.
+     * @see java.util.HashMap#remove(java.lang.Object)
+     */
+    public Object remove(Object key) {
+        Object retVal = null;
+
+        if (null == key) throw new IllegalArgumentException("key must not be null");
+        if (this.containsKey(key)) {
+            retVal = super.remove(key);
+
+            for (int i = 0; i < this.order.size(); i++) {
+                Object obj = this.order.get(i);
+                if (obj.equals(key)) {
+                    this.order.remove(i);
+                    break;
+                }
+            }
+        }
+        return retVal;
+    }
+
+    /**
+     * (non-Javadoc)
+     * @see java.util.HashMap#clear()
+     */
+    public void clear() {
+        super.clear();
+        this.order.clear();
+    }
+
+    /** 
+     * Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
+     */
+    public Object clone() {
+        OrderedJSONObject clone = (OrderedJSONObject)super.clone();
+        Iterator order = clone.getOrder();
+        ArrayList orderList = new ArrayList();
+        while (order.hasNext()) {
+            orderList.add(order.next());
+            clone.order = orderList;
+        }
+        return clone;
+    }
+
+    /**
+     * Method to obtain the order in which the items will be serialized.
+     * @return An iterator that represents the attribute names in the order that they will be serialized.
+     */
+    public Iterator getOrder() {
+        return this.order.iterator();
+    }
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONArray.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONArray.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONArray.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONArray.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,101 @@
+/*
+ * 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.wink.json4j.compat;
+
+import java.io.Writer;
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ *
+ */
+public interface JSONArray {
+	
+	// ------- data accessor methods -------
+	public Object get(int index) throws JSONException;
+	
+	public boolean getBoolean(int index) throws JSONException;
+	
+	public double getDouble(int index) throws JSONException;
+	
+	public int getInt(int index) throws JSONException;
+	
+	public long getLong(int index) throws JSONException;
+
+        public short getShort(int index) throws JSONException;
+	
+	public String getString(int index) throws JSONException;
+	
+	public JSONArray getJSONArray(int index) throws JSONException;
+	
+	public JSONObject getJSONObject(int index) throws JSONException;
+	
+	// ------- modifier methods -------	
+	
+	public String join(String separator);
+	
+	public JSONArray put(boolean value);
+	
+	public JSONArray put(Collection value) throws JSONException ;
+	
+	public JSONArray put(double value);
+	
+	public JSONArray put(int value);
+	
+	public JSONArray put(int index, boolean value) throws JSONException;
+
+	public JSONArray put(int index, Collection value) throws JSONException;
+	
+	public JSONArray put(int index, double value) throws JSONException;	
+	
+	public JSONArray put(int index, int value) throws JSONException;
+	
+	public JSONArray put(int index, long value) throws JSONException;
+        
+        public JSONArray put(int index, short value) throws JSONException;
+	
+	public JSONArray put(int index, Map value) throws JSONException;
+	
+	public JSONArray put(int index, Object value) throws JSONException;
+		
+	public JSONArray put(long value);
+        
+        public JSONArray put(short value);
+	
+	public JSONArray put(Map value) throws JSONException ;
+	
+	public JSONArray put(Object value) throws JSONException ;
+	
+	public Object remove(int index);
+	
+	// ------- utility methods -------	
+	
+	public boolean isNull(int index);
+	
+	public int length();
+	
+	// ------- output methods -------
+	
+	public String toString();
+	
+	public String toString(int indent);
+	
+	public Writer write(Writer w) throws JSONException;
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONException.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONException.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONException.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONException.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,42 @@
+/*
+ * 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.wink.json4j.compat;
+
+/**
+ *
+ */
+public class JSONException extends Exception {
+	
+	private Throwable cause;
+	
+	public JSONException(String message) {
+		super(message);
+	}
+	
+	public JSONException(Throwable t) {
+		super(t);
+		cause = t;
+	}
+	
+	public Throwable getCause() {
+		return null;
+	}
+
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONFactory.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONFactory.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONFactory.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONFactory.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,78 @@
+/*
+ * 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.wink.json4j.compat;
+
+import java.io.Reader;
+import java.util.Collection;
+import java.util.Map;
+import java.io.Writer;
+
+public abstract class JSONFactory {
+	
+	public static final JSONFactory newInstance() {
+		String factory = System.getProperty("org.apache.wink.common.model.json.factory.impl");
+		if (factory != null && factory.length() > 0) {
+                    try {
+                        Class clazz = Class.forName(factory);
+                        return (JSONFactory)clazz.newInstance();
+
+                    } catch (Throwable th) {
+                        // Ignore
+                        th.printStackTrace();
+                    }
+		}
+		return null;
+	}
+	
+	public abstract JSONArray createJSONArray();
+	
+	public abstract JSONArray createJSONArray(Collection collection) throws JSONException;
+	
+	public abstract JSONArray createJSONArray(Collection collect, boolean useSuperClass) throws JSONException;
+	
+	public abstract JSONArray createJSONArray(Reader reader) throws JSONException;
+	
+	public abstract JSONArray createJSONArray(Object array) throws JSONException;
+	
+	public abstract JSONArray createJSONArray(Object array, boolean useSuperClass) throws JSONException;
+	
+	public abstract JSONArray createJSONArray(String src) throws JSONException;
+
+	public abstract JSONObject createJSONObject();
+	
+        public abstract JSONObject createJSONObject(Reader reader) throws JSONException;
+	
+	public abstract JSONObject createJSONObject(Map m) throws JSONException;
+
+	public abstract JSONObject createJSONObject(Map m, boolean useSuperClass) throws JSONException;
+	
+	public abstract JSONObject createJSONObject(Object obj) throws JSONException;
+	
+	public abstract JSONObject createJSONObject(Object obj, boolean useSuperClass) throws JSONException;
+	
+	public abstract JSONObject createJSONObject(Object obj, String[] names) throws JSONException;
+	
+	public abstract JSONObject createJSONObject(String src) throws JSONException;
+	
+	public abstract JSONStringer createJSONStringer();
+	
+	public abstract JSONWriter createJSONWriter(Writer writer);
+	
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONObject.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONObject.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONObject.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONObject.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,94 @@
+/*
+ * 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.wink.json4j.compat;
+
+import java.io.Writer;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ *	 
+ */
+public interface JSONObject {
+	// ------- data accessor methods -------
+	public Object get(String key);
+	
+	public boolean getBoolean(String key) throws JSONException; 
+	
+	public double getDouble(String key) throws JSONException;
+	
+	public int getInt(String key) throws JSONException;
+        
+        public short getShort(String key) throws JSONException;
+	
+	public long getLong(String key) throws JSONException;
+	
+	public JSONArray getJSONArray(String key) throws JSONException;
+	
+	public JSONObject getJSONObject(String key) throws JSONException;
+	
+	public String getString(String key) throws JSONException;
+	
+	
+	// ------- modifier methods -------	
+	
+	public JSONObject append(String key, Object value) throws JSONException;
+	
+	public JSONObject put(String key, boolean value);
+	
+	public JSONObject put(String key, Collection value) throws JSONException;
+	
+	public JSONObject put(String key, double value);
+	
+	public JSONObject put(String key, int value);
+	
+	public JSONObject put(String key, long value);
+        
+        public JSONObject put(String key, short value);
+	
+	public JSONObject put(String key, Map value) throws JSONException;
+	
+	public JSONObject put(String key, Object value) throws JSONException;
+	
+	public Object remove(String key);
+	
+	// ------- utility methods -------	
+	
+	public boolean has(String key);
+	
+	public boolean isNull(String key);
+	
+	public Iterator keys();
+	
+	public Iterator sortedKeys();
+	
+	public int length();
+	
+	public JSONArray names();
+		
+	// ------- output methods -------	
+	
+        public String toString();
+	
+	public String toString(int indent);
+	
+	public Writer write(Writer w) throws JSONException;
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONString.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONString.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONString.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONString.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,29 @@
+/*
+ * 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.wink.json4j.compat;
+
+/**
+ *
+ */
+public interface JSONString {
+	
+	public String toJSONString();
+
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONStringer.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONStringer.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONStringer.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONStringer.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,29 @@
+/*
+ * 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.wink.json4j.compat;
+
+/**
+ *
+ */
+public interface JSONStringer extends JSONWriter {
+	
+	public String toString();
+
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONWriter.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONWriter.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONWriter.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/JSONWriter.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,52 @@
+/*
+ * 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.wink.json4j.compat;
+
+import java.io.IOException;
+
+/**
+ *
+ */
+public interface JSONWriter {
+
+    public JSONWriter array() throws IOException, IllegalStateException;
+
+    public JSONWriter endArray() throws IOException, IllegalStateException;
+
+    public JSONWriter endObject() throws IOException, IllegalStateException;
+
+    public JSONWriter key(String s) throws IOException, IllegalStateException;
+
+    public JSONWriter object() throws IOException, IllegalStateException;
+
+    public JSONWriter value(boolean b) throws IOException, IllegalStateException;
+
+    public JSONWriter value(double d) throws IOException, IllegalStateException;
+
+    public JSONWriter value(long l) throws IOException, IllegalStateException;
+
+    public JSONWriter value(short s) throws IOException, IllegalStateException;
+
+    public JSONWriter value(Object o) throws IOException, IllegalStateException, JSONException;
+
+    public void close() throws IOException, IllegalStateException;
+
+    public JSONWriter flush() throws IOException;
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONArrayDelegate.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONArrayDelegate.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONArrayDelegate.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONArrayDelegate.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,401 @@
+/*
+ * 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.wink.json4j.compat.impl;
+
+import java.io.Writer;
+import java.util.Collection;
+import java.util.Map;
+
+import org.apache.wink.json4j.compat.JSONArray;
+import org.apache.wink.json4j.compat.JSONException;
+import org.apache.wink.json4j.compat.JSONObject;
+
+/**
+ *
+ */
+public class ApacheJSONArrayDelegate implements JSONArray {
+
+    protected org.apache.wink.json4j.JSONArray delegate = null;
+
+    public ApacheJSONArrayDelegate() {
+        delegate = new org.apache.wink.json4j.JSONArray();
+    }
+
+    public ApacheJSONArrayDelegate(org.apache.wink.json4j.JSONArray jArray) {
+        delegate = jArray;
+    }
+
+    // ------- data accessor methods -------
+    public Object get(int index) {
+        try {
+            Object obj = this.delegate.get(index);
+            if (obj == null) {
+                return null;
+            } else {
+                Class clazz = obj.getClass();
+                if (org.apache.wink.json4j.JSONObject.class.isAssignableFrom(clazz)) {
+                    return new ApacheJSONObjectDelegate((org.apache.wink.json4j.JSONObject)obj);
+                } else if (org.apache.wink.json4j.JSONArray.class.isAssignableFrom(clazz)) {
+                    return new ApacheJSONArrayDelegate((org.apache.wink.json4j.JSONArray)obj);
+                } else if (Number.class.isAssignableFrom(clazz)) {
+                    return obj;
+                } else if (Boolean.class.isAssignableFrom(clazz)) {
+                    return obj;
+                } else if (String.class.isAssignableFrom(clazz)) {
+                    return obj;
+                }
+            }
+        } catch (Exception ex) {
+            // DO something
+        }
+        return null;
+    }
+
+    public boolean getBoolean(int index) throws JSONException {
+        try {
+            return delegate.getBoolean(index); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public double getDouble(int index) throws JSONException {
+        try {
+            return delegate.getDouble(index); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public int getInt(int index) throws JSONException {
+        try {
+            return delegate.getInt(index); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public long getLong(int index) throws JSONException {
+        try {
+            return delegate.getLong(index); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    } 
+
+    public short getShort(int index) throws JSONException {
+        try {
+            return delegate.getShort(index); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    } 
+
+    public String getString(int index) throws JSONException {
+        try {
+            return delegate.getString(index); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray getJSONArray(int index) throws JSONException {
+        try {
+            org.apache.wink.json4j.JSONArray obj = delegate.getJSONArray(index); 
+            if (obj == null) {
+                return null;
+            }
+            return new ApacheJSONArrayDelegate(obj);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONObject getJSONObject(int index) throws JSONException {
+        try {
+            org.apache.wink.json4j.JSONObject obj = delegate.getJSONObject(index); 
+            if (obj == null) {
+                return null;
+            }
+            return new ApacheJSONObjectDelegate(obj);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+
+    // ------- modifier methods -------	
+
+    public String join(String separator) {
+        return delegate.join(separator);
+    }
+
+    public JSONArray put(boolean value) {
+        delegate.put(value);
+        return this;
+    }
+
+    public JSONArray put(double value) {
+        delegate.put(value);
+        return this;
+    }
+
+    public JSONArray put(int value) {
+        delegate.put(value);
+        return this;
+    }
+
+    public JSONArray put(short value) {
+        delegate.put((short)value);
+        return this;
+    }
+
+    public JSONArray put(Collection value) throws JSONException {
+        try {
+            delegate.put(value);
+            return this;
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray put(int index, boolean value) throws JSONException {
+        try {
+            delegate.put(index,value);
+            return this;
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray put(int index, Collection value) throws JSONException {
+        try {
+            delegate.put(index,value);
+            return this;
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray put(int index, double value) throws JSONException {
+        try {
+            delegate.put(index,value);
+            return this;
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray put(int index, int value) throws JSONException {
+        try {
+            delegate.put(index,value);
+            return this;
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray put(int index, long value) throws JSONException {
+        try {
+            delegate.put(index,value);
+            return this;
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray put(int index, short value) throws JSONException {
+        try {
+            delegate.put(index,(short)value);
+            return this;
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray put(int index, Map value) throws JSONException {
+        try {
+            delegate.put(index,value);
+            return this;
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray put(int index, Object value) throws JSONException {
+        try {
+            if (value == null) {
+                this.delegate.put(index, value);
+            } else {
+                Class clazz = value.getClass();
+                if (org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(index, ((org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate)value).delegate);
+                } else if (org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(index, ((org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate)value).delegate);
+                } else if (Number.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(index, value);
+                } else if (Boolean.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(index, value);
+                } else if (String.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(index, value);
+                }
+            }
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+        return this;
+    }
+
+    public JSONArray put(long value) {
+        delegate.put(value);
+        return this;
+    }
+
+    public JSONArray put(Map value) throws JSONException {
+        try {
+            delegate.put(value);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+        return this;
+    }
+
+    public JSONArray put(Object value) throws JSONException {
+        try {
+            if (value == null) {
+                this.delegate.put(value);
+            } else {
+                Class clazz = value.getClass();
+                if (org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(((org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate)value).delegate);
+                } else if (org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(((org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate)value).delegate);
+                } else if (Number.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(value);
+                } else if (Boolean.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(value);
+                } else if (String.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(value);
+                }
+            }
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+        return this;
+    }
+
+    public Object remove(int index) {
+        try {
+            Object obj = this.delegate.remove(index);
+            if (obj == null) {
+                return null;
+            } else {
+                Class clazz = obj.getClass();
+                if (org.apache.wink.json4j.JSONObject.class.isAssignableFrom(clazz)) {
+                    return new ApacheJSONObjectDelegate((org.apache.wink.json4j.JSONObject)obj);
+                } else if (org.apache.wink.json4j.JSONArray.class.isAssignableFrom(clazz)) {
+                    return new ApacheJSONArrayDelegate((org.apache.wink.json4j.JSONArray)obj);
+                } else if (Number.class.isAssignableFrom(clazz)) {
+                    return obj;
+                } else if (Boolean.class.isAssignableFrom(clazz)) {
+                    return obj;
+                } else if (String.class.isAssignableFrom(clazz)) {
+                    return obj;
+                }
+            }
+        } catch (Exception ex) {
+            // DO something
+        }
+        return null;
+    }
+
+    // ------- utility methods -------	
+
+    public boolean isNull(int index) {
+        return delegate.isNull(index);
+    }
+
+    public int length() {
+        return delegate.length();
+    }
+
+    // ------- output methods -------
+
+    public String toString() {
+        return delegate.toString();
+    }
+
+    public String toString(int indent) {
+        try {
+            return delegate.toString(indent);
+        } catch (Exception ex) {
+            return "JSON Serializarion error: [" + ex.getMessage() + "]";
+        }
+    }
+
+    public Writer write(Writer w) throws JSONException {
+        try {
+            delegate.write(w);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+        return w;
+    }
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONFactory.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONFactory.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONFactory.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONFactory.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,185 @@
+/*
+ * 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.wink.json4j.compat.impl;
+
+import java.io.Reader;
+import java.util.Collection;
+import java.util.Map;
+import java.io.Writer;
+
+import org.apache.wink.json4j.compat.JSONArray;
+import org.apache.wink.json4j.compat.JSONException;
+import org.apache.wink.json4j.compat.JSONFactory;
+import org.apache.wink.json4j.compat.JSONObject;
+import org.apache.wink.json4j.compat.JSONStringer;
+import org.apache.wink.json4j.compat.JSONWriter;
+
+public class ApacheJSONFactory extends JSONFactory{
+    public JSONArray createJSONArray(){
+        org.apache.wink.json4j.JSONArray jArray = new org.apache.wink.json4j.JSONArray();
+        return new org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate(jArray);
+    }
+
+    public JSONArray createJSONArray(Collection collection) throws JSONException {
+        try {
+            org.apache.wink.json4j.JSONArray jArray = new org.apache.wink.json4j.JSONArray(collection);
+            return new org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate(jArray);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+
+    public JSONArray createJSONArray(Collection collect, boolean useSuperClass) throws JSONException{
+        //TODO:  Really implement this!
+        try {
+            org.apache.wink.json4j.JSONArray jArray = new org.apache.wink.json4j.JSONArray(collect);
+            return new org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate(jArray);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray createJSONArray(Reader reader) throws JSONException {
+        try {
+            org.apache.wink.json4j.JSONArray jArray = new org.apache.wink.json4j.JSONArray(reader);
+            return new org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate(jArray);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray createJSONArray(Object array) throws JSONException {
+        return null;
+    }
+
+    public JSONArray createJSONArray(Object array, boolean useSuperClass) throws JSONException {
+        return null;
+    }
+
+    public JSONArray createJSONArray(String src) throws JSONException {
+        try {
+            org.apache.wink.json4j.JSONArray jArray = new org.apache.wink.json4j.JSONArray(src);
+            return new org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate(jArray);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONObject createJSONObject() {
+        org.apache.wink.json4j.JSONObject jObj = new org.apache.wink.json4j.JSONObject();
+        return new org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate(jObj);
+    }
+
+    public JSONObject createJSONObject(Reader reader) throws JSONException {
+        try {
+            org.apache.wink.json4j.JSONObject jObj = new org.apache.wink.json4j.JSONObject(reader);
+            return new org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate(jObj);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONObject createJSONObject(Map m) throws JSONException {
+        try {
+            org.apache.wink.json4j.JSONObject jObj = new org.apache.wink.json4j.JSONObject(m);
+            return new org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate(jObj);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONObject createJSONObject(Map m, boolean useSuperClass) throws JSONException {
+        try {
+            org.apache.wink.json4j.JSONObject jObj = new org.apache.wink.json4j.JSONObject(m);
+            return new org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate(jObj);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONObject createJSONObject(Object obj)  throws JSONException {
+        try {
+            org.apache.wink.json4j.JSONObject jObj = new org.apache.wink.json4j.JSONObject();
+            return new org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate(jObj);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONObject createJSONObject(Object obj, boolean useSuperClass) throws JSONException {
+        // TODO:  Implement this.
+        try {
+            org.apache.wink.json4j.JSONObject jObj = new org.apache.wink.json4j.JSONObject();
+            return new org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate(jObj);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONObject createJSONObject(Object obj, String[] names) throws JSONException {
+        // TODO:  Implement this.
+        try {
+            org.apache.wink.json4j.JSONObject jObj = new org.apache.wink.json4j.JSONObject();
+            return new org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate(jObj);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONObject createJSONObject(String src) throws JSONException {
+        try {
+            org.apache.wink.json4j.JSONObject jObj = new org.apache.wink.json4j.JSONObject(src);
+            return new org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate(jObj);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONStringer createJSONStringer() {
+        return new org.apache.wink.json4j.compat.impl.ApacheJSONStringerDelegate();
+    }
+
+    public JSONWriter createJSONWriter(Writer writer) {
+        return new org.apache.wink.json4j.compat.impl.ApacheJSONWriterDelegate(writer);
+    }
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONObjectDelegate.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONObjectDelegate.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONObjectDelegate.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONObjectDelegate.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,340 @@
+/*
+ * 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.wink.json4j.compat.impl;
+
+import java.io.Writer;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.wink.json4j.compat.JSONArray;
+import org.apache.wink.json4j.compat.JSONException;
+import org.apache.wink.json4j.compat.JSONObject;
+
+/**
+ *	 
+ */          
+public class ApacheJSONObjectDelegate implements JSONObject {
+
+    protected org.apache.wink.json4j.JSONObject delegate = null;
+
+    public ApacheJSONObjectDelegate() {
+        delegate = new org.apache.wink.json4j.JSONObject();
+    }
+
+    public ApacheJSONObjectDelegate(org.apache.wink.json4j.JSONObject jObj) {
+        delegate = jObj;
+    }
+
+
+    // ------- data accessor methods -------
+    public Object get(String key) {
+        try {
+            Object obj = this.delegate.get(key);
+            if (obj == null) {
+                return null;
+            } else {
+                Class clazz = obj.getClass();
+                if (org.apache.wink.json4j.JSONObject.class.isAssignableFrom(clazz)) {
+                    return new ApacheJSONObjectDelegate((org.apache.wink.json4j.JSONObject)obj);
+                } else if (org.apache.wink.json4j.JSONArray.class.isAssignableFrom(clazz)) {
+                    return new ApacheJSONArrayDelegate((org.apache.wink.json4j.JSONArray)obj);
+                }else if (Number.class.isAssignableFrom(clazz)) {
+                    return obj;
+                } else if (Boolean.class.isAssignableFrom(clazz)) {
+                    return obj;
+                } else if (String.class.isAssignableFrom(clazz)) {
+                    return obj;
+                }
+            }
+        } catch (Exception ex) {
+            // DO something
+        }
+        return null;
+    }
+
+
+    public boolean getBoolean(String key) throws JSONException {
+        try {
+            return delegate.getBoolean(key); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public double getDouble(String key) throws JSONException {
+        try {
+            return delegate.getDouble(key); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public int getInt(String key) throws JSONException {
+        try {
+            return delegate.getInt(key); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public long getLong(String key) throws JSONException {
+        try {
+            return delegate.getLong(key); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public short getShort(String key) throws JSONException {
+        try {
+            return delegate.getShort(key); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public String getString(String key) throws JSONException {
+        try {
+            return delegate.getString(key); 
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONObject getJSONObject(String key) throws JSONException {
+        try {
+            org.apache.wink.json4j.JSONObject obj = delegate.getJSONObject(key); 
+            if (obj == null) {
+                return null;
+            }
+            return new ApacheJSONObjectDelegate(obj);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONArray getJSONArray(String key) {
+        return null;
+    }
+
+    public JSONObject append(String key, Object value) throws JSONException {
+        try {
+            Object oldVal = this.delegate.get(key);
+            JSONArray array = null;
+            if (oldVal == null) {
+                array = new org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate(new org.apache.wink.json4j.JSONArray());
+                if (this.has(key)) {
+                    // Add a null if the key was actually there, but just
+                    // had value of null.
+                    array.put((Object)null);
+                }
+            } else if (org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate.class.isAssignableFrom(oldVal.getClass())) {
+                array = (JSONArray)oldVal;
+            } else if (org.apache.wink.json4j.JSONArray.class.isAssignableFrom(oldVal.getClass())) {
+                array = new org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate((org.apache.wink.json4j.JSONArray)oldVal);
+            } else {
+                array = new org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate(new org.apache.wink.json4j.JSONArray());
+                array.put(oldVal);
+            }
+            array.put(value);
+            return put(key,array);
+        } catch (Exception ex) {
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+    }
+
+    public JSONObject put(String key, boolean value) {
+        try { 
+            delegate.put(key, value);
+        } catch (Exception ex) {
+            // Squelch, should never throw anyway.  
+        }
+        return this;
+    }
+
+    public JSONObject put(String key, Collection value) throws JSONException{
+        try {
+            delegate.put(key, value);
+        } catch (Exception ex){
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+        return this;
+    }
+
+    public JSONObject put(String key, double value) {
+        try { 
+            delegate.put(key, value);
+        } catch (Exception ex) {
+            // Squelch, should never throw anyway.  
+        }
+        return this;
+    }
+
+    public JSONObject put(String key, int value) {
+        try { 
+            delegate.put(key, value);
+        } catch (Exception ex) {
+            // Squelch, should never throw anyway.  
+        }
+        return this;
+    }
+
+    public JSONObject put(String key, long value) {
+        try { 
+            delegate.put(key, value);
+        } catch (Exception ex) {
+            // Squelch, should never throw anyway.  
+        }
+        return this;
+    }
+
+    public JSONObject put(String key, JSONObject value) {
+        try { 
+            delegate.put(key, ((org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate)value).delegate);
+        } catch (Exception ex) {
+            // Squelch, should never throw anyway.  
+        }
+        return this;
+    }
+
+    public JSONObject put(String key, short value) {
+        try { 
+            delegate.put(key, value);
+        } catch (Exception ex) {
+            // Squelch, should never throw anyway.  
+        }
+        return this;
+    }
+
+    public JSONObject put(String key, Map value) throws JSONException{
+        try {
+            delegate.put(key, value);
+        } catch (Exception ex){
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+        return this;
+    }
+
+    public JSONObject put(String key, Object value) throws JSONException {
+        if(value == null) {
+            try { 
+                delegate.put(key, value);
+            } catch (Exception ex) {
+                JSONException jex = new JSONException(ex.getMessage());
+                jex.initCause(ex);
+                throw jex;
+            }
+        } else {
+            try{
+                Class clazz = value.getClass();
+                if (org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(key, ((org.apache.wink.json4j.compat.impl.ApacheJSONObjectDelegate)value).delegate);
+                } else if (org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(key, ((org.apache.wink.json4j.compat.impl.ApacheJSONArrayDelegate)value).delegate);
+                } else if (Number.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(key, value);
+                } else if (Boolean.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(key, value);
+                } else if (String.class.isAssignableFrom(clazz)) {
+                    this.delegate.put(key, value);
+                }
+            } catch (Exception ex) {
+                JSONException jex = new JSONException(ex.getMessage());
+                jex.initCause(ex);
+                throw jex;
+            }
+        }
+        return this;
+    }
+
+    public Object remove(String key) {
+        return null;
+    }
+
+    // ------- utility methods -------	
+
+    public boolean has(String key) {
+        return delegate.has(key);
+    }
+
+    public boolean isNull(String key) {
+        return delegate.isNull(key);
+    }
+
+    public Iterator keys() {
+        return delegate.keys();
+    }
+
+    public Iterator sortedKeys() {
+        return delegate.sortedKeys();
+    }
+
+    public int length() {
+        return delegate.length();
+    }
+
+    public JSONArray names() {
+        return null;
+    }
+
+    public String toString() {
+        return delegate.toString();
+    }
+
+    public String toString(int indent) {
+        try {
+            return delegate.toString(indent);
+        } catch (Exception ex){
+            return "JSON Serialization error: [" + ex.getMessage() + "]";
+        }
+    }
+
+    public Writer write(Writer w) throws JSONException {
+        try {
+            delegate.write(w);
+        } catch (Exception ex){
+            JSONException jex = new JSONException(ex.getMessage());
+            jex.initCause(ex);
+            throw jex;
+        }
+        return w;
+    }
+}

Added: incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONStringerDelegate.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONStringerDelegate.java?rev=1021933&view=auto
==============================================================================
--- incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONStringerDelegate.java (added)
+++ incubator/wink/trunk/wink-json4j/src/main/java/org/apache/wink/json4j/compat/impl/ApacheJSONStringerDelegate.java Tue Oct 12 21:30:30 2010
@@ -0,0 +1,57 @@
+/*
+ * 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.wink.json4j.compat.impl;
+
+import org.apache.wink.json4j.compat.JSONStringer;
+
+import java.io.StringWriter;
+import java.io.IOException;
+
+/**
+ *
+ */
+public class ApacheJSONStringerDelegate extends ApacheJSONWriterDelegate implements JSONStringer{
+    public ApacheJSONStringerDelegate() {
+        super(new StringWriter());
+    }
+
+    /**
+     * Return a string of the stringer contents.  This also terminates the
+     * Stringer and it cannot be used again.  If any errors occur while trying to generate the JSON
+     * it returns an empty string.
+     */
+    public String toString() {
+        try {
+            super.flush();
+            super.close();
+            return ((StringWriter)writer).toString();
+        } catch (Exception ex) {
+            /* Squelch */
+            return "";
+        }
+    }
+
+    /**
+     * Over-ride to do nothing for the stringer.  Only toString() terminates the stringer object.
+     */
+    public void close() throws IOException, IllegalStateException {
+        // Do nothing.
+    }
+}



Mime
View raw message