@Override public void encode(final BsonWriter writer, final Symbol value, final EncoderContext encoderContext) { writer.writeSymbol(value.getSymbol()); }
@Override public Symbol decode(final BsonReader reader, final DecoderContext decoderContext) { return new Symbol(reader.readSymbol()); }
@Override public void serialize(final Object obj, final StringBuilder buf) { Symbol symbol = (Symbol) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$symbol", symbol.toString()); serializer.serialize(temp, buf); } }
/** * Tests if {@code Symbol} objects can be deserialized * @throws Exception if something goes wrong */ @Test public void symbol() throws Exception { org.bson.types.Symbol sym = new org.bson.types.Symbol("symbol"); TC.S obj = generateAndParse(sym, TC.S.class); assertEquals(sym.getSymbol(), obj.obj.getSymbol()); }
/** * Encodes a Symbol field * * @param name the field name * @param symbol the value * @see org.bson.BsonType#SYMBOL */ protected void putSymbol(final String name, final Symbol symbol) { putName(name); bsonWriter.writeSymbol(symbol.getSymbol()); }
return new Code(reader.readJavaScript()); case SYMBOL: return new Symbol(reader.readSymbol()); case JAVASCRIPT_WITH_SCOPE: return new CodeWScope(reader.readJavaScriptWithScope(), (BSONObject) readJavaScriptWithScopeDocument(reader));
@Override public void serialize(final Object obj, final StringBuilder buf) { Symbol symbol = (Symbol) obj; BasicDBObject temp = new BasicDBObject(); temp.put("$symbol", symbol.toString()); serializer.serialize(temp, buf); } }
@SuppressWarnings("unchecked") private void writeValue(final BsonWriter bsonWriter, final EncoderContext encoderContext, final Object initialValue) { Object value = org.bson.BSON.applyEncodingHooks(initialValue); if (value == null) { bsonWriter.writeNull(); } else if (value instanceof DBRef) { encodeDBRef(bsonWriter, (DBRef) value); } else if (value instanceof Map) { encodeMap(bsonWriter, (Map<String, Object>) value); } else if (value instanceof Iterable) { encodeIterable(bsonWriter, (Iterable) value); } else if (value instanceof BSONObject) { encodeBsonObject(bsonWriter, ((BSONObject) value)); } else if (value instanceof CodeWScope) { encodeCodeWScope(bsonWriter, (CodeWScope) value); } else if (value instanceof byte[]) { encodeByteArray(bsonWriter, (byte[]) value); } else if (value.getClass().isArray()) { encodeArray(bsonWriter, value); } else if (value instanceof Symbol) { bsonWriter.writeSymbol(((Symbol) value).getSymbol()); } else { Codec codec = codecRegistry.get(value.getClass()); codec.encode(bsonWriter, value, encoderContext); } }
/** * Test if a complex BSON object containing various values can be * deserialized * @throws Exception if something goes wrong */ @Test public void parseComplex() throws Exception { BSONObject o = new BasicBSONObject(); o.put("Timestamp", new BSONTimestamp(0xAABB, 0xCCDD)); o.put("Symbol", new Symbol("Test")); o.put("ObjectId", org.bson.types.ObjectId.createFromLegacyFormat( Integer.MAX_VALUE, -2, Integer.MIN_VALUE)); Pattern p = Pattern.compile(".*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE | Pattern.UNICODE_CASE); o.put("Regex", p); Map<?, ?> data = parseBsonObject(o); assertEquals(new Timestamp(0xAABB, 0xCCDD), data.get("Timestamp")); assertEquals(new de.undercouch.bson4jackson.types.Symbol("Test"), data.get("Symbol")); ObjectId oid = (ObjectId)data.get("ObjectId"); assertEquals(Integer.MAX_VALUE, oid.getTime()); assertEquals(-2, oid.getMachine()); assertEquals(Integer.MIN_VALUE, oid.getInc()); Pattern p2 = (Pattern)data.get("Regex"); assertEquals(p.flags(), p2.flags()); assertEquals(p.pattern(), p2.pattern()); }
protected void putSymbol(String name, Symbol symbol) { this._putString(name, symbol.getSymbol(), 14); }
} else if (value instanceof Symbol) { Symbol symbol = (Symbol)value; value = new org.bson.types.Symbol(symbol.getSymbol()); } else if (value instanceof ObjectId) { ObjectId id = (ObjectId)value;
protected void putSymbol(String name, Symbol s) { _putString(name, s.getSymbol(), SYMBOL); }
@Override public void serialize(Object obj, StringBuilder buf) { Symbol s = (Symbol) obj; BasicBSONObject temp = new BasicBSONObject(); temp.put("$symbol", s.getSymbol()); serializer.serialize(temp, buf); }
return ((Symbol) value).getSymbol();
@SuppressWarnings("unchecked") private void writeValue(final BsonWriter bsonWriter, final EncoderContext encoderContext, final Object initialValue) { Object value = org.bson.BSON.applyEncodingHooks(initialValue); if (value == null) { bsonWriter.writeNull(); } else if (value instanceof DBRef) { encodeDBRef(bsonWriter, (DBRef) value); } else if (value instanceof Map) { encodeMap(bsonWriter, (Map<String, Object>) value); } else if (value instanceof Iterable) { encodeIterable(bsonWriter, (Iterable) value); } else if (value instanceof BSONObject) { encodeBsonObject(bsonWriter, ((BSONObject) value)); } else if (value instanceof CodeWScope) { encodeCodeWScope(bsonWriter, (CodeWScope) value); } else if (value instanceof byte[]) { encodeByteArray(bsonWriter, (byte[]) value); } else if (value.getClass().isArray()) { encodeArray(bsonWriter, value); } else if (value instanceof Symbol) { bsonWriter.writeSymbol(((Symbol) value).getSymbol()); } else { Codec codec = codecRegistry.get(value.getClass()); codec.encode(bsonWriter, value, encoderContext); } }