use of de.undercouch.bson4jackson.types.JavaScript in project bson4jackson by michel-kraemer.
the class BsonParser method handleJavascriptWithScope.
/**
* Can be called when embedded javascript code with scope is found. Reads
* the code and the embedded document.
* @return the json token read
* @throws IOException if an I/O error occurs
*/
protected JsonToken handleJavascriptWithScope() throws IOException {
// skip size
_in.readInt();
String code = readString();
Map<String, Object> doc = readDocument();
getContext().value = new JavaScript(code, doc);
return JsonToken.VALUE_EMBEDDED_OBJECT;
}
use of de.undercouch.bson4jackson.types.JavaScript in project bson4jackson by michel-kraemer.
the class BsonGeneratorTest method javascript.
/**
* Test if {@link JavaScript} objects can be serialized
* @throws Exception if something goes wrong
*/
@Test
public void javascript() throws Exception {
JavaScript javaScript = new JavaScript("a < 100");
Map<String, Object> data = new LinkedHashMap<String, Object>();
data.put("javaScript", javaScript);
BSONObject obj = generateAndParse(data);
Code result = (Code) obj.get("javaScript");
assertNotNull(result);
assertEquals(javaScript.getCode(), result.getCode());
}
use of de.undercouch.bson4jackson.types.JavaScript in project bson4jackson by michel-kraemer.
the class BsonGeneratorTest method javascriptWithScope.
/**
* Test if {@link JavaScript} objects with a scope can be serialized
* @throws Exception if something goes wrong
*/
@Test
public void javascriptWithScope() throws Exception {
Map<String, Object> scope = new LinkedHashMap<String, Object>();
scope.put("a", 99);
scope.put("b", 80);
JavaScript javaScript = new JavaScript("a < 100", scope);
Map<String, Object> data = new LinkedHashMap<String, Object>();
data.put("javaScript", javaScript);
BSONObject obj = generateAndParse(data);
CodeWScope result = (CodeWScope) obj.get("javaScript");
assertNotNull(result);
assertEquals(javaScript.getCode(), result.getCode());
Map<?, ?> returnedScope = result.getScope().toMap();
assertEquals(returnedScope, scope);
}
use of de.undercouch.bson4jackson.types.JavaScript in project bson4jackson by michel-kraemer.
the class BsonSerializersTest method javascriptWithScope.
/**
* Tests {@link BsonJavaScriptSerializer}
* @throws Exception if something goes wrong
*/
@Test
public void javascriptWithScope() throws Exception {
Map<String, Object> scope = new HashMap<>();
scope.put("j", 5);
JavaScript js = new JavaScript("code", scope);
CodeWScope code = (CodeWScope) generateAndParse(js);
assertEquals(js.getCode(), code.getCode());
assertEquals(js.getScope(), code.getScope());
}
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"));
}
Aggregations