@Override public void serialize(final Object obj, final StringBuilder buf) { CodeWScope c = (CodeWScope) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$code", c.getCode()); temp.put("$scope", c.getScope()); serializer.serialize(temp, buf); }
@SuppressWarnings("unchecked") private void encodeCodeWScope(final BsonWriter bsonWriter, final CodeWScope value) { bsonWriter.writeJavaScriptWithScope(value.getCode()); encodeBsonObject(bsonWriter, value.getScope()); }
/** * Encodes a field to a {@link org.bson.BsonType#JAVASCRIPT_WITH_SCOPE} value. * * @param name the field name * @param codeWScope the value */ protected void putCodeWScope(final String name, final CodeWScope codeWScope) { putName(name); bsonWriter.writeJavaScriptWithScope(codeWScope.getCode()); putObject(codeWScope.getScope()); }
@Override protected void serialize(Object obj, Writer writer, OutputStream out) throws IOException { CodeWScope c = (CodeWScope) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$code", c.getCode()); temp.put("$scope", c.getScope()); JSON.serialize(serializer, temp, writer, out); } }
@Override public void serialize(final Object obj, final StringBuilder buf) { CodeWScope c = (CodeWScope) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$code", c.getCode()); temp.put("$scope", c.getScope()); serializer.serialize(temp, buf); }
private void pickleCode(final Code code, final OutputStream out, final Pickler pickler) throws IOException { out.write(Opcodes.GLOBAL); out.write("bson.code\nCode\n".getBytes()); pickler.save(code.getCode()); out.write(Opcodes.TUPLE1); out.write(Opcodes.NEWOBJ); // PyMongo's bson.code.Code always has a scope, even it if is empty. out.write(Opcodes.EMPTY_DICT); putBinstring("_Code__scope", out); if (code instanceof CodeWithScope) { pickler.save(((CodeWithScope) code).getScope()); } else if (code instanceof CodeWScope) { pickler.save(((CodeWScope) code).getScope().toMap()); } else { out.write(Opcodes.EMPTY_DICT); } out.write(Opcodes.SETITEM); out.write(Opcodes.BUILD); }
@SuppressWarnings("unchecked") private void encodeCodeWScope(final BsonWriter bsonWriter, final CodeWScope value) { bsonWriter.writeJavaScriptWithScope(value.getCode()); encodeBsonObject(bsonWriter, value.getScope()); }
public void serialize(Object obj, StringBuilder buf) { CodeWScope c = (CodeWScope) obj; BasicBSONObject temp = new BasicBSONObject(); temp.put("$code", c.getCode()); temp.put("$scope", c.getScope()); serializer.serialize(temp, buf); }
protected void putCodeWScope(String name, CodeWScope code) { _put(CODE_W_SCOPE, name); int temp = _buf.getPosition(); _buf.writeInt(0); _putValueString(code.getCode()); putObject(code.getScope()); _buf.writeInt(temp, _buf.getPosition() - temp); }
protected void putCodeWScope(String name, CodeWScope code) { this._put(15, name); int temp = this._buf.getPosition(); this._buf.writeInt(0); this._putValueString(code.getCode()); this.putObject(code.getScope()); this._buf.writeInt(temp, this._buf.getPosition() - temp); }
/** * 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); }
/** * 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()); }
/** * Tests if {@code JavaScript} objects can be deserialized * @throws Exception if something goes wrong */ @Test public void javascript() throws Exception { Code code = new Code("code"); TC.J obj = generateAndParse(code, TC.J.class); assertEquals(code.getCode(), obj.obj.getCode()); CodeWScope codews = new CodeWScope("code", new BasicBSONObject("key", "value")); obj = generateAndParse(codews, TC.J.class); assertEquals(code.getCode(), obj.obj.getCode()); assertEquals(codews.getScope().toMap(), obj.obj.getScope()); }