@Override public void encode(final BsonWriter writer, final CodeWithScope codeWithScope, final EncoderContext encoderContext) { writer.writeJavaScriptWithScope(codeWithScope.getCode()); documentCodec.encode(writer, codeWithScope.getScope(), encoderContext); }
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); }