public static BsonDocument getBsonDocument() { BsonDocument bsonObj = new BsonDocument().append("testDouble", new BsonDouble(20.777)); List<BsonDocument> list = new ArrayList<BsonDocument>(); list.add(bsonObj); list.add(bsonObj); byte[] bytes = new byte[3]; bytes[0] = 3; bytes[1] = 2; bytes[2] = 1; BsonDocument bsonDocument = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("testArray", new BsonArray(list)); return new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("testArray", new BsonArray(list)) .append("bson_test", bsonDocument) .append("testBinary", new BsonBinary(bytes)) .append("testBsonUndefined", new BsonUndefined()) .append("testObjectId", new BsonObjectId()) .append("testStringObjectId", new BsonObjectId()) .append("testBoolean", new BsonBoolean(true)) .append("testDate", new BsonDateTime(time)) .append("testNull", new BsonNull()) .append("testInt", new BsonInt32(233)) .append("testLong", new BsonInt64(233332)); }
@Test public void testBsonDocumentDeSerialize() { BsonDocument document = new BsonDocument().append("a", new BsonString("MongoDB")) .append("b", new BsonArray(Arrays.asList(new BsonInt32(1), new BsonInt32(2)))) .append("c", new BsonBoolean(true)) .append("d", new BsonDateTime(0)); String json = oson.useAttribute(false).setValueOnly(true).serialize(document); String expected = "{\"a\":\"MongoDB\",\"b\":[1,2],\"c\":true,\"d\":0}"; assertEquals(expected, json); BsonDocument document2 = oson.deserialize(json, BsonDocument.class); assertEquals(expected, oson.serialize(document2)); }
private static BsonValue converseType(String value) { String[] valArr = value.split("\\^"); if (valArr.length != 2) { return new BsonString(value); } try { String type = valArr[1]; if (type.equals("int")) { return new BsonInt32(Integer.parseInt(valArr[0])); } else if (type.equals("long")) { return new BsonInt64(Long.parseLong(valArr[0])); } else if (type.equals("double")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("boolean")) { return new BsonBoolean(Boolean.parseBoolean(valArr[0])); } else { return new BsonString(value); } } catch (NumberFormatException e) { return new BsonString(value); } }
static BsonDocument getExistsQueryObject(String[] fieldArr, String str, BsonBoolean isExist) { BsonArray conjQueries = new BsonArray(); for (String field : fieldArr) { BsonDocument query = new BsonDocument(); if (str != null) { str = encodeMongoObjectKey(str); query.put(field + "." + str, new BsonDocument("$exists", isExist)); } else { query.put(field, new BsonDocument("$exists", isExist)); } conjQueries.add(query); } if (conjQueries.size() != 0) { BsonDocument queryObject = new BsonDocument(); if (isExist.equals(BsonBoolean.TRUE)) queryObject.put("$or", conjQueries); else { queryObject.put("$and", conjQueries); } return queryObject; } else { return null; } }
public static BsonDocument asBsonDocument(SubscriptionType subscription) { BsonDocument bson = new BsonDocument(); if (subscription.getSubscriptionID() != null) { bson.put("subscriptionID", new BsonString(subscription.getSubscriptionID())); } if (subscription.getDest() != null) { bson.put("dest", new BsonString(subscription.getDest())); } if (subscription.getSchedule() != null) { bson.put("schedule", new BsonString(subscription.getSchedule())); } if (subscription.getTrigger() != null) { bson.put("trigger", new BsonString(subscription.getTrigger())); } if (subscription.getInitialRecordTime() != null) { bson.put("initialRecordTime", new BsonString(subscription.getInitialRecordTime())); } bson.put("reportIfEmpty", new BsonBoolean(subscription.getReportIfEmpty())); bson.put("pollParameters", PollParameters.asBsonDocument(subscription.getPollParameters())); return bson; }
static BsonValue converseType(String value) { String[] valArr = value.split("\\^"); if (valArr.length != 2) { return new BsonString(value); } try { String type = valArr[1]; if (type.equals("int")) { return new BsonInt32(Integer.parseInt(valArr[0])); } else if (type.equals("long")) { return new BsonInt64(Long.parseLong(valArr[0])); } else if (type.equals("double")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("boolean")) { return new BsonBoolean(Boolean.parseBoolean(valArr[0])); } else { return new BsonString(value); } } catch (NumberFormatException e) { return new BsonString(value); } }
/** * @param timestamp * @param projection * @return TimestampProperties BsonDocument only containing projection keys */ public BsonDocument getTimestampProperties(final Long timestamp, final String[] projection) { BsonDocument bsonProjection = new BsonDocument(); if (projection != null) { for (String string : projection) { bsonProjection.append(string, new BsonBoolean(true)); } } if (this instanceof ChronoVertex) return graph.getVertexEvents() .find(new BsonDocument(Tokens.VERTEX, new BsonString(this.id)).append(Tokens.TIMESTAMP, new BsonDateTime(timestamp))) .projection(bsonProjection).first(); else return graph.getEdgeCollection() .find(new BsonDocument(Tokens.ID, new BsonString(this.id + "-" + timestamp))) .projection(bsonProjection).first(); }
@Test public void deserialise_boolean() { Key<Boolean> booleanKey = Key.named("my-value"); BsonRecordDeserialiser deserialiser = BsonRecordDeserialiser.builder() .readBoolean(booleanKey) .get(); BsonDocument trueDoc = new BsonDocument(); trueDoc.put("my-value", new BsonBoolean(true)); Record trueRecord = deserialiser.apply(trueDoc); assertTrue("wasn't true", booleanKey.get(trueRecord).get()); BsonDocument falseDoc = new BsonDocument(); falseDoc.put("my-value", new BsonBoolean(false)); Record falseRecord = Record.of(booleanKey.of(false)); assertFalse("wasn't false", booleanKey.get(falseRecord).get()); }
/** * Reading from BSON to GSON */ @Test public void bsonToGson() throws Exception { BsonDocument document = new BsonDocument(); document.append("boolean", new BsonBoolean(true)); document.append("int32", new BsonInt32(32)); document.append("int64", new BsonInt64(64)); document.append("double", new BsonDouble(42.42D)); document.append("string", new BsonString("foo")); document.append("null", new BsonNull()); document.append("array", new BsonArray()); document.append("object", new BsonDocument()); JsonElement element = TypeAdapters.JSON_ELEMENT.read(new BsonReader(new BsonDocumentReader(document))); check(element.isJsonObject()); check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().isBoolean()); check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().getAsBoolean()); check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().getAsNumber().intValue()).is(32); check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().getAsNumber().longValue()).is(64L); check(element.getAsJsonObject().get("double").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("double").getAsJsonPrimitive().getAsNumber().doubleValue()).is(42.42D); check(element.getAsJsonObject().get("string").getAsJsonPrimitive().isString()); check(element.getAsJsonObject().get("string").getAsJsonPrimitive().getAsString()).is("foo"); check(element.getAsJsonObject().get("null").isJsonNull()); check(element.getAsJsonObject().get("array").isJsonArray()); check(element.getAsJsonObject().get("object").isJsonObject()); }
private Boolean getBooleanFromInner(Object key) { BsonBoolean bsonBoolean = innerBsonDocument.getBoolean(key); if (bsonBoolean != null) { return (Boolean) BsonValueConverterRepertory.getValueConverterByBsonType(bsonBoolean.getBsonType()).decode(bsonBoolean); } return null; }
@Test public void testBooleanType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("booleanKey", new BsonBoolean(true)); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertTrue(mapReader.reader("booleanKey").readBoolean()); }
public static BsonValue converseType(String value) { String[] valArr = value.split("\\^"); if (valArr.length != 2) { return new BsonString(value); } try { String type = valArr[1]; if (type.equals("int")) { return new BsonInt32(Integer.parseInt(valArr[0])); } else if (type.equals("long")) { return new BsonInt64(Long.parseLong(valArr[0])); } else if (type.equals("double")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("boolean")) { return new BsonBoolean(Boolean.parseBoolean(valArr[0])); } else if (type.equals("float")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("dateTime")) { BsonDateTime time = getBsonDateTime(valArr[0]); if (time != null) return time; return new BsonString(value); } else if (type.equals("geoPoint")) { BsonDocument point = getBsonGeoPoint(valArr[0]); if (point == null) return new BsonString(value); return point; } else if (type.equals("geoArea")) { BsonDocument area = getBsonGeoArea(valArr[0]); if (area == null) return new BsonString(value); return area; } else { return new BsonString(value); } } catch (NumberFormatException e) { return new BsonString(value); } }
static BsonValue converseType(String value) { String[] valArr = value.split("\\^"); if (valArr.length != 2) { return new BsonString(value); } try { String type = valArr[1].trim(); if (type.equals("int")) { return new BsonInt32(Integer.parseInt(valArr[0])); } else if (type.equals("long")) { return new BsonInt64(Long.parseLong(valArr[0])); } else if (type.equals("double")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("boolean")) { return new BsonBoolean(Boolean.parseBoolean(valArr[0])); } else if (type.equals("regex")) { return new BsonRegularExpression("^" + valArr[0] + "$"); } else if (type.equals("float")) { return new BsonDouble(Double.parseDouble(valArr[0])); } else if (type.equals("dateTime")) { BsonDateTime time = MongoQueryService.getTimeMillis(valArr[0]); if (time != null) return time; return new BsonString(value); } else { return new BsonString(value); } } catch (NumberFormatException e) { return new BsonString(value); } }
static BsonDocument getExistsQueryObject(String field, String str, BsonBoolean isExist) { BsonDocument query = new BsonDocument(); if (str != null) { str = encodeMongoObjectKey(str); query.put(field + "." + str, new BsonDocument("$exists", isExist)); } else { query.put(field, new BsonDocument("$exists", isExist)); } return query; }
private Map<String, BsonValue> getExtensionMap(List<ECReportMemberField> fields) { Map<String, BsonValue> extMap = new HashMap<String, BsonValue>(); for (int l = 0; l < fields.size(); l++) { ECReportMemberField field = fields.get(l); String key = field.getName(); String value = field.getValue(); String[] valArr = value.split("\\^"); if (valArr.length != 2) { extMap.put(key, new BsonString(value)); continue; } try { String type = valArr[1]; if (type.equals("int")) { extMap.put(key, new BsonInt32(Integer.parseInt(valArr[0]))); } else if (type.equals("long")) { extMap.put(key, new BsonInt64(Long.parseLong(valArr[0]))); } else if (type.equals("double")) { extMap.put(key, new BsonDouble(Double.parseDouble(valArr[0]))); } else if (type.equals("boolean")) { extMap.put(key, new BsonBoolean(Boolean.parseBoolean(valArr[0]))); } else if (type.equals("dateTime")) { extMap.put(key, new BsonDateTime(Long.parseLong(valArr[0]))); } else { extMap.put(key, new BsonString(valArr[0])); } } catch (NumberFormatException e) { extMap.put(key, new BsonString(valArr[0])); } } return extMap; }
@SuppressWarnings("unused") private BsonDocument makeOtherwiseIdenticalFilter(BsonDocument filter) { filter.remove("errorDeclaration"); filter.put("errorDeclaration", new BsonDocument("$exists", new BsonBoolean(false))); filter.remove("recordTime"); return filter; }
/** * Return the object value associated with the provided string key. If no value * exists for that key, return null. * * @param key * the key of the key/value property, Tokens.ID, Tokens.LABEL, * Tokens.OUT_VERTEX, Tokens.IN_VERTEX included * @return the object value related to the string key */ @Override public <T> T getProperty(final String key) { BsonDocument doc = null; if (this instanceof ChronoVertex) { doc = graph.getVertexCollection().find(new BsonDocument(Tokens.ID, new BsonString(this.id))) .projection(new BsonDocument(key, new BsonBoolean(true))).first(); } else { doc = graph.getEdgeCollection().find(new BsonDocument(Tokens.ID, new BsonString(this.id))) .projection(new BsonDocument(key, new BsonBoolean(true))).first(); } if (doc != null) return (T) doc.get(key); return null; }
public Set<VertexEvent> getOutVertexEventSet(final String label, final AC tt) { // db.edges.createIndex({"_outV" : 1, "_label" : 1, "_t" : 1, "_inV" : 1}) BsonDocument query = new BsonDocument(Tokens.OUT_VERTEX, new BsonString(vertex.toString())); query.append(Tokens.LABEL, new BsonString(label)); query.append(Tokens.TIMESTAMP, new BsonDocument(tt.toString(), new BsonDateTime(timestamp))); BsonDocument proj = new BsonDocument(Tokens.TIMESTAMP, new BsonBoolean(true)) .append(Tokens.IN_VERTEX, new BsonBoolean(true)).append(Tokens.ID, new BsonBoolean(false)); HashSet<VertexEvent> ret = new HashSet<VertexEvent>(); Iterator<BsonDocument> x = vertex.graph.getEdgeEvents().find(query).projection(proj).iterator(); HashMap<String, Long> map = new HashMap<String, Long>(); while (x.hasNext()) { BsonDocument d = x.next(); String inV = d.getString(Tokens.IN_VERTEX).getValue(); Long t = d.getDateTime(Tokens.TIMESTAMP).getValue(); if (map.containsKey(inV)) { // TODO: if (map.get(inV) > t) map.put(inV, t); } else map.put(inV, t); } map.entrySet().parallelStream().forEach(entry -> { VertexEvent ve = new VertexEvent(graph, entry.getKey() + "-" + entry.getValue()); ret.add(ve); }); return ret; }
public Set<VertexEvent> getInVertexEventSet(final String label, final AC tt) { // db.edges.createIndex({"_outV" : 1, "_label" : 1, "_t" : 1, "_inV" : 1}) BsonDocument query = new BsonDocument(Tokens.IN_VERTEX, new BsonString(vertex.toString())); query.append(Tokens.LABEL, new BsonString(label)); query.append(Tokens.TIMESTAMP, new BsonDocument(tt.toString(), new BsonDateTime(timestamp))); BsonDocument proj = new BsonDocument(Tokens.TIMESTAMP, new BsonBoolean(true)) .append(Tokens.OUT_VERTEX, new BsonBoolean(true)).append(Tokens.ID, new BsonBoolean(false)); HashSet<VertexEvent> ret = new HashSet<VertexEvent>(); Iterator<BsonDocument> x = vertex.graph.getEdgeCollection().find(query).projection(proj).iterator(); HashMap<String, Long> map = new HashMap<String, Long>(); while (x.hasNext()) { BsonDocument d = x.next(); String outV = d.getString(Tokens.OUT_VERTEX).getValue(); Long t = d.getDateTime(Tokens.TIMESTAMP).getValue(); if (map.containsKey(outV)) { // TODO: if (map.get(outV) > t) map.put(outV, t); } else map.put(outV, t); } map.entrySet().parallelStream().forEach(entry -> { VertexEvent ve = new VertexEvent(graph, entry.getKey() + "-" + entry.getValue()); ret.add(ve); }); return ret; }
public Set<VertexEvent> getOutVertexEventSet(final AC tt) { while (true) { try { // db.tEdgeEvents.aggregate([{$match:{"_o":"1","_t":{ $lt : ISODate(0) // }}},{$project:{"_i":1,"_t":1,"_id":0}},{$group:{"_id":"$_i", "_mt": {$min: // "$_t"}}}]) BsonDocument match = new BsonDocument("$match", new BsonDocument(Tokens.OUT_VERTEX, new BsonString(vertex.toString())).append(Tokens.TIMESTAMP, new BsonDocument("$gt", new BsonDateTime(timestamp)))); BsonDocument project = new BsonDocument("$project", new BsonDocument(Tokens.IN_VERTEX, new BsonBoolean(true)) .append(Tokens.TIMESTAMP, new BsonBoolean(true)) .append(Tokens.ID, new BsonBoolean(false))); BsonDocument group = new BsonDocument("$group", new BsonDocument(Tokens.ID, new BsonString("$" + Tokens.IN_VERTEX)).append(Tokens.TIMESTAMP, new BsonDocument("$min", new BsonString("$" + Tokens.TIMESTAMP)))); ArrayList<BsonDocument> aggregateQuery = new ArrayList<BsonDocument>(); aggregateQuery.add(match); aggregateQuery.add(project); aggregateQuery.add(group); HashSet<VertexEvent> ret = new HashSet<VertexEvent>(); Function<BsonDocument, VertexEvent> mapper = new Function<BsonDocument, VertexEvent>() { @Override public VertexEvent apply(BsonDocument d) { String inV = d.getString(Tokens.ID).getValue(); Long t = d.getDateTime(Tokens.TIMESTAMP).getValue(); return new VertexEvent(graph, new ChronoVertex(inV, graph), t); } }; vertex.graph.getEdgeEvents().aggregate(aggregateQuery).map(mapper).into(ret); return ret; } catch (MongoCursorNotFoundException e1) { System.out.println(e1.getErrorMessage()); } } }
private static BsonBoolean getIdAsBoolean(String id) throws IllegalArgumentException { if (id.equals(RequestContext.TRUE_KEY_ID)) { return new BsonBoolean(true); } if (id.equals(RequestContext.FALSE_KEY_ID)) { return new BsonBoolean(false); } return null; }
/** * * @param ref * @param href * @param templated */ public Link(String ref, String href, boolean templated) { this(ref, href); if (templated) { doc.getDocument(ref).put("templated", new BsonBoolean(true)); } }
@Override public void accept(ObjectMapper mapper) { SimpleModule module = new SimpleModule(); addSerializer(module, BsonBoolean.class, (value, gen) -> { gen.writeBoolean(value.getValue()); }); addSerializer(module, BsonDateTime.class, (value, gen) -> { if (Config.USE_TIMESTAMPS) { gen.writeString(DataConverterRegistry.convert(String.class, new Date(value.getValue()))); } else { gen.writeNumber(value.getValue()); } }); addSerializer(module, BsonDouble.class, (value, gen) -> { gen.writeNumber(value.getValue()); }); addSerializer(module, BsonInt32.class, (value, gen) -> { gen.writeNumber(value.getValue()); }); addSerializer(module, BsonInt64.class, (value, gen) -> { gen.writeNumber(value.getValue()); }); addSerializer(module, BsonNull.class, (value, gen) -> { gen.writeNull(); }); addSerializer(module, BsonRegularExpression.class, (value, gen) -> { gen.writeString(value.getPattern()); }); addSerializer(module, BsonString.class, (value, gen) -> { gen.writeString(value.getValue()); }); addSerializer(module, BsonTimestamp.class, (value, gen) -> { if (Config.USE_TIMESTAMPS) { gen.writeString(DataConverterRegistry.convert(String.class, new Date(value.getTime() * 1000L))); } else { gen.writeNumber(value.getTime()); } }); addSerializer(module, BsonUndefined.class, (value, gen) -> { gen.writeNull(); }); addSerializer(module, Binary.class, (value, gen) -> { gen.writeString(BASE64.encode(value.getData())); }); addSerializer(module, Code.class, (value, gen) -> { gen.writeString(value.getCode()); }); addSerializer(module, Decimal128.class, (value, gen) -> { gen.writeNumber(value.bigDecimalValue()); }); addSerializer(module, ObjectId.class, (value, gen) -> { gen.writeString(value.toHexString()); }); addSerializer(module, Symbol.class, (value, gen) -> { gen.writeString(value.getSymbol()); }); mapper.registerModule(module); }
@Override public void accept(ExtensibleRepresenter representer) { addSerializer(representer, BsonBoolean.class, (value) -> { return Boolean.toString(value.getValue()); }); addSerializer(representer, BsonDateTime.class, (value) -> { if (Config.USE_TIMESTAMPS) { return DataConverterRegistry.convert(String.class, new Date(value.getValue())); } return Long.toString(value.getValue()); }); addSerializer(representer, BsonDouble.class, (value) -> { return Double.toString(value.getValue()); }); addSerializer(representer, BsonInt32.class, (value) -> { return Integer.toString(value.getValue()); }); addSerializer(representer, BsonInt64.class, (value) -> { return Long.toString(value.getValue()); }); addSerializer(representer, BsonNull.class, (value) -> { return null; }); addSerializer(representer, BsonRegularExpression.class, (value) -> { return value.getPattern(); }); addSerializer(representer, BsonString.class, (value) -> { return value.getValue(); }); addSerializer(representer, BsonTimestamp.class, (value) -> { if (Config.USE_TIMESTAMPS) { return DataConverterRegistry.convert(String.class, new Date(value.getTime() * 1000L)); } return Integer.toString(value.getTime()); }); addSerializer(representer, BsonUndefined.class, (value) -> { return null; }); addSerializer(representer, Binary.class, (value) -> { return BASE64.encode(value.getData()); }); addSerializer(representer, Code.class, (value) -> { return value.getCode(); }); addSerializer(representer, Decimal128.class, (value) -> { return value.bigDecimalValue().toPlainString(); }); addSerializer(representer, ObjectId.class, (value) -> { return value.toHexString(); }); addSerializer(representer, Symbol.class, (value) -> { return value.getSymbol(); }); }
@Override public void accept(MapperBuilder builder) { addSerializer(builder, BsonBoolean.class, Boolean.class, (value) -> { return value.getValue(); }); if (Config.USE_TIMESTAMPS) { addSerializer(builder, BsonDateTime.class, String.class, (value) -> { return DataConverterRegistry.convert(String.class, new Date(value.getValue())); }); } else { addSerializer(builder, BsonDateTime.class, Long.class, (value) -> { return value.getValue(); }); } addSerializer(builder, BsonDouble.class, Double.class, (value) -> { return value.getValue(); }); addSerializer(builder, BsonInt32.class, Integer.class, (value) -> { return value.getValue(); }); addSerializer(builder, BsonInt64.class, Long.class, (value) -> { return value.getValue(); }); addSerializer(builder, BsonNull.class, Object.class, (value) -> { // Johnzon fails from null values return "null"; }); addSerializer(builder, BsonRegularExpression.class, String.class, (value) -> { return value.getPattern(); }); addSerializer(builder, BsonString.class, String.class, (value) -> { return value.getValue(); }); if (Config.USE_TIMESTAMPS) { addSerializer(builder, BsonTimestamp.class, String.class, (value) -> { return DataConverterRegistry.convert(String.class, new Date(value.getTime() * 1000L)); }); } else { addSerializer(builder, BsonTimestamp.class, Integer.class, (value) -> { return value.getTime(); }); } addSerializer(builder, BsonUndefined.class, String.class, (value) -> { // Johnzon fails from null values return "null"; }); addSerializer(builder, Binary.class, String.class, (value) -> { return BASE64.encode(value.getData()); }); addSerializer(builder, Code.class, String.class, (value) -> { return value.getCode(); }); addSerializer(builder, Decimal128.class, BigDecimal.class, (value) -> { return value.bigDecimalValue(); }); addSerializer(builder, ObjectId.class, String.class, (value) -> { return value.toHexString(); }); addSerializer(builder, Symbol.class, String.class, (value) -> { return value.getSymbol(); }); }
@Override public void accept(GensonBuilder builder) { addSerializer(builder, BsonBoolean.class, (value, writer, ctx) -> { writer.writeBoolean(value.getValue()); }); addSerializer(builder, BsonDateTime.class, (value, writer, ctx) -> { if (Config.USE_TIMESTAMPS) { writer.writeString(DataConverterRegistry.convert(String.class, new Date(value.getValue()))); } else { writer.writeNumber(value.getValue()); } }); addSerializer(builder, BsonDouble.class, (value, writer, ctx) -> { writer.writeNumber(value.getValue()); }); addSerializer(builder, BsonInt32.class, (value, writer, ctx) -> { writer.writeNumber(value.getValue()); }); addSerializer(builder, BsonInt64.class, (value, writer, ctx) -> { writer.writeNumber(value.getValue()); }); addSerializer(builder, BsonNull.class, (value, writer, ctx) -> { writer.writeNull(); }); addSerializer(builder, BsonRegularExpression.class, (value, writer, ctx) -> { writer.writeString(value.getPattern()); }); addSerializer(builder, BsonString.class, (value, writer, ctx) -> { writer.writeString(value.getValue()); }); addSerializer(builder, BsonTimestamp.class, (value, writer, ctx) -> { if (Config.USE_TIMESTAMPS) { writer.writeString(DataConverterRegistry.convert(String.class, new Date(value.getTime() * 1000L))); } else { writer.writeNumber(value.getTime()); } }); addSerializer(builder, BsonUndefined.class, (value, writer, ctx) -> { writer.writeNull(); }); addSerializer(builder, Binary.class, (value, writer, ctx) -> { writer.writeString(BASE64.encode(value.getData())); }); addSerializer(builder, Code.class, (value, writer, ctx) -> { writer.writeString(value.getCode()); }); addSerializer(builder, Decimal128.class, (value, writer, ctx) -> { writer.writeNumber(value.bigDecimalValue()); }); addSerializer(builder, ObjectId.class, (value, writer, ctx) -> { writer.writeString(value.toHexString()); }); addSerializer(builder, Symbol.class, (value, writer, ctx) -> { writer.writeString(value.getSymbol()); }); }
@Override public void accept(DslJson<Object> dslJson) { dslJson.registerWriter(BsonBoolean.class, (writer, value) -> { BoolConverter.serialize(value.getValue(), writer); }); dslJson.registerWriter(BsonDateTime.class, (writer, value) -> { if (Config.USE_TIMESTAMPS) { StringConverter.serialize(DataConverterRegistry.convert(String.class, new Date(value.getValue())), writer); } else { NumberConverter.serialize(value.getValue(), writer); } }); dslJson.registerWriter(BsonDouble.class, (writer, value) -> { NumberConverter.serialize(value.getValue(), writer); }); dslJson.registerWriter(BsonInt32.class, (writer, value) -> { NumberConverter.serialize(value.getValue(), writer); }); dslJson.registerWriter(BsonInt64.class, (writer, value) -> { NumberConverter.serialize(value.getValue(), writer); }); dslJson.registerWriter(BsonNull.class, (writer, value) -> { writer.writeNull(); }); dslJson.registerWriter(BsonRegularExpression.class, (writer, value) -> { StringConverter.serializeNullable(value.getPattern(), writer); }); dslJson.registerWriter(BsonString.class, (writer, value) -> { StringConverter.serializeNullable(value.getValue(), writer); }); dslJson.registerWriter(BsonTimestamp.class, (writer, value) -> { if (Config.USE_TIMESTAMPS) { StringConverter.serialize( DataConverterRegistry.convert(String.class, new Date(value.getTime() * 1000L)), writer); } else { NumberConverter.serialize(value.getTime(), writer); } }); dslJson.registerWriter(BsonUndefined.class, (writer, value) -> { writer.writeNull(); }); dslJson.registerWriter(Binary.class, (writer, value) -> { StringConverter.serialize(BASE64.encode(value.getData()), writer); }); dslJson.registerWriter(Code.class, (writer, value) -> { StringConverter.serializeNullable(value.getCode(), writer); }); dslJson.registerWriter(Decimal128.class, (writer, value) -> { NumberConverter.serialize(value.bigDecimalValue(), writer); }); dslJson.registerWriter(ObjectId.class, (writer, value) -> { StringConverter.serialize(value.toHexString(), writer); }); dslJson.registerWriter(Symbol.class, (writer, value) -> { StringConverter.serializeNullable(value.getSymbol(), writer); }); }