Search in sources :

Example 1 with JavaScript

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;
}
Also used : JavaScript(de.undercouch.bson4jackson.types.JavaScript)

Example 2 with JavaScript

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());
}
Also used : JavaScript(de.undercouch.bson4jackson.types.JavaScript) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) Code(org.bson.types.Code) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 3 with JavaScript

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);
}
Also used : JavaScript(de.undercouch.bson4jackson.types.JavaScript) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) LinkedHashMap(java.util.LinkedHashMap) CodeWScope(org.bson.types.CodeWScope) Test(org.junit.Test)

Example 4 with JavaScript

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());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JavaScript(de.undercouch.bson4jackson.types.JavaScript) BSONObject(org.bson.BSONObject) CodeWScope(org.bson.types.CodeWScope) Test(org.junit.Test)

Example 5 with JavaScript

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"));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) JavaScript(de.undercouch.bson4jackson.types.JavaScript) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) Code(org.bson.types.Code) CodeWScope(org.bson.types.CodeWScope) Test(org.junit.Test)

Aggregations

JavaScript (de.undercouch.bson4jackson.types.JavaScript)8 Test (org.junit.Test)5 BSONObject (org.bson.BSONObject)4 LinkedHashMap (java.util.LinkedHashMap)3 Code (org.bson.types.Code)3 CodeWScope (org.bson.types.CodeWScope)3 SerializableString (com.fasterxml.jackson.core.SerializableString)2 SerializedString (com.fasterxml.jackson.core.io.SerializedString)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 TreeNode (com.fasterxml.jackson.core.TreeNode)1 IOContext (com.fasterxml.jackson.core.io.IOContext)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)1 BsonParser (de.undercouch.bson4jackson.BsonParser)1 EOFException (java.io.EOFException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 BasicBSONObject (org.bson.BasicBSONObject)1