use of de.undercouch.bson4jackson.types.JavaScript in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseCode.
/**
* Test if {@link JavaScript} objects can be deserialized
* @throws Exception if something goes wrong
*/
@Test
public void parseCode() throws Exception {
BSONObject scope = new BasicBSONObject();
scope.put("Int32", 5);
BSONObject o = new BasicBSONObject();
o.put("Code1", new CodeWScope("alert('test');", scope));
o.put("Code2", new Code("alert('Hello');"));
Map<?, ?> data = parseBsonObject(o);
assertEquals(2, data.size());
JavaScript c1 = (JavaScript) data.get("Code1");
JavaScript c2 = (JavaScript) data.get("Code2");
assertEquals("alert('test');", c1.getCode());
assertEquals("alert('Hello');", c2.getCode());
Map<String, Object> c1scope = c1.getScope();
assertEquals(5, c1scope.get("Int32"));
}
use of de.undercouch.bson4jackson.types.JavaScript in project bson4jackson by michel-kraemer.
the class BsonSerializersTest method javascript.
/**
* Tests {@link BsonJavaScriptSerializer}
* @throws Exception if something goes wrong
*/
@Test
public void javascript() throws Exception {
JavaScript js = new JavaScript("code");
Code code = (Code) generateAndParse(js);
assertEquals(js.getCode(), code.getCode());
}
use of de.undercouch.bson4jackson.types.JavaScript in project bson4jackson by michel-kraemer.
the class BsonParser method nextToken.
@Override
@SuppressWarnings("deprecation")
public JsonToken nextToken() throws IOException {
Context ctx = _currentContext;
if (_currToken == null && ctx == null) {
try {
_currToken = handleNewDocument(false);
} catch (EOFException e) {
// there is nothing more to read. indicate EOF
return null;
}
} else {
_tokenPos = _counter.getPosition();
if (ctx == null) {
if (_currToken == JsonToken.END_OBJECT) {
// end of input
return null;
}
throw new JsonParseException("Found element outside the document", getTokenLocation());
}
if (ctx.state == State.DONE) {
// next field
ctx.reset();
}
boolean readValue = true;
if (ctx.state == State.FIELDNAME) {
readValue = false;
while (true) {
// read field name or end of document
ctx.type = _in.readByte();
if (ctx.type == BsonConstants.TYPE_END) {
// end of document
_currToken = ctx.array ? JsonToken.END_ARRAY : JsonToken.END_OBJECT;
_currentContext = _currentContext.parent;
} else if (ctx.type == BsonConstants.TYPE_UNDEFINED) {
// skip field name and then ignore this token
skipCString();
continue;
} else {
ctx.state = State.VALUE;
_currToken = JsonToken.FIELD_NAME;
if (ctx.array) {
// immediately read value of array element (discard field name)
readValue = true;
skipCString();
ctx.fieldName = null;
} else {
// read field name
ctx.fieldName = readCString();
}
}
break;
}
}
if (readValue) {
// parse element's value
switch(ctx.type) {
case BsonConstants.TYPE_DOUBLE:
ctx.value = _in.readDouble();
_currToken = JsonToken.VALUE_NUMBER_FLOAT;
break;
case BsonConstants.TYPE_STRING:
ctx.value = readString();
_currToken = JsonToken.VALUE_STRING;
break;
case BsonConstants.TYPE_DOCUMENT:
_currToken = handleNewDocument(false);
break;
case BsonConstants.TYPE_ARRAY:
_currToken = handleNewDocument(true);
break;
case BsonConstants.TYPE_BINARY:
_currToken = handleBinary();
break;
case BsonConstants.TYPE_OBJECTID:
ctx.value = readObjectId();
_currToken = JsonToken.VALUE_EMBEDDED_OBJECT;
break;
case BsonConstants.TYPE_BOOLEAN:
boolean b = _in.readBoolean();
ctx.value = b;
_currToken = b ? JsonToken.VALUE_TRUE : JsonToken.VALUE_FALSE;
break;
case BsonConstants.TYPE_DATETIME:
ctx.value = new Date(_in.readLong());
_currToken = JsonToken.VALUE_EMBEDDED_OBJECT;
break;
case BsonConstants.TYPE_NULL:
_currToken = JsonToken.VALUE_NULL;
break;
case BsonConstants.TYPE_REGEX:
_currToken = handleRegEx();
break;
case BsonConstants.TYPE_DBPOINTER:
_currToken = handleDBPointer();
break;
case BsonConstants.TYPE_JAVASCRIPT:
ctx.value = new JavaScript(readString());
_currToken = JsonToken.VALUE_EMBEDDED_OBJECT;
break;
case BsonConstants.TYPE_SYMBOL:
ctx.value = readSymbol();
_currToken = JsonToken.VALUE_EMBEDDED_OBJECT;
break;
case BsonConstants.TYPE_JAVASCRIPT_WITH_SCOPE:
_currToken = handleJavascriptWithScope();
break;
case BsonConstants.TYPE_INT32:
ctx.value = _in.readInt();
_currToken = JsonToken.VALUE_NUMBER_INT;
break;
case BsonConstants.TYPE_TIMESTAMP:
ctx.value = readTimestamp();
_currToken = JsonToken.VALUE_EMBEDDED_OBJECT;
break;
case BsonConstants.TYPE_INT64:
ctx.value = _in.readLong();
_currToken = JsonToken.VALUE_NUMBER_INT;
break;
case BsonConstants.TYPE_DECIMAL128:
long low = _in.readLong();
long high = _in.readLong();
ctx.value = Decimal128.fromIEEE754BIDEncoding(high, low);
_currToken = JsonToken.VALUE_EMBEDDED_OBJECT;
break;
case BsonConstants.TYPE_MINKEY:
ctx.value = "MinKey";
_currToken = JsonToken.VALUE_STRING;
break;
case BsonConstants.TYPE_MAXKEY:
ctx.value = "MaxKey";
_currToken = JsonToken.VALUE_STRING;
break;
default:
throw new JsonParseException("Unknown element type " + ctx.type, getTokenLocation());
}
ctx.state = State.DONE;
}
}
return _currToken;
}
Aggregations