package me.doubledutch.lazyjson;

import java.nio.ByteBuffer;

public class LazyElement{
	protected LazyNode root;
	protected char[] cbuf;

	// Cache value for length
	private int length=-1;

	protected LazyElement(LazyNode root,char[] source){
		this.root=root;
		this.cbuf=source;
	}

	protected LazyElement() throws LazyException{

	}

	protected char[] getCharBuffer(){
		return cbuf;
	}

	/**
	 * Returns the number of fields on this object
	 *
	 * @return the number of fields
	 */
	public int length(){
		if(root.child==null){
			return 0;
		}
		if(length>-1){
			return length;
		}
		length=root.getChildCount();
		return length;
	}

	/**
	 * Returns a raw string extracted from the source string that covers the
	 * start and end index of this object.
	 *
	 * @return as string representation of this object as given in the source string
	 */
	public String toString(){
		return new String(cbuf,root.startIndex,root.endIndex-root.startIndex);
	}

	public byte[] toByteArray(){
		int size=root.getBufferSize();
		ByteBuffer buf=ByteBuffer.allocate(size);
		root.writeToBuffer(buf);
		return buf.array();
	}
}
@@ -382,24 +382,420 @@
 n cbuf;%0A%09%7D%0A%0A
+%09/**%0A%09 * Parses a string and returns either a LazyObject or LazyArray%0A%09 *%0A%09 * @param str the source json data%0A%09 * @return either a LazyObject or LazyArray instance%0A%09 * @throws LazyException if the string could not be parsed%0A%09 */%0A%09public static Object parse(String str) throws LazyException%7B%0A%09%09if(str.startsWith(%22%5B%22))%7B%0A%09%09%09return new LazyArray(str);%0A%09%09%7Delse%7B%0A%09%09%09return new LazyObject(str);%0A%09%09%7D%0A%09%7D%0A%0A
 %09/**%0A%09 * Ret

Add generic static parse method