Stack<Object> stack = new Stack<Object>();
Object obj = null;
boolean inArray = false;
if( ev != JSONParser.OBJECT_START ) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "object should already be started" );
}
while( true ) {
//System.out.println( ev + "["+JSONParser.getEventString(ev)+"] "+parser.wasKey() ); //+ parser.getString() );
switch (ev) {
case JSONParser.STRING:
if( parser.wasKey() ) {
obj = stack.peek();
String v = parser.getString();
if( obj instanceof SolrInputField ) {
SolrInputField field = (SolrInputField)obj;
if( "boost".equals( v ) ) {
ev = parser.nextEvent();
if( ev != JSONParser.NUMBER &&
ev != JSONParser.LONG &&
ev != JSONParser.BIGNUMBER ) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "boost should have number! "+JSONParser.getEventString(ev) );
}
field.setBoost((float)parser.getDouble());
}
else if( "value".equals( v ) ) {
// nothing special...
stack.push( field ); // so it can be popped
}
else {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "invalid key: "+v + " ["+ parser.getPosition()+"]" );
}
}
else if( obj instanceof SolrInputDocument ) {
SolrInputDocument doc = (SolrInputDocument)obj;
SolrInputField f = doc.get( v );
if( f == null ) {
f = new SolrInputField( v );
doc.put( f.getName(), f );
}
stack.push( f );
}
else {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "hymmm ["+ parser.getPosition()+"]" );
}
}
else {
addValToField(stack, parser.getString(), inArray, parser);
}
break;
case JSONParser.LONG:
case JSONParser.NUMBER:
case JSONParser.BIGNUMBER:
addValToField(stack, parser.getNumberChars().toString(), inArray, parser);
break;
case JSONParser.BOOLEAN:
addValToField(stack, parser.getBoolean(),inArray, parser);
break;
case JSONParser.NULL:
parser.getNull();
/*** if we wanted to remove the field from the document now...
if (!inArray) {
Object o = stack.peek();
// if null was only value in the field, then remove the field
if (o instanceof SolrInputField) {
SolrInputField sif = (SolrInputField)o;
if (sif.getValueCount() == 0) {
sdoc.remove(sif.getName());
}
}
}
***/
addValToField(stack, null, inArray, parser);
break;
case JSONParser.OBJECT_START:
if( stack.isEmpty() ) {
stack.push( new SolrInputDocument() );
}
else {
obj = stack.peek();
if( obj instanceof SolrInputField ) {
// should alreay be pushed...
}
else {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "should not start new object with: "+obj + " ["+ parser.getPosition()+"]" );
}
}
break;
case JSONParser.OBJECT_END:
obj = stack.pop();
if( obj instanceof SolrInputDocument ) {
return (SolrInputDocument)obj;
}
else if( obj instanceof SolrInputField ) {
// should already be pushed...
}
else {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "should not start new object with: "+obj + " ["+ parser.getPosition()+"]" );
}
break;
case JSONParser.ARRAY_START:
inArray = true;
break;
case JSONParser.ARRAY_END:
inArray = false;
stack.pop(); // the val should have done it...
break;
default:
System.out.println("UNKNOWN_EVENT_ID:"+ev);
break;
}
ev = parser.nextEvent();
if( ev == JSONParser.EOF ) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "should finish doc first!" );
}
}
}