Added:
/trunk/jsonTesting/src/main/java/net
/trunk/jsonTesting/src/main/java/net/ddaniels
/trunk/jsonTesting/src/main/java/net/ddaniels/json
/trunk/jsonTesting/src/main/java/net/ddaniels/json/JsonHierarchicalStreamReadWriteDriver.java
/trunk/jsonTesting/src/main/java/net/ddaniels/json/JsonReader.java
/trunk/jsonTesting/src/main/java/net/ddaniels/json/JsonTypeInformationWriter.java
Deleted:
/trunk/jsonTesting/src/main/java/json
Modified:
/trunk/jsonTesting/src/main/java/net/ddaniels/json/BasicJSONTest.java
/trunk/jsonTesting/src/main/java/net/ddaniels/json/FileResource.java
=======================================
--- /dev/null
+++
/trunk/jsonTesting/src/main/java/net/ddaniels/json/JsonHierarchicalStreamReadWriteDriver.java
Thu Oct 15 17:27:11 2009
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2006 Joe Walnes.
+ * Copyright (C) 2006, 2007, 2008 XStream Committers.
+ * All rights reserved.
+ *
+ * The software in this package is published under the terms of the BSD
+ * style license a copy of which has been included with this distribution
in
+ * the LICENSE.txt file.
+ *
+ * Created on 22. June 2006 by Mauro Talevi
+ */
+package net.ddaniels.json;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+
+import org.codehaus.jettison.json.JSONException;
+
+import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+import com.thoughtworks.xstream.io.StreamException;
+
+/**
+ * A driver for JSON that writes optimized JSON format, but is not able to
+ * deserialize the result.
+ *
+ * @author Paul Hammant
+ * @since 1.2
+ */
+public class JsonHierarchicalStreamReadWriteDriver implements
+ HierarchicalStreamDriver {
+
+ public HierarchicalStreamReader createReader(Reader in) throws
RuntimeException {
+ JsonReader reader = null;
+ try {
+ reader = new JsonReader(in);
+ } catch (IOException e) {
+ new RuntimeException(e);
+ } catch (JSONException e) {
+ new RuntimeException(e);
+ }
+ return reader;
+ }
+
+ public HierarchicalStreamReader createReader(InputStream in) throws
RuntimeException {
+ try {
+ return createReader(new InputStreamReader(in));
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Create a HierarchicalStreamWriter that writes JSON.
+ */
+ public HierarchicalStreamWriter createWriter(Writer out) {
+ return new JsonTypeInformationWriter(out);
+ }
+
+ public HierarchicalStreamWriter createWriter(OutputStream out) {
+ try {
+ // JSON spec requires UTF-8
+ return createWriter(new OutputStreamWriter(out, "UTF-8"));
+ } catch (UnsupportedEncodingException e) {
+ throw new StreamException(e);
+ }
+ }
+
+}
=======================================
--- /dev/null
+++ /trunk/jsonTesting/src/main/java/net/ddaniels/json/JsonReader.java Thu
Oct 15 17:27:11 2009
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2006 Joe Walnes.
+ * Copyright (C) 2006, 2007, 2008, 2009 XStream Committers.
+ * All rights reserved.
+ *
+ * The software in this package is published under the terms of the BSD
+ * style license a copy of which has been included with this distribution
in
+ * the LICENSE.txt file.
+ *
+ */
+package net.ddaniels.json;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.Iterator;
+
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+
+import com.thoughtworks.xstream.converters.ErrorWriter;
+import com.thoughtworks.xstream.io.AbstractReader;
+
+/**
+ * A simple reader that reads JSON in a pretty-printed indented stream.
Arrays,
+ * Lists and Sets rely on you NOT using XStream.addImplicitCollection(..).
+ *
+ * @author Doug Daniels
+ * @since 1.3.1
+ */
+public class JsonReader extends AbstractReader {
+ private JSONObject jsonObj = null;
+
+
+ public JsonReader(Reader in) throws IOException, JSONException {
+ String jsonString = readFileAsString(in);
+ jsonObj = new JSONObject(jsonString);
+ }
+
+ @Override
+ public void appendErrors(ErrorWriter errorWriter) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void close() {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public String getAttribute(String name) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public String getAttribute(int index) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public int getAttributeCount() {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public String getAttributeName(int index) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Iterator getAttributeNames() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public String getNodeName() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public String getValue() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public boolean hasMoreChildren() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public void moveDown() {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void moveUp() {
+ // TODO Auto-generated method stub
+
+ }
+
+ private static String readFileAsString(Reader rawReader)
+ throws java.io.IOException {
+ StringBuffer fileData = new StringBuffer(1000);
+ BufferedReader reader = new BufferedReader(rawReader);
+ char[] buf = new char[1024];
+ int numRead = 0;
+ while ((numRead = reader.read(buf)) != -1) {
+ String readData = String.valueOf(buf, 0, numRead);
+ fileData.append(readData);
+ buf = new char[1024];
+ }
+ reader.close();
+ return fileData.toString();
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/jsonTesting/src/main/java/net/ddaniels/json/JsonTypeInformationWriter.java
Thu Oct 15 17:27:11 2009
@@ -0,0 +1,354 @@
+/*
+ * Copyright (C) 2006 Joe Walnes.
+ * Copyright (C) 2006, 2007, 2008, 2009 XStream Committers.
+ * All rights reserved.
+ *
+ * The software in this package is published under the terms of the BSD
+ * style license a copy of which has been included with this distribution
in
+ * the LICENSE.txt file.
+ *
+ * Created on 28. November 2008 by Joerg Schaible
+ */
+package net.ddaniels.json;
+
+import java.io.Writer;
+import java.lang.reflect.Type;
+
+import com.thoughtworks.xstream.converters.ConversionException;
+import com.thoughtworks.xstream.core.util.QuickWriter;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+import com.thoughtworks.xstream.io.json.AbstractJsonWriter;
+
+
+/**
+ * A simple writer that outputs JSON in a pretty-printed indented stream.
Arrays, Lists and Sets
+ * rely on you NOT using XStream.addImplicitCollection(..).
+ *
+ * @author Paul Hammant
+ * @author Jörg Schaible
+ * @since 1.3.1
+ */
+public class JsonTypeInformationWriter extends AbstractJsonWriter {
+
+ protected final QuickWriter writer;
+ protected final Format format;
+ private int depth;
+ private boolean newLineProposed;
+
+ /**
+ * @deprecated As of upcoming use {@link
JsonTypeInformationWriter#JsonWriter(Writer, Format) instead}
+ */
+ public JsonTypeInformationWriter(Writer writer, char[] lineIndenter,
String newLine) {
+ this(writer, 0, new Format(
+ lineIndenter, newLine.toCharArray(), Format.SPACE_AFTER_LABEL
+ | Format.COMPACT_EMPTY_ELEMENT));
+ }
+
+ /**
+ * @deprecated As of upcoming use {@link
JsonTypeInformationWriter#JsonWriter(Writer, Format) instead}
+ */
+ public JsonTypeInformationWriter(Writer writer, char[] lineIndenter) {
+ this(writer, 0, new Format(lineIndenter, new char[]{'\n'},
Format.SPACE_AFTER_LABEL
+ | Format.COMPACT_EMPTY_ELEMENT));
+ }
+
+ /**
+ * @deprecated As of upcoming use {@link
JsonTypeInformationWriter#JsonWriter(Writer, Format) instead}
+ */
+ public JsonTypeInformationWriter(Writer writer, String lineIndenter,
String newLine) {
+ this(writer, 0, new Format(
+ lineIndenter.toCharArray(), newLine.toCharArray(),
Format.SPACE_AFTER_LABEL
+ | Format.COMPACT_EMPTY_ELEMENT));
+ }
+
+ /**
+ * @deprecated As of upcoming use {@link
JsonTypeInformationWriter#JsonWriter(Writer, Format) instead}
+ */
+ public JsonTypeInformationWriter(Writer writer, String lineIndenter) {
+ this(writer, 0, new Format(
+ lineIndenter.toCharArray(), new char[]{'\n'},
Format.SPACE_AFTER_LABEL
+ | Format.COMPACT_EMPTY_ELEMENT));
+ }
+
+ public JsonTypeInformationWriter(Writer writer) {
+ this(writer, 0, new Format(
+ new char[]{' ', ' '}, new char[]{'\n'},
Format.SPACE_AFTER_LABEL
+ | Format.COMPACT_EMPTY_ELEMENT));
+ }
+
+ /**
+ * @since 1.3.1
+ * @deprecated As of upcoming use {@link
JsonTypeInformationWriter#JsonWriter(Writer, int, Format) instead}
+ */
+ public JsonTypeInformationWriter(Writer writer, char[] lineIndenter,
String newLine, int mode) {
+ this(writer, mode, new Format(
+ lineIndenter, newLine.toCharArray(), Format.SPACE_AFTER_LABEL
+ | Format.COMPACT_EMPTY_ELEMENT));
+ }
+
+ /**
+ * Create a JsonWriter where the writer mode can be chosen.
+ *
+ * @param writer the {@link Writer} where the JSON is written to
+ * @param mode the JsonWriter mode
+ * @since 1.3.1
+ * @see #JsonWriter(Writer, int, Format)
+ */
+ public JsonTypeInformationWriter(Writer writer, int mode) {
+ this(writer, mode, new Format(
+ new char[]{' ', ' '}, new char[]{'\n'},
Format.SPACE_AFTER_LABEL
+ | Format.COMPACT_EMPTY_ELEMENT));
+ }
+
+ /**
+ * Create a JsonWriter where the format is provided.
+ *
+ * @param writer the {@link Writer} where the JSON is written to
+ * @param format the JSON format definition
+ * @since upcoming
+ * @see #JsonWriter(Writer, int, Format)
+ */
+ public JsonTypeInformationWriter(Writer writer, Format format) {
+ this(writer, 0, format);
+ }
+
+ /**
+ * Create a JsonWriter where the writer mode can be chosen and the
format definition is
+ * provided.
+ * <p>
+ * Following constants can be used as bit mask for the mode:
+ * <ul>
+ * <li>{@link #DROP_ROOT_MODE}: drop the root node</li>
+ * <li>{@link #STRICT_MODE}: do not throw {@link ConversionException},
if writer should
+ * generate invalid JSON</li>
+ * <li>{@link #EXPLICIT_MODE}: ensure that all available data is
explicitly written even if
+ * addition objects must be added</li>
+ * </ul>
+ * </p>
+ *
+ * @param writer the {@link Writer} where the JSON is written to
+ * @param mode the JsonWriter mode
+ * @param format the JSON format definition
+ * @since upcoming
+ */
+ public JsonTypeInformationWriter(Writer writer, int mode, Format
format) {
+ this(writer, mode, format, 1024);
+ }
+
+ /**
+ * Create a JsonWriter.
+ *
+ * @param writer the {@link Writer} where the JSON is written to
+ * @param mode the JsonWriter mode
+ * @param format the JSON format definition
+ * @param bufferSize the buffer size of the internally used QuickWriter
+ * @see JsonTypeInformationWriter#JsonWriter(Writer, int, Format)
+ * @since upcoming
+ */
+ public JsonTypeInformationWriter(Writer writer, int mode, Format
format, int bufferSize) {
+ super(mode);
+ this.writer = new QuickWriter(writer, bufferSize);
+ this.format = format;
+ depth = (mode & DROP_ROOT_MODE) == 0 ? -1 : 0;
+ }
+
+ public void flush() {
+ writer.flush();
+ }
+
+ public void close() {
+ writer.close();
+ }
+
+ public HierarchicalStreamWriter underlyingWriter() {
+ return this;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void startObject(String name) {
+ if (newLineProposed) {
+ writeNewLine();
+ }
+ writer.write('{');
+ startNewLine();
+ if (name != null) {
+ addLabel(name);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void addLabel(String name) {
+ if (newLineProposed) {
+ writeNewLine();
+ }
+ writer.write('"');
+ writeText(name);
+ writer.write("\":");
+ if ((format.mode() & Format.SPACE_AFTER_LABEL) != 0) {
+ writer.write(' ');
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void addValue(String value, Type type) {
+ if (newLineProposed) {
+ writeNewLine();
+ }
+ if (type == Type.STRING) {
+ writer.write('"');
+ }
+ writeText(value);
+ if (type == Type.STRING) {
+ writer.write('"');
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void startArray() {
+ if (newLineProposed) {
+ writeNewLine();
+ }
+ writer.write("[");
+ startNewLine();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void nextElement() {
+ writer.write(",");
+ writeNewLine();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void endArray() {
+ endNewLine();
+ writer.write("]");
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ protected void endObject() {
+ endNewLine();
+ writer.write("}");
+ }
+
+ private void startNewLine() {
+ if ( ++depth > 0) {
+ newLineProposed = true;
+ }
+ }
+
+ private void endNewLine() {
+ if (depth-- > 0) {
+ if (((format.mode() & Format.COMPACT_EMPTY_ELEMENT) != 0) &&
newLineProposed) {
+ newLineProposed = false;
+ } else {
+ writeNewLine();
+ }
+ }
+ }
+
+ private void writeNewLine() {
+ int depth = this.depth;
+ writer.write(format.getNewLine());
+ while (depth-- > 0) {
+ writer.write(format.getLineIndenter());
+ }
+ newLineProposed = false;
+ }
+
+ private void writeText(String text) {
+ int length = text.length();
+ for (int i = 0; i < length; i++ ) {
+ char c = text.charAt(i);
+ switch (c) {
+ case '"':
+ this.writer.write("\\\"");
+ break;
+ case '\\':
+ this.writer.write("\\\\");
+ break;
+ default:
+ if (c > 0x1f) {
+ this.writer.write(c);
+ } else {
+ this.writer.write("\\u");
+ String hex = "000" + Integer.toHexString(c);
+ this.writer.write(hex.substring(hex.length() - 4));
+ }
+ }
+ }
+ }
+
+ /**
+ * Format definition for JSON.
+ *
+ * @author Jörg Schaible
+ * @since upcoming
+ */
+ public static class Format {
+
+ public static int SPACE_AFTER_LABEL = 1;
+ public static int COMPACT_EMPTY_ELEMENT = 2;
+
+ private char[] lineIndenter;
+ private char[] newLine;
+ private final int mode;
+
+ /**
+ * Create a new Formatter.
+ *
+ * @param lineIndenter the characters used for indenting the line
+ * @param newLine the characters used to create a new line
+ * @param mode the flags for the format modes
+ * @since upcoming
+ */
+ public Format(char[] lineIndenter, char[] newLine, int mode) {
+ this.lineIndenter = lineIndenter;
+ this.newLine = newLine;
+ this.mode = mode;
+ }
+
+ /**
+ * Retrieve the lineIndenter.
+ *
+ * @return the lineIndenter
+ * @since upcoming
+ */
+ public char[] getLineIndenter() {
+ return this.lineIndenter;
+ }
+
+ /**
+ * Retrieve the newLine.
+ *
+ * @return the newLine
+ * @since upcoming
+ */
+ public char[] getNewLine() {
+ return this.newLine;
+ }
+
+ /**
+ * Retrieve the mode flags of the formatter.
+ *
+ * @return the mode
+ * @since upcoming
+ */
+ public int mode() {
+ return this.mode;
+ }
+ }
+}
=======================================
--- /trunk/jsonTesting/src/main/java/json/BasicJSONTest.java Thu Oct 15
16:15:03 2009
+++ /trunk/jsonTesting/src/main/java/net/ddaniels/json/BasicJSONTest.java
Thu Oct 15 17:27:11 2009
@@ -1,4 +1,4 @@
-package json;
+package net.ddaniels.json;
import java.io.IOException;
=======================================
--- /trunk/jsonTesting/src/main/java/json/FileResource.java Thu Oct 15
16:15:03 2009
+++ /trunk/jsonTesting/src/main/java/net/ddaniels/json/FileResource.java
Thu Oct 15 17:27:11 2009
@@ -1,4 +1,4 @@
-package json;
+package net.ddaniels.json;
import java.io.File;
import java.net.URISyntaxException;