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)); }
private List<BsonDocument> processFiles(final BsonArray bsonArray, final List<BsonDocument> documents) { for (BsonValue rawDocument : bsonArray.getValues()) { if (rawDocument.isDocument()) { BsonDocument document = rawDocument.asDocument(); if (document.get("length").isInt32()) { document.put("length", new BsonInt64(document.getInt32("length").getValue())); } if (document.containsKey("metadata") && document.getDocument("metadata").isEmpty()) { document.remove("metadata"); } if (document.containsKey("aliases") && document.getArray("aliases").getValues().size() == 0) { document.remove("aliases"); } if (document.containsKey("contentType") && document.getString("contentType").getValue().length() == 0) { document.remove("contentType"); } documents.add(document); } } return documents; }
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 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); } }
private BsonDocument generateHistogram(Histogram histogram) { BsonDocument document = new BsonDocument(); final Snapshot snapshot = histogram.getSnapshot(); document.append("count", new BsonInt64(histogram.getCount())); document.append("max", new BsonDouble(snapshot.getMax())); document.append("mean", new BsonDouble(snapshot.getMean())); document.append("min", new BsonDouble(snapshot.getMin())); document.append("p50", new BsonDouble(snapshot.getMedian())); document.append("p75", new BsonDouble(snapshot.get75thPercentile())); document.append("p95", new BsonDouble(snapshot.get95thPercentile())); document.append("p98", new BsonDouble(snapshot.get98thPercentile())); document.append("p99", new BsonDouble(snapshot.get99thPercentile())); document.append("p999", new BsonDouble(snapshot.get999thPercentile())); if (showSamples) { document.append("values", new BsonArray( Arrays.stream(snapshot.getValues()) .mapToObj(BsonInt64::new) .collect(Collectors.toList()) )); } document.append("stddev", new BsonDouble(snapshot.getStdDev())); return document; }
/** * 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()); }
@Override public Long getLong(Object key) { BsonInt64 int64 = innerBsonDocument.getInt64(key); if (int64 != null) { return (Long) BsonValueConverterRepertory.getValueConverterByBsonType(int64.getBsonType()).decode(int64); } else { return null; } }
private BsonDocument getBsonDocumentHasDeepLayer() { BsonDocument bsonObj = new BsonDocument().append("testDouble", new BsonDouble(20.777)); List<BsonDocument> list = new ArrayList<BsonDocument>(); list.add(bsonObj); list.add(bsonObj); BsonDocument deepBsonDocument = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("testArray", new BsonArray(list)); BsonDocument bsonDocument1 = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("bson_test", deepBsonDocument); BsonDocument bsonDocument2 = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("bson_test", bsonDocument1); BsonDocument bsonDocument3 = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("bson_test", bsonDocument2) .append("testArray", new BsonArray(list)); BsonDocument bsonDocument4 = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("bson_test", bsonDocument3) .append("testArray", new BsonArray(list)); BsonDocument bsonDocument5 = new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("testString", new BsonString("testStringV")) .append("bson_test", bsonDocument4) .append("testArray", new BsonArray(list)); return new BsonDocument().append("testDouble", new BsonDouble(20.99)) .append("bson_test", bsonDocument5) .append("testLong", new BsonInt64(233332)); }
@Test public void testIntType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); bsonDoc.append("seqNo", new BsonInt64(10)); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); assertEquals(10l, mapReader.reader("seqNo").readLong().longValue()); }
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); } }
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; }
private BsonDocument generateTimer(Timer timer) { BsonDocument document = new BsonDocument(); final Snapshot snapshot = timer.getSnapshot(); document.append("count", new BsonInt64(timer.getCount())); document.append("max", new BsonDouble(snapshot.getMax() * durationFactor)); document.append("mean", new BsonDouble(snapshot.getMean() * durationFactor)); document.append("min", new BsonDouble(snapshot.getMin() * durationFactor)); document.append("p50", new BsonDouble(snapshot.getMedian() * durationFactor)); document.append("p75", new BsonDouble(snapshot.get75thPercentile() * durationFactor)); document.append("p95", new BsonDouble(snapshot.get95thPercentile() * durationFactor)); document.append("p98", new BsonDouble(snapshot.get98thPercentile() * durationFactor)); document.append("p99", new BsonDouble(snapshot.get99thPercentile() * durationFactor)); document.append("p999", new BsonDouble(snapshot.get999thPercentile() * durationFactor)); if (showSamples) { document.append("values", new BsonArray( Arrays.stream(snapshot.getValues()) .mapToDouble(x -> x * durationFactor) .mapToObj(BsonDouble::new) .collect(Collectors.toList()) )); } document.append("stddev", new BsonDouble(snapshot.getStdDev() * durationFactor)); document.append("m15_rate", new BsonDouble(timer.getFifteenMinuteRate() * rateFactor)); document.append("m1_rate", new BsonDouble(timer.getOneMinuteRate() * rateFactor)); document.append("m5_rate", new BsonDouble(timer.getFiveMinuteRate() * rateFactor)); document.append("mean_rate", new BsonDouble(timer.getMeanRate() * rateFactor)); document.append("duration_units", new BsonString(durationUnit.name().toLowerCase(Locale.US))); document.append("rate_units", new BsonString("calls/" + singularRateUnitString)); return document; }
private BsonDocument generateMeter(Meter meter) { BsonDocument document = new BsonDocument(); document.append("count", new BsonInt64(meter.getCount())); document.append("m15_rate", new BsonDouble(meter.getFifteenMinuteRate() * rateFactor)); document.append("m1_rate", new BsonDouble(meter.getOneMinuteRate() * rateFactor)); document.append("m5_rate", new BsonDouble(meter.getFiveMinuteRate() * rateFactor)); document.append("mean_rate", new BsonDouble(meter.getMeanRate() * rateFactor)); document.append("units", new BsonString("calls/" + singularRateUnitString)); return document; }
@Test public void deserialise_long() { long value = random.nextLong(); BsonDocument doc = new BsonDocument(); doc.put("my-value", new BsonInt64(value)); Key<Long> longKey = Key.named("my-value"); Record record = BsonRecordDeserialiser.builder() .readLong(longKey) .get() .apply(doc); assertEquals("wrong long value", value, (long) longKey.get(record).get()); }
@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); }); }
@Override public void accept(MessagePack mapper) { addSerializer(mapper, BsonBoolean.class, (packer, value) -> { packer.write(value.getValue()); }); addSerializer(mapper, BsonDateTime.class, (packer, value) -> { packer.write(value.getValue()); }); addSerializer(mapper, BsonDouble.class, (packer, value) -> { packer.write(value.getValue()); }); addSerializer(mapper, BsonInt32.class, (packer, value) -> { packer.write(value.getValue()); }); addSerializer(mapper, BsonInt64.class, (packer, value) -> { packer.write(value.getValue()); }); addSerializer(mapper, BsonNull.class, (packer, value) -> { packer.writeNil(); }); addSerializer(mapper, BsonRegularExpression.class, (packer, value) -> { packer.write(value.getPattern()); }); addSerializer(mapper, BsonString.class, (packer, value) -> { packer.write(value.getValue()); }); addSerializer(mapper, BsonTimestamp.class, (packer, value) -> { packer.write(value.getTime() * 1000L); }); addSerializer(mapper, BsonUndefined.class, (packer, value) -> { packer.writeNil(); }); addSerializer(mapper, Binary.class, (packer, value) -> { packer.write(BASE64.encode(value.getData())); }); addSerializer(mapper, Code.class, (packer, value) -> { packer.write(value.getCode()); }); addSerializer(mapper, Decimal128.class, (packer, value) -> { packer.write(value.bigDecimalValue()); }); addSerializer(mapper, ObjectId.class, (packer, value) -> { packer.write(value.toHexString()); }); addSerializer(mapper, Symbol.class, (packer, value) -> { packer.write(value.getSymbol()); }); }
@Override public void accept(SerializeConfig config) { addSerializer(config, BsonBoolean.class, (value, serializer) -> { serializer.write(value.getValue()); }); addSerializer(config, BsonDateTime.class, (value, serializer) -> { if (Config.USE_TIMESTAMPS) { serializer.write(DataConverterRegistry.convert(String.class, new Date(value.getValue()))); } else { serializer.write(value.getValue()); } }); addSerializer(config, BsonDouble.class, (value, serializer) -> { serializer.write(value.getValue()); }); addSerializer(config, BsonInt32.class, (value, serializer) -> { serializer.write(value.getValue()); }); addSerializer(config, BsonInt64.class, (value, serializer) -> { serializer.write(value.getValue()); }); addSerializer(config, BsonNull.class, (value, serializer) -> { serializer.writeNull(); }); addSerializer(config, BsonRegularExpression.class, (value, serializer) -> { serializer.write(value.getPattern()); }); addSerializer(config, BsonString.class, (value, serializer) -> { serializer.write(value.getValue()); }); addSerializer(config, BsonTimestamp.class, (value, serializer) -> { if (Config.USE_TIMESTAMPS) { serializer.write(DataConverterRegistry.convert(String.class, new Date(value.getTime() * 1000L))); } else { serializer.write(value.getTime()); } }); addSerializer(config, BsonUndefined.class, (value, serializer) -> { serializer.writeNull(); }); addSerializer(config, Binary.class, (value, serializer) -> { serializer.write(BASE64.encode(value.getData())); }); addSerializer(config, Code.class, (value, serializer) -> { serializer.write(value.getCode()); }); addSerializer(config, Decimal128.class, (value, serializer) -> { serializer.write(value.bigDecimalValue()); }); addSerializer(config, ObjectId.class, (value, serializer) -> { serializer.write(value.toHexString()); }); addSerializer(config, Symbol.class, (value, serializer) -> { serializer.write(value.getSymbol()); }); }
@Override public void accept(JsonSerializer serializer) { addSerializer(serializer, BsonBoolean.class, (value, ctx) -> { ctx.write(Boolean.toString(value.getValue())); }); addSerializer(serializer, BsonDateTime.class, (value, ctx) -> { if (Config.USE_TIMESTAMPS) { ctx.writeString(DataConverterRegistry.convert(String.class, new Date(value.getValue()))); } else { ctx.writeNumber(value.getValue()); } }); addSerializer(serializer, BsonDouble.class, (value, ctx) -> { ctx.writeNumber(value.getValue()); }); addSerializer(serializer, BsonInt32.class, (value, ctx) -> { ctx.writeNumber(value.getValue()); }); addSerializer(serializer, BsonInt64.class, (value, ctx) -> { ctx.writeNumber(value.getValue()); }); addSerializer(serializer, BsonNull.class, (value, ctx) -> { ctx.write("null"); }); addSerializer(serializer, BsonRegularExpression.class, (value, ctx) -> { ctx.writeString(value.getPattern()); }); addSerializer(serializer, BsonString.class, (value, ctx) -> { ctx.writeString(value.getValue()); }); addSerializer(serializer, BsonTimestamp.class, (value, ctx) -> { if (Config.USE_TIMESTAMPS) { ctx.writeString( DataConverterRegistry.convert(String.class, new Date(value.getTime() * 1000L))); } else { ctx.writeNumber(value.getTime()); } }); addSerializer(serializer, BsonUndefined.class, (value, ctx) -> { ctx.write("null"); }); addSerializer(serializer, Binary.class, (value, ctx) -> { ctx.writeString(BASE64.encode(value.getData())); }); addSerializer(serializer, Code.class, (value, ctx) -> { ctx.writeString(value.getCode()); }); addSerializer(serializer, Decimal128.class, (value, ctx) -> { ctx.writeNumber(value.bigDecimalValue()); }); addSerializer(serializer, ObjectId.class, (value, ctx) -> { ctx.writeString(value.toHexString()); }); addSerializer(serializer, Symbol.class, (value, ctx) -> { ctx.writeString(value.getSymbol()); }); }
@Override public void accept(TypeTransformerMap map) { addSerializer(map, BsonBoolean.class, (value, ctx) -> { ctx.write(Boolean.toString(value.getValue())); }); addSerializer(map, BsonDateTime.class, (value, ctx) -> { if (Config.USE_TIMESTAMPS) { ctx.writeQuoted(DataConverterRegistry.convert(String.class, new Date(value.getValue()))); } else { ctx.write(Long.toString(value.getValue())); } }); addSerializer(map, BsonDouble.class, (value, ctx) -> { ctx.write(Double.toString(value.getValue())); }); addSerializer(map, BsonInt32.class, (value, ctx) -> { ctx.write(Integer.toString(value.getValue())); }); addSerializer(map, BsonInt64.class, (value, ctx) -> { ctx.write(Long.toString(value.getValue())); }); addSerializer(map, BsonNull.class, (value, ctx) -> { ctx.write("null"); }); addSerializer(map, BsonRegularExpression.class, (value, ctx) -> { ctx.writeQuoted(value.getPattern()); }); addSerializer(map, BsonString.class, (value, ctx) -> { ctx.writeQuoted(value.getValue()); }); addSerializer(map, BsonTimestamp.class, (value, ctx) -> { if (Config.USE_TIMESTAMPS) { ctx.writeQuoted(DataConverterRegistry.convert(String.class, new Date(value.getTime() * 1000L))); } else { ctx.write(Integer.toString(value.getTime())); } }); addSerializer(map, BsonUndefined.class, (value, ctx) -> { ctx.write("null"); }); addSerializer(map, Binary.class, (value, ctx) -> { ctx.writeQuoted(BASE64.encode(value.getData())); }); addSerializer(map, Code.class, (value, ctx) -> { ctx.writeQuoted(value.getCode()); }); addSerializer(map, Decimal128.class, (value, ctx) -> { ctx.write(value.bigDecimalValue().toPlainString()); }); addSerializer(map, ObjectId.class, (value, ctx) -> { ctx.writeQuoted(value.toHexString()); }); addSerializer(map, Symbol.class, (value, ctx) -> { ctx.writeQuoted(value.getSymbol()); }); }
@Override public void accept(HashMap<Class<?>, Function<Object, Object>> converters) { addSerializer(converters, BsonBoolean.class, (value) -> { return value.getValue(); }); addSerializer(converters, BsonDateTime.class, (value) -> { return new Date(value.getValue()); }); addSerializer(converters, BsonDouble.class, (value) -> { return value.getValue(); }); addSerializer(converters, BsonInt32.class, (value) -> { return value.getValue(); }); addSerializer(converters, BsonInt64.class, (value) -> { return value.getValue(); }); addSerializer(converters, BsonNull.class, (value) -> { return null; }); addSerializer(converters, BsonRegularExpression.class, (value) -> { return value.getPattern(); }); addSerializer(converters, BsonString.class, (value) -> { return value.getValue(); }); addSerializer(converters, BsonTimestamp.class, (value) -> { return new Date(value.getTime() * 1000L); }); addSerializer(converters, BsonUndefined.class, (value) -> { return null; }); addSerializer(converters, Binary.class, (value) -> { return value.getData(); }); addSerializer(converters, Code.class, (value) -> { return value.getCode(); }); addSerializer(converters, Decimal128.class, (value) -> { return value.bigDecimalValue(); }); addSerializer(converters, ObjectId.class, (value) -> { return value.toHexString(); }); addSerializer(converters, Symbol.class, (value) -> { return value.getSymbol(); }); }
@Override public void accept(GsonBuilder builder) { addSerializer(builder, BsonBoolean.class, (value) -> { return new JsonPrimitive(value.getValue()); }); addSerializer(builder, BsonDateTime.class, (value) -> { if (Config.USE_TIMESTAMPS) { return new JsonPrimitive(DataConverterRegistry.convert(String.class, new Date(value.getValue()))); } else { return new JsonPrimitive(value.getValue()); } }); addSerializer(builder, BsonDouble.class, (value) -> { return new JsonPrimitive(value.getValue()); }); addSerializer(builder, BsonInt32.class, (value) -> { return new JsonPrimitive(value.getValue()); }); addSerializer(builder, BsonInt64.class, (value) -> { return new JsonPrimitive(value.getValue()); }); addSerializer(builder, BsonNull.class, (value) -> { return JsonNull.INSTANCE; }); addSerializer(builder, BsonRegularExpression.class, (value) -> { return new JsonPrimitive(value.getPattern()); }); addSerializer(builder, BsonString.class, (value) -> { return new JsonPrimitive(value.getValue()); }); addSerializer(builder, BsonTimestamp.class, (value) -> { if (Config.USE_TIMESTAMPS) { return new JsonPrimitive( DataConverterRegistry.convert(String.class, new Date(value.getTime() * 1000L))); } else { return new JsonPrimitive(value.getTime()); } }); addSerializer(builder, BsonUndefined.class, (value) -> { return JsonNull.INSTANCE; }); addSerializer(builder, Binary.class, (value) -> { return new JsonPrimitive(BASE64.encode(value.getData())); }); addSerializer(builder, Code.class, (value) -> { return new JsonPrimitive(value.getCode()); }); addSerializer(builder, Decimal128.class, (value) -> { return new JsonPrimitive(value.bigDecimalValue()); }); addSerializer(builder, ObjectId.class, (value) -> { return new JsonPrimitive(value.toHexString()); }); addSerializer(builder, Symbol.class, (value) -> { return new JsonPrimitive(value.getSymbol()); }); }
@Override public void accept(XStream mapper) { addSerializer(mapper, BsonBoolean.class, (value) -> { return Boolean.toString(value.getValue()); }); addSerializer(mapper, BsonDateTime.class, (value) -> { if (Config.USE_TIMESTAMPS) { return DataConverterRegistry.convert(String.class, new Date(value.getValue())); } return Long.toString(value.getValue()); }); addSerializer(mapper, BsonDouble.class, (value) -> { return Double.toString(value.getValue()); }); addSerializer(mapper, BsonInt32.class, (value) -> { return Integer.toString(value.getValue()); }); addSerializer(mapper, BsonInt64.class, (value) -> { return Long.toString(value.getValue()); }); addSerializer(mapper, BsonNull.class, (value) -> { return "null"; }); addSerializer(mapper, BsonRegularExpression.class, (value) -> { return value.getPattern(); }); addSerializer(mapper, BsonString.class, (value) -> { return value.getValue(); }); addSerializer(mapper, BsonTimestamp.class, (value) -> { if (Config.USE_TIMESTAMPS) { return DataConverterRegistry.convert(String.class, new Date(value.getTime() * 1000L)); } return Integer.toString(value.getTime()); }); addSerializer(mapper, BsonUndefined.class, (value) -> { return "null"; }); addSerializer(mapper, Binary.class, (value) -> { return BASE64.encode(value.getData()); }); addSerializer(mapper, Code.class, (value) -> { return value.getCode(); }); addSerializer(mapper, Decimal128.class, (value) -> { return value.bigDecimalValue().toPlainString(); }); addSerializer(mapper, ObjectId.class, (value) -> { return value.toHexString(); }); addSerializer(mapper, Symbol.class, (value) -> { return value.getSymbol(); }); }
@Test public void testMongoTypes() throws Exception { // JSON-Simple and JsonUtil aren't extendable APIs String writerClass = TreeWriterRegistry.getWriter(TreeWriterRegistry.JSON).getClass().toString(); boolean unsupportedAPI = writerClass.contains("Simple") || writerClass.contains("JsonUtil"); if (unsupportedAPI) { return; } Document doc = new Document(); doc.put("BsonBoolean", new BsonBoolean(true)); long time = System.currentTimeMillis(); doc.put("BsonDateTime", new BsonDateTime(time)); doc.put("BsonDouble", new BsonDouble(123.456)); doc.put("BsonInt32", new BsonInt32(123)); doc.put("BsonInt64", new BsonInt64(123456)); doc.put("BsonNull", new BsonNull()); doc.put("BsonRegularExpression", new BsonRegularExpression("abc")); doc.put("BsonString", new BsonString("abcdefgh")); doc.put("BsonTimestamp", new BsonTimestamp(12, 23)); doc.put("BsonUndefined", new BsonUndefined()); doc.put("Binary", new Binary("abcdefgh".getBytes())); doc.put("Code", new Code("var a = 5;")); doc.put("Decimal128", new Decimal128(123456789)); ObjectId objectID = new ObjectId(); doc.put("ObjectId", objectID); doc.put("Symbol", new Symbol("s")); Tree t = new Tree(doc, null); String json = t.toString(); System.out.println("-------------------- BSON --------------------"); System.out.println("Output of " + writerClass + " serializer (MongoDB types):"); System.out.println(json); t = new Tree(json); assertTrue(t.get("BsonBoolean", false)); Date date = t.get("BsonDateTime", new Date()); assertEquals(time / 1000L, date.getTime() / 1000L); assertEquals(123.456, t.get("BsonDouble", 1d)); assertEquals(123, t.get("BsonInt32", 1)); assertEquals(123456L, t.get("BsonInt64", 1L)); assertNull(t.get("BsonNull", "?")); assertEquals("abc", t.get("BsonRegularExpression", "?")); assertEquals("abcdefgh", t.get("BsonString", "?")); // String or Number date = t.get("BsonTimestamp", new Date()); assertEquals(12000L, date.getTime()); assertNull(t.get("BsonUndefined", "?")); assertEquals("abcdefgh", new String(t.get("Binary", "?".getBytes()))); assertEquals("var a = 5;", t.get("Code", "?")); assertEquals(123456789L, t.get("Decimal128", 1L)); assertEquals(objectID.toHexString(), t.get("ObjectId", "?")); assertEquals("s", t.get("Symbol", "?")); }
@Test public void testMongoTypes() throws Exception { Document doc = new Document(); doc.put("BsonBoolean", new BsonBoolean(true)); doc.put("BsonDateTime", new BsonDateTime(12345)); doc.put("BsonDouble", new BsonDouble(123.456)); doc.put("BsonInt32", new BsonInt32(123)); doc.put("BsonInt64", new BsonInt64(123456)); doc.put("BsonNull", new BsonNull()); doc.put("BsonRegularExpression", new BsonRegularExpression("abc")); doc.put("BsonString", new BsonString("abcdefgh")); doc.put("BsonTimestamp", new BsonTimestamp(12, 23)); doc.put("BsonUndefined", new BsonUndefined()); doc.put("Binary", new Binary("abcdefgh".getBytes())); doc.put("Code", new Code("var a = 5;")); doc.put("Decimal128", new Decimal128(123456789)); doc.put("ObjectId", new ObjectId()); doc.put("Symbol", new Symbol("s")); Document map = new Document(); map.put("a", "b"); map.put("c", 5); doc.put("map", map); ArrayList<Object> list = new ArrayList<>(); list.add("c"); list.add("b"); list.add("a"); doc.put("list", list); Tree t = new Tree(doc, null); String json = t.toString(); String writerClass = TreeWriterRegistry.getWriter(TreeWriterRegistry.JSON).getClass().toString(); System.out.println("--------------------------------------------------------------"); System.out.println("Output of " + writerClass + " serializer:"); System.out.println(json); Tree t2 = new Tree(json); assertEquals(true, t2.get("BsonBoolean", false)); // assertEquals(12345, t2.get("BsonDateTime", -1)); assertEquals(123.456, t2.get("BsonDouble", 1d)); assertEquals(123, t2.get("BsonInt32", 345)); assertEquals(123456, t2.get("BsonInt64", 1)); assertNull(t2.get("BsonNull", "X")); assertEquals("abc", t2.get("BsonRegularExpression", "xcf")); assertEquals("abcdefgh", t2.get("BsonString", "fsdfasdf")); // doc.put("BsonTimestamp", new BsonTimestamp(12, 23)); // doc.put("BsonUndefined", new BsonUndefined()); // doc.put("Binary", new Binary("abcdefgh".getBytes())); // doc.put("Code", new Code("var a = 5;")); // doc.put("Decimal128", new Decimal128(123456789)); // doc.put("ObjectId", new ObjectId()); // doc.put("Symbol", new Symbol("s")); }
@Override public BsonInt64 encode(Long object) { return new BsonInt64(object); }
@Test public void decode() throws Exception { BsonDocument bsonObj = new BsonDocument().append("testDouble", new BsonDouble(20.777)); List<BsonDocument> list = new ArrayList<BsonDocument>(); list.add(bsonObj); list.add(bsonObj); List<BsonArray> arrayList = new ArrayList<BsonArray>(); arrayList.add(new BsonArray(list)); arrayList.add(new BsonArray(list)); 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)); BsonDocument bsonDocument1 = 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(new Date().getTime())) .append("testNull", new BsonNull()) .append("testInt", new BsonInt32(233)) .append("testLong", new BsonInt64(233332)); BsonTest bsonTest = BsonDocumentConverter.getInstance().decode(bsonDocument1, BsonTest.class, BsonMapperConfig.DEFALUT); System.out.println(bsonTest.getTestDouble()); System.out.println(bsonTest.getTestString()); System.out.println(bsonTest.getTestArray()); System.out.println(Arrays.toString(bsonTest.getTestBinary().getData())); System.out.println(bsonTest.getTestObjectId()); System.out.println(bsonTest.getTestStringObjectId()); System.out.println(bsonTest.isTestBoolean()); System.out.println(bsonTest.getTestDate()); System.out.println(bsonTest.getTestNull()); System.out.println(bsonTest.getTestInt()); System.out.println(bsonTest.getTestLong()); System.out.println(bsonTest); }