You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by yo...@apache.org on 2006/11/28 06:53:57 UTC

svn commit: r479919 - in /labs/noggit: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/noggit/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/noggit/

Author: yonik
Date: Mon Nov 27 21:53:55 2006
New Revision: 479919

URL: http://svn.apache.org/viewvc?view=rev&rev=479919
Log:
initial noggit code checkin, JSON parser in pretty good shape, form of writer undecided

Added:
    labs/noggit/pom.xml   (with props)
    labs/noggit/src/
    labs/noggit/src/main/
    labs/noggit/src/main/java/
    labs/noggit/src/main/java/org/
    labs/noggit/src/main/java/org/apache/
    labs/noggit/src/main/java/org/apache/noggit/
    labs/noggit/src/main/java/org/apache/noggit/CharArr.java   (with props)
    labs/noggit/src/main/java/org/apache/noggit/CharUtil.java   (with props)
    labs/noggit/src/main/java/org/apache/noggit/JSONParser.java   (with props)
    labs/noggit/src/main/java/org/apache/noggit/JSONUtil.java   (with props)
    labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java   (with props)
    labs/noggit/src/main/java/org/apache/noggit/TextWriter.java   (with props)
    labs/noggit/src/test/
    labs/noggit/src/test/java/
    labs/noggit/src/test/java/org/
    labs/noggit/src/test/java/org/apache/
    labs/noggit/src/test/java/org/apache/noggit/
    labs/noggit/src/test/java/org/apache/noggit/MyParse.java   (with props)
    labs/noggit/src/test/java/org/apache/noggit/StaxPerf.java   (with props)
    labs/noggit/src/test/java/org/apache/noggit/TestJSONParser.java   (with props)

Added: labs/noggit/pom.xml
URL: http://svn.apache.org/viewvc/labs/noggit/pom.xml?view=auto&rev=479919
==============================================================================
--- labs/noggit/pom.xml (added)
+++ labs/noggit/pom.xml Mon Nov 27 21:53:55 2006
@@ -0,0 +1,36 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.noggit</groupId>
+  <artifactId>noggit</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <name>Apache Noggit</name>
+  <url>http://www.apache.org/~yonik/noggit</url>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+  <!-- uncomment for Java5 support
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  -->
+
+</project>
+
+

Propchange: labs/noggit/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: labs/noggit/pom.xml
------------------------------------------------------------------------------
    svn:executable = *

Propchange: labs/noggit/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: labs/noggit/src/main/java/org/apache/noggit/CharArr.java
URL: http://svn.apache.org/viewvc/labs/noggit/src/main/java/org/apache/noggit/CharArr.java?view=auto&rev=479919
==============================================================================
--- labs/noggit/src/main/java/org/apache/noggit/CharArr.java (added)
+++ labs/noggit/src/main/java/org/apache/noggit/CharArr.java Mon Nov 27 21:53:55 2006
@@ -0,0 +1,263 @@
+/**
+ * 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.noggit;
+
+
+import java.io.IOException;
+import java.io.Reader;
+import java.nio.CharBuffer;
+
+// CharArr origins
+// V1.0 7/06/97
+// V1.1 9/21/99
+// V1.2 2/02/04  // Java5 features
+// V1.3 11/26/06 // Make safe for Java 1.4, work into Noggit
+// @author yonik
+
+
+// Java5 version could look like the following:
+// public class CharArr implements CharSequence, Appendable, Readable, Closeable {
+
+/**
+ * @author yonik
+ * @version $Id$
+ */
+public class CharArr {
+  protected char[] buf;
+  protected int start;
+  protected int end;
+
+  public CharArr() {
+    this(32);
+  }
+
+  public CharArr(int size) {
+    buf = new char[size];
+  }
+
+  public CharArr(char[] arr, int start, int end) {
+    set(arr,start,end);
+  }
+
+  public void setStart(int start) { this.start = start; }
+  public void setEnd(int end) { this.end = end; }
+  public void set(char[] arr, int start, int end) {
+    this.buf = arr;
+    this.start = start;
+    this.end = end;
+  }
+
+  public char[] getArray() { return buf; }
+  public int getStart() { return start; }
+  public int getEnd() { return end; }
+  public int size() { return end-start; }
+  public int length() { return size(); }
+  public int capacity() { return buf.length; }
+
+
+  public char charAt(int index) {
+    return buf[start+index];
+  }
+
+  public CharArr subSequence(int start, int end) {
+    return new CharArr(buf, start, end);
+  }
+
+  public int read() throws IOException {
+    if (start>=end) return -1;
+    return buf[start++];
+  }
+
+  public int read(char cbuf[], int off, int len) throws IOException {
+//TODO
+    return 0;
+  }
+
+
+  public void unsafeWrite(char b) {
+    buf[end++] = b;
+  }
+
+  public void unsafeWrite(int b) { unsafeWrite((char)b); }
+
+  public void unsafeWrite(char b[], int off, int len) {
+    System.arraycopy(b, off, buf, end, len);
+    end += len;
+  }
+
+  protected void resize(int len) {
+    char newbuf[] = new char[Math.max(buf.length << 1, len)];
+    System.arraycopy(buf, start, newbuf, 0, size());
+    buf = newbuf;
+  }
+
+  public void reserve(int num) {
+    if (end + num > buf.length) resize(end + num);
+  }
+
+  public void write(char b) {
+    if (end >= buf.length) {
+      resize(end+1);
+    }
+    unsafeWrite(b);
+  }
+
+  public void write(int b) { write((char)b); }
+
+  public final void write(char[] b) {
+    write(b,0,b.length);
+  }
+
+  public void write(char b[], int off, int len) {
+    reserve(len);
+    unsafeWrite(b, off, len);
+  }
+
+  public final void write(CharArr arr) {
+    write(arr.buf, start, end-start);
+  }
+
+  public void write(String s) {
+    reserve(s.length());
+    s.getChars(0,s.length(),buf,end);
+    end+=s.length();
+  }
+
+  public void flush() {
+  }
+
+  public final void reset() {
+    start=end=0;
+  }
+
+  public void close() throws IOException {
+  }
+
+
+  public char[] toCharArray() {
+    char newbuf[] = new char[size()];
+    System.arraycopy(buf, start, newbuf, 0, size());
+    return newbuf;
+  }
+
+
+  public String toString() {
+    return new String(buf, start, size());
+  }
+
+
+  public int read(CharBuffer cb) throws IOException {
+
+    /***
+    int sz = size();
+    if (sz<=0) return -1;
+    if (sz>0) cb.put(buf, start, sz);
+    return -1;
+    ***/
+
+    int sz = size();
+    if (sz>0) cb.put(buf, start, sz);
+    start=end;
+    while (true) {
+      fill();
+      int s = size();
+      if (s==0) return sz==0 ? -1 : sz;
+      sz += s;
+      cb.put(buf, start, s);
+    }
+  }
+
+
+  public int fill() throws IOException {
+    return 0;  // or -1?
+  }
+
+
+}
+
+
+class NullCharArr extends CharArr {
+  public NullCharArr() {
+    super(null,0,0);
+  }
+  public void unsafeWrite(char b) {}
+
+  public void unsafeWrite(char b[], int off, int len) {}
+
+  public void unsafeWrite(int b) {}
+
+  public void write(char b) {}
+
+  public void write(String s) {}
+
+  public void write(char b[], int off, int len) {}
+
+  public void write(int b) {}
+
+  public void reserve(int num) {}
+
+  protected void resize(int len) {}
+}
+
+
+
+// IDEA: a subclass that refills the array from a reader?
+class CharArrReader extends CharArr {
+  protected final Reader in;
+
+  public CharArrReader(Reader in, int size) {
+    super(size);
+    this.in = in;
+  }
+
+  public int read() throws IOException {
+    if (start>=end) fill();
+    return start>=end ? -1 : buf[start++];
+  }
+
+  public int read(CharBuffer cb) throws IOException {
+    // empty the buffer and then read direct
+    int sz = size();
+    if (sz>0) cb.put(buf,start,end);
+    int sz2 = in.read(cb);
+    if (sz2>=0) return sz+sz2;
+    return sz>0 ? sz : -1;
+  }
+
+  public int fill() throws IOException {
+    if (start>=end) {
+      reset();
+    } else if (start>0) {
+      System.arraycopy(buf, start, buf, 0, size());
+      end=size(); start=0;
+    }
+    /***
+    // fill fully or not???
+    do {
+      int sz = in.read(buf,end,buf.length-end);
+      if (sz==-1) return;
+      end+=sz;
+    } while (end < buf.length);
+    ***/
+
+    int sz = in.read(buf,end,buf.length-end);
+    if (sz>0) end+=sz;
+    return sz;
+  }
+
+}

Propchange: labs/noggit/src/main/java/org/apache/noggit/CharArr.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: labs/noggit/src/main/java/org/apache/noggit/CharArr.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: labs/noggit/src/main/java/org/apache/noggit/CharArr.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: labs/noggit/src/main/java/org/apache/noggit/CharUtil.java
URL: http://svn.apache.org/viewvc/labs/noggit/src/main/java/org/apache/noggit/CharUtil.java?view=auto&rev=479919
==============================================================================
--- labs/noggit/src/main/java/org/apache/noggit/CharUtil.java (added)
+++ labs/noggit/src/main/java/org/apache/noggit/CharUtil.java Mon Nov 27 21:53:55 2006
@@ -0,0 +1,55 @@
+/**
+ * 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.noggit;
+
+
+/**
+ * @author yonik
+ * @version $Id$
+ */
+public class CharUtil {
+
+  // belongs in number utils or charutil?
+  public long parseLong(char[] arr, int start, int end) {
+    long x = 0;
+    boolean negative = arr[start] == '-';
+    for (int i=negative ? start+1 : start; i<end; i++) {
+      // If constructing the largest negative number, this will overflow
+      // to the largest negative number.  This is OK since the negation of
+      // the largest negative number is itself in two's complement.
+      x = x * 10 + (arr[i] - '0');
+    }
+    // could replace conditional-move with multiplication of sign... not sure
+    // which is faster.
+    return negative ? -x : x;
+  }
+
+
+  public int compare(char[] a, int a_start, int a_end, char[] b, int b_start, int b_end) {
+    int a_len = a_end - a_start;
+    int b_len = b_end - b_start;
+    int len = Math.min(a_len,b_len);
+    while (--len>=0) {
+      int c = a[a_start] - b[b_start];
+      if (c!=0) return c;
+      a_start++; b_start++;
+    }
+    return a_len-b_len;
+  }
+
+}

Propchange: labs/noggit/src/main/java/org/apache/noggit/CharUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: labs/noggit/src/main/java/org/apache/noggit/CharUtil.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: labs/noggit/src/main/java/org/apache/noggit/CharUtil.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: labs/noggit/src/main/java/org/apache/noggit/JSONParser.java
URL: http://svn.apache.org/viewvc/labs/noggit/src/main/java/org/apache/noggit/JSONParser.java?view=auto&rev=479919
==============================================================================
--- labs/noggit/src/main/java/org/apache/noggit/JSONParser.java (added)
+++ labs/noggit/src/main/java/org/apache/noggit/JSONParser.java Mon Nov 27 21:53:55 2006
@@ -0,0 +1,725 @@
+/**
+ * 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.noggit;
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * @author yonik
+ * @version $Id$
+ */
+
+public class JSONParser {
+
+  /** Event indicating a JSON string value, including member names of objects */
+  public static final int STRING=1;
+  /** Event indicating a JSON number value which fits into a signed 64 bit integer */
+  public static final int LONG=2;
+  /** Event indicating a JSON number value which has a fractional part or an exponent
+   * and with string length <= 23 chars not including sign.  This covers
+   * all representations of normal values for Double.toString().
+   */
+  public static final int NUMBER=3;
+  /** Event indicating a JSON number value that was not produced by toString of any
+   * Java primitive numerics such as Double or Long.  It is either
+   * an integer outside the range of a 64 bit signed integer, or a floating
+   * point value with a string representation of more than 23 chars.
+    */
+  public static final int BIGNUMBER=4;
+  /** Event indicating a JSON boolean */
+  public static final int BOOLEAN=5;
+  /** Event indicating a JSON null */
+  public static final int NULL=6;
+  /** Event indicating the start of a JSON object */
+  public static final int OBJECT_START=7;
+  /** Event indicating the end of a JSON object */
+  public static final int OBJECT_END=8;
+  /** Event indicating the start of a JSON array */
+  public static final int ARRAY_START=9;
+  /** Event indicating the end of a JSON array */
+  public static final int ARRAY_END=10;
+  /** Event indicating the end of input has been reached */
+  public static final int EOF=11;
+
+
+  private static final CharArr devNull = new NullCharArr();
+
+
+  final char[] buf;  // input buffer with JSON text in it
+  int start;         // current position in the buffer
+  int end;           // end position in the buffer (one past last valid index)
+  final Reader in;   // optional reader to obtain data from
+  boolean eof=false; // true if the end of the stream was reached.
+  int gpos;          // global position = gpos + start
+
+
+  public JSONParser(Reader in) {
+    this(in, new char[8192]);
+    // 8192 matches the default buffer size of a BufferedReader so double
+    // buffering of the data is avoided.
+  }
+
+  public JSONParser(Reader in, char[] buffer) {
+    this.in = in;
+    this.buf = buffer;
+  }
+
+  // idea - if someone passes us a CharArrayReader, we could
+  // directly use that buffer as it's protected.
+
+  public JSONParser(char[] data, int start, int end) {
+    this.in = null;
+    this.buf = data;
+    this.start = start;
+    this.end = end;
+  }
+
+  public JSONParser(String data) {
+    this(data, 0, data.length());
+  }
+
+  public JSONParser(String data, int start, int end) {
+    this.in = null;
+    this.buf = new char[end-start];
+    data.getChars(start,end,buf,0);
+  }
+
+  // temporary output buffer
+  private final CharArr out = new CharArr(64);
+
+  // We need to keep some state in order to (at a minimum) know if
+  // we should skip ',' or ':'.
+  private byte[] stack = new byte[16];
+  private int ptr=0;     // pointer into the stack of parser states
+  private byte state=0;  // current parser state
+
+  // parser states stored in the stack
+  private static final byte DID_OBJSTART =1;  // '{' just read
+  private static final byte DID_ARRSTART =2;  // '[' just read
+  private static final byte DID_ARRELEM =3;   // array element just read
+  private static final byte DID_MEMNAME =4;   // object member name (map key) just read
+  private static final byte DID_MEMVAL =5;    // object member value (map val) just read
+
+  // info about value that was just read (or is in the middle of being read)
+  private int valstate;
+
+  // push current parser state (use at start of new container)
+  private final void push() {
+    if (ptr >= stack.length) {
+      // doubling here is probably overkill, but anything that needs to double more than
+      // once (32 levels deep) is very atypical anyway.
+      byte[] newstack = new byte[stack.length<<1];
+      System.arraycopy(stack,0,newstack,0,stack.length);
+      stack = newstack;
+    }
+    stack[ptr++] = state;
+  }
+
+  // pop  parser state (use at end of container)
+  private final void pop() {
+    if (--ptr<0) {
+      throw err("Unbalanced container");
+    } else {
+      state = stack[ptr];
+    }
+  }
+
+  protected void fill() throws IOException {
+    if (in!=null) {
+      gpos += end;
+      start=0;
+      int num = in.read(buf,0,buf.length);
+      end = num>=0 ? num : 0;
+    }
+    if (start>=end) eof=true;
+  }
+
+  private void getMore() throws IOException {
+    fill();
+    if (start>=end) {
+      throw err(null);
+    }
+  }
+
+  protected int getChar() throws IOException {
+    if (start>=end) {
+      fill();
+      if (start>=end) return -1;
+    }
+    return buf[start++];
+  }
+
+  private int getCharNWS() throws IOException {
+    for (;;) {
+      int ch = getChar();
+      if (!(ch==' ' || ch=='\t' || ch=='\n' || ch=='\r')) return ch;
+    }
+  }
+
+  private void expect(char[] arr) throws IOException {
+    for (int i=1; i<arr.length; i++) {
+      int ch = getChar();
+      if (ch != arr[i]) {
+        if (ch==-1) throw new RuntimeException("Unexpected EOF");
+        throw new RuntimeException("Expected " + new String(arr));
+      }
+    }
+  }
+
+  private RuntimeException err(String msg) {
+    // We can't tell if EOF was hit by comparing start<=end
+    // because the illegal char could have been the last in the buffer
+    // or in the stream.  To deal with this, the "eof" var was introduced
+    if (!eof && start>0) start--;  // backup one char
+    String chs = "char=" + ((start>=end) ? "(EOF)" : "" + (char)buf[start]);
+    String pos = "position=" + (gpos+start);
+    String tot = chs + ',' + pos;
+    if (msg==null) {
+      if (start>=end) msg = "Unexpected EOF";
+      else msg="JSON Parse Error";
+    }
+    return new RuntimeException(msg + ": " + tot);
+  }
+
+
+  private boolean bool; // boolean value read
+  private long lval;    // long value read
+  private int nstate;   // current state while reading a number
+  private static final int HAS_FRACTION = 0x01;  // nstate flag, '.' already read
+  private static final int HAS_EXPONENT = 0x02;  // nstate flag, '[eE][+-]?[0-9]' already read
+
+  // Returns the negative of the long read... only significant if valstate==LONG after
+  // this call.  firstChar should be the first numeric digit read.
+  // We build up the number in the negative plane since it's larger (by one) than
+  // the positive plane.
+  private long readNumber(int firstChar) throws IOException {
+    out.unsafeWrite(firstChar);   // unsafe OK since we know output is big enough
+    long v = '0' - firstChar;
+    for (int i=0; i<22; i++) {
+      int ch = getChar();
+      // TODO: is this switch faster as an if-then-else?
+      switch(ch) {
+        case '0':
+        case '1':
+        case '2':
+        case '3':
+        case '4':
+        case '5':
+        case '6':
+        case '7':
+        case '8':
+        case '9':
+          v = v *10 - (ch-'0');
+          out.unsafeWrite(ch);
+          continue;
+        case '.':
+          out.unsafeWrite('.');
+          valstate = readFrac(out,22-i);
+          return 0;
+        case 'e':
+        case 'E':
+          out.unsafeWrite(ch);
+          valstate = readExp(out,22-i);
+          return 0;
+        default:
+          // return the number, relying on nextEvent() to return an error
+          // for invalid chars following the number
+          if (ch!=-1) --start;   // push back last char if not EOF
+          // if we didn't overflow, it was a LONG, otherwise BIGNUMBER
+          valstate = v<=0 ? LONG : BIGNUMBER;
+          return v;
+      }
+    }
+    nstate=0;
+    valstate = BIGNUMBER;
+    return 0;
+  }
+
+  // read digits right of decimal point
+  private int readFrac(CharArr arr, int lim) throws IOException {
+    nstate = HAS_FRACTION;  // deliberate set instead of '|'
+    while(--lim>=0) {
+      int ch = getChar();
+      if (ch>='0' && ch<='9') {
+        arr.write(ch);
+      } else if (ch=='e' || ch=='E') {
+        arr.write(ch);
+        return readExp(arr,lim);
+      } else {
+        if (ch!=-1) start--; // back up
+        return NUMBER;
+      }
+    }
+    return BIGNUMBER;
+  }
+
+
+  // call after 'e' or 'E' has been seen to read the rest of the exponent
+  private int readExp(CharArr arr, int lim) throws IOException {
+    nstate |= HAS_EXPONENT;
+    int ch = getChar(); lim--;
+
+    if (ch=='+' || ch=='-') {
+      arr.write(ch);
+      ch = getChar(); lim--;
+    }
+
+    // make sure at least one digit is read.
+    if (ch<'0' || ch>'9') {
+      throw err("missing exponent number");
+    }
+    arr.write(ch);
+
+    return readExpDigits(arr,lim);
+  }
+
+  // continuation of readExpStart
+  private int readExpDigits(CharArr arr, int lim) throws IOException {
+    while (--lim>=0) {
+      int ch = getChar();
+      if (ch>='0' && ch<='9') {
+        arr.write(ch);
+      } else {
+        if (ch!=-1) start--; // back up
+        return NUMBER;
+      }
+    }
+    return BIGNUMBER;
+  }
+
+  private void continueNumber(CharArr arr) throws IOException {
+    if (arr != out) arr.write(out);
+
+    if ((nstate & HAS_EXPONENT)!=0){
+      readExpDigits(arr, Integer.MAX_VALUE);
+      return;
+    }
+    if (nstate != 0) {
+      readFrac(arr, Integer.MAX_VALUE);
+      return;
+    }
+
+    for(;;) {
+      int ch = getChar();
+      if (ch>='0' && ch <='9') {
+        arr.write(ch);
+      } else if (ch=='.') {
+        arr.write(ch);
+        readFrac(arr,Integer.MAX_VALUE);
+        return;
+      } else if (ch=='e' || ch=='E') {
+        arr.write(ch);
+        readExp(arr,Integer.MAX_VALUE);
+        return;
+      } else {
+        if (ch!=-1) start--;
+        return;
+      }
+    }
+  }
+
+
+  private int hexval(int hexdig) {
+    if (hexdig>='0' && hexdig <='9') {
+      return hexdig-'0';
+    } else if (hexdig>='A' && hexdig <='F') {
+      return hexdig-'A';
+    } else if (hexdig>='a' && hexdig <='f') {
+      return hexdig-'a';
+    }
+    throw err("invalid hex digit");
+  }
+
+  // backslash has already been read when this is called
+  private char readEscapedChar() throws IOException {
+    switch (getChar()) {
+      case '\\' : return '\\';
+      case '/' : return '/';
+      case '\n' : return '\n';
+      case '\r' : return '\r';
+      case '\t' : return '\t';
+      case '\f' : return '\f';
+      case '\b' : return '\b';
+      case 'u' :
+        return (char)(
+               (hexval(getChar()) << 12)
+             | (hexval(getChar()) << 8)
+             | (hexval(getChar()) << 4)
+             | (hexval(getChar())));
+    }
+    throw err("Invalid character escape in string");
+  }
+
+  // a dummy buffer we can use to point at other buffers
+  private final CharArr tmp = new CharArr(null,0,0);
+
+  private CharArr readStringChars() throws IOException {
+     char c=0;
+     int i;
+     for (i=start; i<end; i++) {
+      c = buf[i];
+      if (c=='"') {
+        tmp.set(buf,start,i);  // directly use input buffer
+        start=i+1; // advance past last '"'
+        return tmp;
+      } else if (c=='\\') {
+        break;
+      }
+    }
+    out.reset();
+    readStringChars2(out, i);
+    return out;
+  }
+
+
+  // middle is the pointer to the middle of a buffer to start scanning for a non-string
+  // character ('"' or "/").  start<=middle<end
+  // this should be faster for strings with fewer escapes, but probably slower for many escapes.
+  private void readStringChars2(CharArr arr, int middle) throws IOException {
+    for (;;) {
+      if (middle>=end) {
+        arr.write(buf,start,middle-start);
+        getMore();
+        middle=start;
+      }
+      int ch = buf[middle++];
+      if (ch=='"') {
+        arr.write(buf,start,middle-start-1);
+        start=middle;
+        return;
+      } else if (ch=='\\') {
+        arr.write(buf,start,middle-start-1);
+        start=middle;
+        arr.write(readEscapedChar());
+      }
+    }
+  }
+
+
+  /*** alternate implelentation
+  // middle is the pointer to the middle of a buffer to start scanning for a non-string
+  // character ('"' or "/").  start<=middle<end
+  private void readStringChars2a(CharArr arr, int middle) throws IOException {
+    int ch=0;
+    for(;;) {
+      // find the next non-string char
+      for (; middle<end; middle++) {
+        ch = buf[middle];
+        if (ch=='"' || ch=='\\') break;
+      }
+
+      arr.write(buf,start,middle-start);
+      if (middle>=end) {
+        getMore();
+        middle=start;
+      } else {
+        start = middle+1;   // set buffer pointer to correct spot
+        if (ch=='"') {
+          valstate=0;
+          return;
+        } else if (ch=='\\') {
+          arr.write(readEscapedChar());
+          if (start>=end) getMore();
+          middle=start;
+        }
+      }
+    }
+  }
+  ***/
+
+
+  // return the next event when parser is in a neutral state (no
+  // map separators or array element separators to read
+  private int next(int ch) throws IOException {
+    for(;;) {
+      switch (ch) {
+        case ' ':
+        case '\t': break;
+        case '\r':
+        case '\n': break;  // try and keep track of linecounts?
+        case '"' :
+          valstate = STRING;
+          return STRING;
+        case '{' :
+          push();
+          state= DID_OBJSTART;
+          return OBJECT_START;
+        case '[':
+          push();
+          state=DID_ARRSTART;
+          return ARRAY_START;
+        case '0' :
+          out.reset();
+          //special case '0'?  If next char isn't '.' val=0
+          ch=getChar();
+          if (ch=='.') {
+            start--; ch='0';
+            readNumber('0');
+            return valstate;
+          } else if (ch>'9' || ch<'0') {
+            out.unsafeWrite('0');
+            start--;
+            lval = 0;
+            valstate=LONG;
+            return LONG;
+          } else {
+            throw err("Leading zeros not allowed");
+          }
+        case '1' :
+        case '2' :
+        case '3' :
+        case '4' :
+        case '5' :
+        case '6' :
+        case '7' :
+        case '8' :
+        case '9' :
+          out.reset();
+          lval = -readNumber(ch);
+          return valstate;
+        case '-' :
+          out.reset();
+          out.unsafeWrite('-');
+          ch = getChar();
+          if (ch<'0' || ch>'9') throw err("expected digit after '-'");
+          lval = readNumber(ch);
+          return valstate;
+        case 't':
+          valstate=BOOLEAN;
+          // TODO: test performance of this non-branching inline version.
+          // if ((('r'-getChar())|('u'-getChar())|('e'-getChar())) != 0) err("");
+          expect(JSONUtil.TRUE_CHARS);
+          bool=true;
+          return BOOLEAN;
+        case 'f':
+          valstate=BOOLEAN;
+          expect(JSONUtil.FALSE_CHARS);
+          bool=false;
+          return BOOLEAN;
+        case 'n':
+          valstate=NULL;
+          expect(JSONUtil.NULL_CHARS);
+          return NULL;
+        case -1:
+          if (getLevel()>0) throw new RuntimeException("Premature EOF");
+          return EOF;
+        default: throw err(null);
+      }
+
+      ch = getChar();
+    }
+  }
+
+  public String toString() {
+    return "start="+start+",end="+end+",state="+state+"valstate="+valstate;
+  }
+
+
+  /** Returns the next event encountered in the JSON stream, one of
+   * <ul>
+   * <li>{@link #STRING}</li>
+   * <li>{@link #LONG}</li>
+   * <li>{@link #NUMBER}</li>
+   * <li>{@link #BIGNUMBER}</li>
+   * <li>{@link #BOOLEAN}</li>
+   * <li>{@link #NULL}</li>
+   * <li>{@link #OBJECT_START}</li>
+   * <li>{@link #OBJECT_END}</li>
+   * <li>{@link #OBJECT_END}</li>
+   * <li>{@link #ARRAY_START}</li>
+   * <li>{@link #ARRAY_END}</li>
+   * <li>{@link #EOF}</li>
+   * </ul>
+   */
+  public int nextEvent() throws IOException {
+    if (valstate==STRING) {
+      readStringChars2(devNull,start);
+    }
+    else if (valstate==BIGNUMBER) {
+      continueNumber(devNull);
+    }
+
+    valstate=0;
+
+    int ch;   // TODO: factor out getCharNWS() to here and check speed
+    switch (state) {
+      case 0:
+        return next(getCharNWS());
+      case DID_OBJSTART:
+        ch = getCharNWS();
+        if (ch=='}') {
+          pop();
+          return OBJECT_END;
+        }
+        if (ch != '"') {
+          throw err("Expected string");
+        }
+        state = DID_MEMNAME;
+        valstate = STRING;
+        return STRING;
+      case DID_MEMNAME:
+        ch = getCharNWS();
+        if (ch!=':') {
+          throw err("Expected key,value separator ':'");
+        }
+        state = DID_MEMVAL;  // set state first because it might be pushed...
+        return next(getChar());
+      case DID_MEMVAL:
+        ch = getCharNWS();
+        if (ch=='}') {
+          pop();
+          return OBJECT_END;
+        } else if (ch!=',') {
+          throw err("Expected ',' or '}'");
+        }
+        ch = getCharNWS();
+        if (ch != '"') {
+          throw err("Expected string");
+        }
+        state = DID_MEMNAME;
+        valstate = STRING;
+        return STRING;
+      case DID_ARRSTART:
+        ch = getCharNWS();
+        if (ch==']') {
+          pop();
+          return ARRAY_END;
+        }
+        state = DID_ARRELEM;  // set state first, might be pushed...
+        return next(ch);
+      case DID_ARRELEM:
+        ch = getCharNWS();
+        if (ch==']') {
+          pop();
+          return ARRAY_END;
+        } else if (ch!=',') {
+          throw err("Expected ',' or ']'");
+        }
+        // state = DID_ARRELEM;
+        return next(getChar());
+    }
+    return 0;
+  }
+
+
+  private void goTo(int what) throws IOException {
+    if (valstate==what) { valstate=0; return; }
+    if (valstate==0) {
+      int ev = nextEvent();      // TODO
+      if (valstate!=what) {
+        throw err("type mismatch");
+      }
+      valstate=0;
+    }
+    else {
+      throw err("type mismatch");
+    }
+  }
+
+  /** Returns the JSON string value, decoding any escaped characters. */
+  public String getString() throws IOException {
+    return getStringChars().toString();
+  }
+
+  /** Returns the characters of a JSON string value, decoding any escaped characters.
+   * <p/>The underlying buffer of the returned <code>CharArr</code> should *not* be
+   * modified as it may be shared with the input buffer.
+   * <p/>The returned <code>CharArr</code> will only be valid up until
+   * the next JSONParser method is called.  Any required data should be
+   * read before that point.
+   */
+  public CharArr getStringChars() throws IOException {
+    goTo(STRING);
+    return readStringChars();
+  }
+
+  /** Reads a JSON string into the output, decoding any escaped characters. */
+  public void getString(CharArr output) throws IOException {
+    goTo(STRING);
+    readStringChars2(output,start);
+  }
+
+  /** Reads a number from the input stream and parses it as a long, only if
+   * the value will in fact fit into a signed 64 bit integer. */
+  public long getLong() throws IOException {
+    goTo(LONG);
+    return lval;
+  }
+
+  /** Reads a number from the input stream and parses it as a double */
+  public double getDouble() throws IOException {
+    return Double.parseDouble(getNumberChars().toString());
+  }
+
+  /** Returns the characters of a JSON numeric value.
+   * <p/>The underlying buffer of the returned <code>CharArr</code> should *not* be
+   * modified as it may be shared with the input buffer.
+   * <p/>The returned <code>CharArr</code> will only be valid up until
+   * the next JSONParser method is called.  Any required data should be
+   * read before that point.
+   */  
+  public CharArr getNumberChars() throws IOException {
+    int ev=0;
+    if (valstate==0) ev = nextEvent();
+
+    if (valstate == LONG || valstate == NUMBER) {
+      valstate=0;
+      return out;
+    }
+    else if (valstate==BIGNUMBER) {
+      continueNumber(out);
+      valstate=0;
+      return out;
+    } else {
+      throw err("Unexpected " + ev);
+    }
+  }
+
+  /** Reads a JSON numeric value into the output. */
+  public void getNumberChars(CharArr output) throws IOException {
+    int ev=0;
+    if (valstate==0) ev=nextEvent();
+    if (valstate == LONG || valstate == NUMBER) output.write(this.out);
+    else if (valstate==BIGNUMBER) {
+      continueNumber(output);
+    } else {
+      throw err("Unexpected " + ev);
+    }
+    valstate=0;
+  }
+
+  /** Reads a boolean value */  
+  public boolean getBoolean() throws IOException {
+    goTo(BOOLEAN);
+    return bool;
+  }
+
+  /** Reads a null value */
+  public void getNull() throws IOException {
+    goTo(NULL);
+  }
+
+  /**
+   * @return the current nesting level, the number of parent objects or arrays.
+   */
+  public int getLevel() {
+    return ptr;
+  }
+
+}

Propchange: labs/noggit/src/main/java/org/apache/noggit/JSONParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: labs/noggit/src/main/java/org/apache/noggit/JSONParser.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: labs/noggit/src/main/java/org/apache/noggit/JSONParser.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: labs/noggit/src/main/java/org/apache/noggit/JSONUtil.java
URL: http://svn.apache.org/viewvc/labs/noggit/src/main/java/org/apache/noggit/JSONUtil.java?view=auto&rev=479919
==============================================================================
--- labs/noggit/src/main/java/org/apache/noggit/JSONUtil.java (added)
+++ labs/noggit/src/main/java/org/apache/noggit/JSONUtil.java Mon Nov 27 21:53:55 2006
@@ -0,0 +1,100 @@
+/**
+ * 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.noggit;
+
+/**
+ * @author yonik
+ * @version $Id$
+ */
+
+public class JSONUtil {
+  public static final char[] TRUE_CHARS = new char[] {'t','r','u','e'};
+  public static final char[] FALSE_CHARS = new char[] {'f','a','l','s','e'};
+  public static final char[] NULL_CHARS = new char[] {'n','u','l','l'};
+  public static final char[] HEX_CHARS = new char[] {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
+  public static final char VALUE_SEPARATOR = ',';
+  public static final char NAME_SEPARATOR = ':';
+  public static final char OBJECT_START = '{';
+  public static final char OBJECT_END = '}';
+  public static final char ARRAY_START = '[';
+  public static final char ARRAY_END = ']';
+
+  public static void writeNumber(long number, CharArr out) {
+    out.write(Long.toString(number));
+  }
+
+  public static void writeNumber(double number, CharArr out) {
+    out.write(Double.toString(number));
+  }
+
+  public static void writeString(String val, CharArr out) {
+
+  }
+
+  public static void writeString(CharArr val, CharArr out) {
+    writeString(val.getArray(), val.getStart(), val.getEnd(), out);
+  }
+
+  public static void writeString(char[] val, int start, int end, CharArr out) {
+    out.write('"');
+    writeStringPart(val,start,end,out);
+    out.write('"');
+  }
+
+  public static void writeStringPart(char[] val, int start, int end, CharArr out) {
+    for (int i=start; i<end; i++) {
+      char ch = val[i];
+      switch(ch) {
+        case '"':
+        case '\\':
+          out.write('\\');
+          out.write(ch);
+          break;
+        case '\r': out.write('\\'); out.write('r'); break;
+        case '\n': out.write('\\'); out.write('n'); break;
+        case '\t': out.write('\\'); out.write('t'); break;
+        case '\b': out.write('\\'); out.write('b'); break;
+        case '\f': out.write('\\'); out.write('f'); break;
+        // case '/':
+        default:
+          if (ch <= 0x1F) {
+            unicodeEscape(ch,out);
+          } else {
+            out.write(ch);
+          }
+      }
+    }
+  }
+
+  public static void unicodeEscape(int ch, CharArr out) {
+    out.write('\\');
+    out.write('u');
+    out.write(HEX_CHARS[ch>>>12]);
+    out.write(HEX_CHARS[(ch>>>8)&0xf]);
+    out.write(HEX_CHARS[(ch>>>4)&0xf]);
+    out.write(HEX_CHARS[ch&0xf]);
+  }
+
+  public static void writeNull(CharArr out) {
+    out.write(NULL_CHARS);
+  }
+
+  public static void writeBoolean(boolean val, CharArr out) {
+    out.write(val ? TRUE_CHARS : FALSE_CHARS);
+  }
+}

Propchange: labs/noggit/src/main/java/org/apache/noggit/JSONUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: labs/noggit/src/main/java/org/apache/noggit/JSONUtil.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: labs/noggit/src/main/java/org/apache/noggit/JSONUtil.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java
URL: http://svn.apache.org/viewvc/labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java?view=auto&rev=479919
==============================================================================
--- labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java (added)
+++ labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java Mon Nov 27 21:53:55 2006
@@ -0,0 +1,103 @@
+/**
+ * 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.noggit;
+
+/**
+ * @author yonik
+ * @version $Id$
+ */
+
+/***
+interface TextWriter {
+  public void writeNumber(int val);
+  public void writeNumber(long val);
+  public void writeString(String val);
+}
+***/
+
+// how to couple with JSONParser to allow streaming large values from input to
+// output?
+// IDEA 1) have JSONParser.getString(JSONWriter out)?
+// IDEA 2) have an output CharArr that acts as a filter to escape data
+
+// IDEA: a subclass of JSONWriter could provide more state and stricter checking
+
+public class JSONWriter extends TextWriter {
+  int level;
+  boolean doIndent;
+  final CharArr out;
+
+  JSONWriter(CharArr out) {
+    this.out = out;
+  }
+
+  public void writeNull() {
+    JSONUtil.writeNull(out);
+  }
+
+  public void writeString(String str) {
+    JSONUtil.writeString(str,out);
+  }
+
+  public void writeString(CharArr str) {
+    JSONUtil.writeString(str,out);
+  }
+
+  public void write(long number) {
+    JSONUtil.writeNumber(number,out);
+  }
+
+  public void write(double number) {
+    JSONUtil.writeNumber(number,out);
+  }
+
+  public void write(boolean bool) {
+    JSONUtil.writeBoolean(bool,out);
+  }
+
+  public void writeNumber(CharArr digits) {
+    out.write(digits);
+  }
+
+  public void startObject() {
+    out.write('{');
+  }
+
+  public void endObject() {
+    out.write('}');
+  }
+
+  public void startArray() {
+    out.write('[');
+  }
+
+  public void endArray() {
+    out.write(']');
+  }
+
+  public void writeValueSeparator() {
+    out.write(',');
+  }
+
+  public void writeNameSeparator() {
+    out.write(':');    
+  }
+
+}
+
+

Propchange: labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: labs/noggit/src/main/java/org/apache/noggit/JSONWriter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: labs/noggit/src/main/java/org/apache/noggit/TextWriter.java
URL: http://svn.apache.org/viewvc/labs/noggit/src/main/java/org/apache/noggit/TextWriter.java?view=auto&rev=479919
==============================================================================
--- labs/noggit/src/main/java/org/apache/noggit/TextWriter.java (added)
+++ labs/noggit/src/main/java/org/apache/noggit/TextWriter.java Mon Nov 27 21:53:55 2006
@@ -0,0 +1,80 @@
+/**
+ * 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.noggit;
+
+import java.util.Map;
+import java.util.List;
+import java.util.Collection;
+
+/**
+ * @author yonik
+ * @version $Id$
+ */
+public abstract class TextWriter {
+  public abstract void writeNull();
+
+  public abstract void writeString(String str);
+
+  public abstract void writeString(CharArr str);
+
+  public abstract void write(long number);
+
+  public abstract void write(double number);
+
+  public abstract void write(boolean bool);
+
+  public abstract void writeNumber(CharArr digits);
+
+  public abstract void startObject();
+
+  public abstract void endObject();
+
+  public abstract void startArray();
+
+  public abstract void endArray();
+
+  public abstract void writeValueSeparator();
+
+  public abstract void writeNameSeparator();
+
+  /*** Should object serialization be in this class, or another class?
+
+  void write(Map map);
+
+  void write(Collection collection);
+
+  void write(Object[] arr);
+  void write(byte[] arr);
+  void write(char[] arr);
+  void write(short[] arr);
+  void write(int[] arr);
+  void write(long[] arr);
+  void write(double[] arr);
+
+  void writeNameValue(String name, Object val)?
+  ***/
+}
+
+class TextSerialzer {
+  public void serialize(TextWriter writer, Map map) {
+    writer.startObject();
+
+    writer.endObject();
+  }
+
+}

Propchange: labs/noggit/src/main/java/org/apache/noggit/TextWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: labs/noggit/src/main/java/org/apache/noggit/TextWriter.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: labs/noggit/src/main/java/org/apache/noggit/TextWriter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: labs/noggit/src/test/java/org/apache/noggit/MyParse.java
URL: http://svn.apache.org/viewvc/labs/noggit/src/test/java/org/apache/noggit/MyParse.java?view=auto&rev=479919
==============================================================================
--- labs/noggit/src/test/java/org/apache/noggit/MyParse.java (added)
+++ labs/noggit/src/test/java/org/apache/noggit/MyParse.java Mon Nov 27 21:53:55 2006
@@ -0,0 +1,105 @@
+/**
+ * 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.noggit;
+
+import java.io.StringReader;
+
+// handling numbers... either put a limit of buffersize on the number
+// while reading it all into another buffer, or ???
+
+/*
+ISSUE: repeated keys in a map... is it an issue?
+Leave it to a higher level to validate this?
+
+The JSON standard says that keys *SHOULD* be unique, not *MUST*!!!
+That means we should support this when parsing.
+*/
+
+
+/**
+ * @author yonik
+ * @version $Id$
+ */
+public class MyParse {
+  public static void main(String[] args) throws Exception {
+    StringReader sr;
+    sr = new StringReader("\"hello\"");
+    sr = new StringReader("[\"a\",\"b\"]");
+    sr = new StringReader("  [  \"a\"  ,  \"b\"  ] ");
+    sr = new StringReader("  [  1  ,  23, 456,7890 ,999]  ");
+    sr = new StringReader("{}");
+    sr = new StringReader("{ \"a\" : 1}");
+    sr = new StringReader("[[1]]");
+
+    JSONParser js = new JSONParser(sr);
+    int maxEv=100;
+    for (int i=0; i<maxEv; i++) {
+      System.out.println("  Parser State:" + js);
+      int ev = js.nextEvent();
+      System.out.println("  Parser State After nextEvent:" + js);
+      switch (ev) {
+        case JSONParser.STRING:
+          System.out.println("STRING:");
+          System.out.println(js.getString());
+          break;
+        case JSONParser.NUMBER:
+          System.out.println("NUMBER:");
+          // System.out.println(js.getString());
+          break;
+        case JSONParser.LONG:
+          System.out.println("LONG:");
+          //System.out.println(js.getLong());
+          System.out.println(js.getNumberChars());
+          break;
+        case JSONParser.NULL:
+          System.out.println("NULL");
+          break;
+        case JSONParser.BOOLEAN:
+          System.out.println("BOOLEAN:");
+          // System.out.println(js.getString());
+          break;
+        case JSONParser.OBJECT_START:
+          System.out.println("OBJECT_START");
+          break;
+        case JSONParser.OBJECT_END:
+          System.out.println("OBJECT_END");
+          break;
+        case JSONParser.ARRAY_START:
+          System.out.println("ARRAY_START");
+          break;
+        case JSONParser.ARRAY_END:
+          System.out.println("ARRAY_END");
+          break;
+         case JSONParser.EOF:
+          System.out.println("EOF");
+          break;
+        default:
+          System.out.println("UNKNOWN_EVENT_ID:"+ev);
+          break;
+      }
+
+      if (ev == JSONParser.EOF) break;
+    }
+
+  }
+
+  // Could also have a cursor that contained information about the last entity
+  // read... string, number (that would allow to pass back more specific info
+  // about the number easier.
+
+}

Propchange: labs/noggit/src/test/java/org/apache/noggit/MyParse.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: labs/noggit/src/test/java/org/apache/noggit/MyParse.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: labs/noggit/src/test/java/org/apache/noggit/MyParse.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: labs/noggit/src/test/java/org/apache/noggit/StaxPerf.java
URL: http://svn.apache.org/viewvc/labs/noggit/src/test/java/org/apache/noggit/StaxPerf.java?view=auto&rev=479919
==============================================================================
--- labs/noggit/src/test/java/org/apache/noggit/StaxPerf.java (added)
+++ labs/noggit/src/test/java/org/apache/noggit/StaxPerf.java Mon Nov 27 21:53:55 2006
@@ -0,0 +1,121 @@
+/**
+ * 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.noggit;
+
+/**
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+**/
+
+import java.io.IOException;
+import java.io.StringReader;
+
+/**
+ * @author yonik
+ * @version $Id$
+ */
+public class StaxPerf {
+
+  /*** requires a stax parser or Java6
+  static XMLInputFactory factory = XMLInputFactory.newInstance();
+
+  public static int parseXML(String s) throws XMLStreamException {
+    XMLStreamReader sr = factory.createXMLStreamReader(new StringReader(s));
+    char[] buf = new char[80];    // more efficient retrieval of strings
+
+    for(;;) {
+      int ev = sr.next();
+      switch (ev) {
+        case XMLStreamConstants.END_DOCUMENT: return -1;
+        case XMLStreamConstants.START_ELEMENT: return sr.getLocalName().charAt(0);
+        case XMLStreamConstants.CHARACTERS:
+          int len = sr.getTextCharacters(0,buf,0,80);
+          return len>0 ? buf[0] : -1;
+        default: break;
+      }
+    }
+  }
+
+  public static int parseJSON(String s) throws IOException {
+    JSONParser sr = new JSONParser(new StringReader(s));
+    for(;;) {
+      int ev = sr.nextEvent();
+      switch (ev) {
+        case JSONParser.EOF: return 1;
+        case JSONParser.STRING: return sr.getStringChars().read();  // first char
+        case JSONParser.LONG: return (int)sr.getLong();
+        default: break;
+      }
+    }
+  }
+
+
+  public static void main(String[] argv) throws Exception {
+    int iter = Integer.parseInt(argv[0]);
+
+    String[] xml = {
+      "<arr> <int>1</int> <int>2</int> <int>3</int> <int>4</int> <int>5</int> </arr>",
+      "<arr> <str>1</str> <str>2</str> <str>3</str> <str>4</str> <str>5</str> </arr>",
+      "<map> <k1>val1</k1> <k2>val2</k2> <k3>val3</k3> <k4>val4</k4> <k5>val5</k5> </map>",
+      "<a><b><c><d><e><f><g><h><i><j>big nesting</j></i></h></g></f></e></d></c></b></a>",
+      "<bigarr>"
+        + "<map> <k1>val1a</k1> <k2>val2a</k2> <k3>val3a</k3> <k4>val4a</k4> <k5>val5a</k5> </map>"
+        + "<map> <k1>val1b</k1> <k2>val2b</k2> <k3>val3b</k3> <k4>val4b</k4> <k5>val5b</k5> </map>"
+        + "<map> <k1>val1c</k1> <k2>val2c</k2> <k3>val3c</k3> <k4>val4c</k4> <k5>val5c</k5> </map>"
+        + "<map> <k1>val1d</k1> <k2>val2d</k2> <k3>val3d</k3> <k4>val4d</k4> <k5>val5d</k5> </map>"
+        + "<map> <k1>val1e</k1> <k2>val2e</k2> <k3>val3e</k3> <k4>val4e</k4> <k5>val5e</k5> </map>"
+        + "</bigarr>"
+    };
+
+    String[] json = {
+      "[ 1, 2, 3, 4, 5 ]",
+      "[ \"1\", \"2\", \"3\", \"4\", \"5\" ] ",
+      "{ \"k1\":\"val1\", \"k2\":\"val2\", \"k3\":\"val3\", \"k4\":\"val4\", \"k5\":\"val5\" }",
+      "{\"a\":{\"b\":{\"c\":{\"d\":{\"e\":{\"f\":{\"g\":{\"h\":{\"i\":{\"j\":{}}}}}}}}}}}",
+      "["
+        + "{ \"k1\":\"val1a\", \"k2\":\"val2a\", \"k3\":\"val3a\", \"k4\":\"val4a\", \"k5\":\"val5a\" },"
+        + "{ \"k1\":\"val1b\", \"k2\":\"val2b\", \"k3\":\"val3b\", \"k4\":\"val4b\", \"k5\":\"val5b\" },"
+        + "{ \"k1\":\"val1c\", \"k2\":\"val2c\", \"k3\":\"val3c\", \"k4\":\"val4c\", \"k5\":\"val5c\" },"
+        + "{ \"k1\":\"val1d\", \"k2\":\"val2d\", \"k3\":\"val3d\", \"k4\":\"val4d\", \"k5\":\"val5d\" },"
+        + "{ \"k1\":\"val1e\", \"k2\":\"val2e\", \"k3\":\"val3e\", \"k4\":\"val4e\", \"k5\":\"val5e\" }"
+       +"]",
+    };
+
+    long start = System.currentTimeMillis();
+
+    int s = 0;
+    int ret=0;
+    for (int i=0; i<iter; i++) {
+      if (s>=xml.length) s=0;
+      // String val = xml[s];
+      // System.out.println(val);
+      // ret += parseXML(val);
+      String val = json[s];
+      ret += parseJSON(val);
+      s++;
+    }
+
+
+    long end = System.currentTimeMillis();
+    System.out.println("Result:"+ret+", TIME="+(end-start));
+  }
+  ***/
+
+}

Propchange: labs/noggit/src/test/java/org/apache/noggit/StaxPerf.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: labs/noggit/src/test/java/org/apache/noggit/StaxPerf.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: labs/noggit/src/test/java/org/apache/noggit/StaxPerf.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: labs/noggit/src/test/java/org/apache/noggit/TestJSONParser.java
URL: http://svn.apache.org/viewvc/labs/noggit/src/test/java/org/apache/noggit/TestJSONParser.java?view=auto&rev=479919
==============================================================================
--- labs/noggit/src/test/java/org/apache/noggit/TestJSONParser.java (added)
+++ labs/noggit/src/test/java/org/apache/noggit/TestJSONParser.java Mon Nov 27 21:53:55 2006
@@ -0,0 +1,157 @@
+/**
+ * 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.noggit;
+
+import junit.framework.TestCase;
+
+import java.util.Random;
+import java.io.StringReader;
+import java.io.IOException;
+
+/**
+ * @author yonik
+ * @version $Id$
+ */
+public class TestJSONParser extends TestCase {
+  public static Random r = new Random(0);
+
+  public static JSONParser getParser(String s) {
+    return getParser(s, r.nextInt(2));
+  }
+  
+  public static JSONParser getParser(String s, int type) {
+    JSONParser parser=null;
+    switch (type) {
+      case 0:
+        // test directly using input buffer
+        parser = new JSONParser(s.toCharArray(),0,s.length());
+        break;
+      case 1:
+        // test using Reader...
+        // small input buffers can help find bugs on boundary conditions
+        parser = new JSONParser(new StringReader(s), new char[r.nextInt(25)+1]);
+        break;
+    }
+    return parser;
+  }
+
+  public static byte[] events = new byte[256];
+  static {
+    events['{'] = JSONParser.OBJECT_START;
+    events['}'] = JSONParser.OBJECT_END;
+    events['['] = JSONParser.ARRAY_START;
+    events[']'] = JSONParser.ARRAY_END;
+    events['s'] = JSONParser.STRING;
+    events['b'] = JSONParser.BOOLEAN;
+    events['l'] = JSONParser.LONG;
+    events['n'] = JSONParser.NUMBER;
+    events['N'] = JSONParser.BIGNUMBER;
+    events['0'] = JSONParser.NULL;
+    events['e'] = JSONParser.EOF;
+  }
+
+  // match parser states with the expected states
+  public static void parse(JSONParser p, String input, String expected) throws IOException {
+    expected += "e";
+    for (int i=0; i<expected.length(); i++) {
+      int ev = p.nextEvent();
+      int expect = events[expected.charAt(i)];
+      if (ev != expect) {
+        TestCase.fail("Expected " + expect + ", got " + ev
+                + "\n\tINPUT=" + input
+                + "\n\tEXPECTED=" + expected
+                + "\n\tAT=" + i + " ("+ expected.charAt(i) + ")");
+      }
+    }
+  }
+
+  public static void parse(String input, String expected) throws IOException {
+    input = input.replace('\'','"');
+    for (int i=0; i<Integer.MAX_VALUE; i++) {
+      JSONParser p = getParser(input,i);
+      if (p==null) break;
+      parse(p,input,expected);
+    }    
+  }
+
+  public static void err(String input) throws IOException {
+    try {
+      JSONParser p = getParser(input);
+      while (p.nextEvent() != JSONParser.EOF);
+    } catch (Exception e) {
+      return;
+    }
+    TestCase.fail("Input should failed:'" + input + "'");    
+  }
+
+  public void testNull() throws IOException {
+    err("[nullz]");
+    parse("[null]","[0]");
+    parse("{'hi':null}","{s0}");
+  }
+
+  public void testNumbers() throws IOException {
+    err("[00]");
+    err("[003]");
+    err("[00.3]");
+    err("[1e1.1]");
+    err("[+1]");
+    err("NaN");
+    err("Infinity");
+  }
+
+  public void testArray() throws IOException {
+    parse("[]","[]");
+    parse("[ ]","[]");
+    parse(" \r\n\t[\r\t\n ]\r\n\t ","[]");
+
+    parse("[0]","[l]");
+    parse("['0']","[s]");
+    parse("[0,'0',0.1]","[lsn]");
+
+    parse("[[[[[]]]]]","[[[[[]]]]]");
+    parse("[[[[[0]]]]]","[[[[[l]]]]]");
+
+    err("]");
+    err("[");
+    err("[[]");
+    err("[]]");
+    err("[}");
+    err("{]");
+    err("['a':'b']");
+  }
+
+  public void testObject() throws IOException {
+    parse("{}","{}");
+    parse("{}","{}");
+    parse(" \r\n\t{\r\t\n }\r\n\t ","{}");
+
+    parse("{'':null}","{s0}");
+
+    err("}");
+    err("{");
+    err("{{}");
+    err("{}}");;
+    err("{1}");
+    err("{'a'}");
+    err("{'a','b'}");
+    err("{null:'b'}");
+  }
+
+
+}

Propchange: labs/noggit/src/test/java/org/apache/noggit/TestJSONParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: labs/noggit/src/test/java/org/apache/noggit/TestJSONParser.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: labs/noggit/src/test/java/org/apache/noggit/TestJSONParser.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org