@Override public void encode(final BsonWriter writer, final Code value, final EncoderContext encoderContext) { writer.writeJavaScript(value.getCode()); }
@Override public void serialize(final Object obj, final StringBuilder buf) { Code c = (Code) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$code", c.getCode()); serializer.serialize(temp, buf); }
/** * Encodes a field to a {@link org.bson.BsonType#JAVASCRIPT} value. * * @param name the field name * @param code the value */ protected void putCode(final String name, final Code code) { putName(name); bsonWriter.writeJavaScript(code.getCode()); }
@Override public NamedMongoScript convert(Document source) { if (source == null) { return null; } String id = source.get("_id").toString(); Object rawValue = source.get("value"); return new NamedMongoScript(id, ((Code) rawValue).getCode()); } }
@Override public NamedMongoScript convert(Document source) { if (source == null) { return null; } String id = source.get("_id").toString(); Object rawValue = source.get("value"); return new NamedMongoScript(id, ((Code) rawValue).getCode()); } }
@Override public String toString() { return getCode(); }
@Override public void serialize(final Object obj, final StringBuilder buf) { Code c = (Code) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$code", c.getCode()); serializer.serialize(temp, buf); }
@Override protected void serialize(Object obj, Writer writer, OutputStream out) throws IOException { Code c = (Code) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$code", c.getCode()); JSON.serialize(serializer, temp, writer, out); } }
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); }
protected void putCode(String name, Code code) { _put(CODE, name); int temp = _buf.getPosition(); _putValueString(code.getCode()); }
public void serialize(Object obj, StringBuilder buf) { Code c = (Code) obj; BasicBSONObject temp = new BasicBSONObject(); temp.put("$code", c.getCode()); serializer.serialize(temp, buf); }
protected void putCode(String name, Code code) { this._put(13, name); int temp = this._buf.getPosition(); this._putValueString(code.getCode()); }
return ((Code) value).getCode();
InstructionList instructions = new InstructionList(code.getCode()); for (Instruction instruction : instructions.getInstructions()) {
/** * 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()); }
/** * 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()); }
/** * 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()); }