/** * Converts a binary uuid to a standard uuid * * @param json The json string (should contain "$binary" and "$type" or something like that) * @return The uuid */ private UUID convertBinaryUUID(String key, String json) { BsonDocument document = BsonDocument.parse(json); BsonValue value = document.get(key); if(!(value instanceof BsonBinary)) { return null; } byte[] bytes = ((BsonBinary) value).getData(); ByteBuffer bb = ByteBuffer.wrap(bytes); bb.order(ByteOrder.LITTLE_ENDIAN); long l1 = bb.getLong(); long l2 = bb.getLong(); return new UUID(l1, l2); }
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)); }
public BsonCodecGenerator(Models models) { this.models = models; Elements elems = models.getElemsUtil(); // Reusable things for multiple types objectIdTypeMirror = elems.getTypeElement(ObjectId.class.getCanonicalName()).asType(); bsonBinaryTypeMirror = elems.getTypeElement(BsonBinary.class.getCanonicalName()).asType(); dateTypeMirror = elems.getTypeElement(Date.class.getCanonicalName()).asType(); instantTypeMirror = elems.getTypeElement(Instant.class.getCanonicalName()).asType(); stringTypeMirror = elems.getTypeElement(String.class.getCanonicalName()).asType(); longTypeMirror = elems.getTypeElement(Long.class.getCanonicalName()).asType(); classClassName = ClassName.get(Class.class); bsonWriterParamSpec = ParameterSpec.builder(BsonWriter.class, "writer").addModifiers(Modifier.FINAL).build(); bsonReaderParamSpec = ParameterSpec.builder(BsonReader.class, "reader").addModifiers(Modifier.FINAL).build(); encoderCtxParamSpec = ParameterSpec.builder(EncoderContext.class, "ctx").addModifiers(Modifier.FINAL).build(); decoderCtxParamSpec = ParameterSpec.builder(DecoderContext.class, "ctx").addModifiers(Modifier.FINAL).build(); }
@Override public void addEncodeStatements(TypeMirror type, CodeGeneratorContext ctx) { MethodSpec.Builder builder = ctx.builder(); if (type.getKind() == TypeKind.BYTE) { builder.addStatement("writer.writeBinaryData(new $T($L))", BsonBinary.class, ctx.getter()); } else { builder.addStatement("$T[] bw = $L", Byte.class, ctx.getter()) .addStatement("byte[] b = new byte[bw.length]") .beginControlFlow("for (int i = 0; i < bw.length; i++)") .addStatement("b[i] = bw[i]").endControlFlow() .addStatement("writer.writeBinaryData(new $T(b))", BsonBinary.class); } }
@Override public void encode(BsonWriter writer, FileUploader value, EncoderContext encoderContext) { writer.writeStartDocument(); ObjectId objectId = new ObjectId(); value.setOid(objectId.toString()); writer.writeObjectId("_id", objectId); writer.writeBinaryData("file", new BsonBinary(value.getFile())); writer.writeEndDocument(); }
@Override protected BsonBinary encode(Collection<Byte> value) { final byte[] data = new byte[value.size()]; final Iterator<Byte> iterator = value.iterator(); int i = 0; while(iterator.hasNext()) { data[i++] = iterator.next(); } return new BsonBinary(data); }
@Override protected Collection<Byte> decode(BsonBinary bsonValue) { final Collection<Byte> value = getInstance(); for(byte bsonByte : bsonValue.getData()) { value.add(bsonByte); } return value; }
@Test public void primitiveByteArraySupport() { final byte[] value = new byte[1024]; new Random().nextBytes(value); final BsonBinary bsonValue = new BsonBinary(value); testSupport(value, bsonValue, byte[].class); assertArrayEquals(value, parser.fromBson(bsonValue, byte[].class, null)); }
@Test public void genericByteArraySupport() { final byte[] value = new byte[1024]; new Random().nextBytes(value); final BsonBinary bsonValue = new BsonBinary(value); testSupport(ArrayUtils.toObject(value), bsonValue, Byte[].class); assertArrayEquals(ArrayUtils.toObject(value), parser.fromBson(bsonValue, Byte[].class, null)); }
@Test public void serializeToBson_ShouldSerializePrimitiveByteArray_Successfully() { byte[] bytes = "a byte array".getBytes(); BsonValue bsonValue = this.parser.serializeToBson(bytes, null); byte[] data = ((BsonBinary) bsonValue).getData(); assertEquals(bytes, data); }
@Test public void serializeToBson_ShouldSerializeWrapperByteArray_Successfully() { byte[] bytes = "a byte array".getBytes(); Byte[] wrappedBytes = ArrayUtils.toObject(bytes); BsonValue bsonValue = this.parser.serializeToBson(wrappedBytes, null); byte[] data = ((BsonBinary) bsonValue).getData(); assertArrayEquals(bytes, data); }
private BsonDocument parseHexDocument(final BsonDocument document, final String hexDocument) { if (document.containsKey(hexDocument) && document.get(hexDocument).isDocument()) { byte[] bytes = DatatypeConverter.parseHexBinary(document.getDocument(hexDocument).getString("$hex").getValue()); document.put(hexDocument, new BsonBinary(bytes)); } return document; }
public void valueBinary(byte[] data) { delegate.writeBinaryData(new BsonBinary(data)); }
@Override public Binary decode(BsonValue bsonValue) { BsonBinary bsonBinary = bsonValue.asBinary(); return new Binary(bsonBinary.getType(), bsonBinary.getData()); }
@Override public BsonBinary encode(Binary object) { return new BsonBinary(object.getType(), object.getData()); }
@Override public Binary decode(BsonReader bsonReader) { BsonBinary bsonBinary = bsonReader.readBinaryData(); return new Binary(bsonBinary.getType(), bsonBinary.getData()); }
@Override public void encode(BsonWriter bsonWriter, Binary value) { bsonWriter.writeBinaryData(new BsonBinary(value.getType(), value.getData())); }
@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); }
@Override protected BsonBinary encode(Byte[] value) { return new BsonBinary(ArrayUtils.toPrimitive(value)); }
@Override protected Byte[] decode(BsonBinary value) { return ArrayUtils.toObject(value.getData()); }
@Override protected BsonBinary encode(byte[] value) { return new BsonBinary(value); }
@Override protected byte[] decode(BsonBinary value) { return value.getData(); }
private void testSupport(Object value, BsonBinary bsonValue, Class<?> type) { assertTrue(parser.isValidType(type)); assertEquals(bsonValue, parser.toBson(value, null)); }
@Test public void fromBson_ShouldParseRawValue_Correctly() { byte[] bytes = this.parser.fromBson(new BsonBinary("a byte array".getBytes()), byte[].class, null); assertArrayEquals("a byte array".getBytes(), bytes); }
@Test public void fromBson_ShouldParseWrapperRawValue_Correctly() { Byte[] bytes = this.parser.fromBson(new BsonBinary("a byte array".getBytes()), Byte[].class, null); assertArrayEquals("a byte array".getBytes(), ArrayUtils.toPrimitive(bytes)); }
@Test(expected = IllegalArgumentException.class) public void fromBson_ShouldRaise_IllegalArgumentExceptionIfTypeIsNull() { this.parser.fromBson(new BsonBinary("a byte array".getBytes()), null, null); }
@Test(expected = IllegalArgumentException.class) public void fromBson_ShouldThrow() { this.parser.fromBson(new BsonBinary("a byte array".getBytes()), String.class, null); }
@Test public void isValidBson_ShouldReturn_True() { this.parser = new ByteParser(null, null); boolean validBson = parser.isValidBson(new BsonBinary("a byte array".getBytes())); assertTrue(validBson); }
private void writeBinary(BsonReader reader, final MapOrListWriterImpl writer, String fieldName, boolean isList) { final VarBinaryHolder vb = new VarBinaryHolder(); BsonBinary readBinaryData = reader.readBinaryData(); byte[] data = readBinaryData.getData(); Byte type = (Byte) readBinaryData.getType(); // Based on specified binary type, cast it accordingly switch (type.intValue()) { case 1: // Double 1 writeDouble(ByteBuffer.wrap(data).getDouble(), writer, fieldName, isList); break; case 2: // String 2 writeString(new String(data), writer, fieldName, isList); break; case 8: // Boolean 8 boolean boolValue = (data == null || data.length == 0) ? false : data[0] != 0x00; writeBoolean(boolValue, writer, fieldName, isList); break; case 9: // Date 9 writeDateTime(ByteBuffer.wrap(data).getLong(), writer, fieldName, isList); break; case 13: // JavaScript 13 writeString(new String(data), writer, fieldName, isList); break; case 14: // Symbol 14 writeString(new String(data), writer, fieldName, isList); break; case 15: // JavaScript (with scope) 15 writeString(new String(data), writer, fieldName, isList); break; case 16: // 32-bit integer 16 writeInt32(ByteBuffer.wrap(data).getInt(), writer, fieldName, isList); break; case 17: // Timestamp 17 writeTimeStamp(ByteBuffer.wrap(data).getInt(), writer, fieldName, isList); break; case 18: // 64-bit integer 18 writeInt64(ByteBuffer.wrap(data).getInt(), writer, fieldName, isList); break; default: // In case of Object(3)/Binary data (5)/Object id (7) or in other case // considering as VarBinary final byte[] bytes = readBinaryData.getData(); writeBinary(writer, fieldName, isList, vb, bytes); break; } }
private List<Document> createValidDocuments() throws Exception { DateFormat format = getUTCDateFormat(); List<Document> documents = new ArrayList<>(); documents.add( new Document("double_field", 1.23) .append("string_field", "embulk") .append("array_field", Arrays.asList(1, 2, 3)) .append("binary_field", new BsonBinary(("test").getBytes("UTF-8"))) .append("boolean_field", true) .append("datetime_field", format.parse("2015-01-27T10:23:49.000Z")) .append("null_field", null) .append("regex_field", new BsonRegularExpression(".+?")) .append("javascript_field", new BsonJavaScript("var s = \"javascript\";")) .append("int32_field", 1) .append("timestamp_field", new BsonTimestamp(1463991177, 4)) .append("int64_field", new BsonInt64(314159265)) .append("document_field", new Document("k", true)) .append("symbol_field", new Symbol("symbol")) ); documents.add( new Document("boolean_field", false) .append("int32_field", 2) .append("document_field", new Document("k", 1)) ); documents.add( new Document("int32_field", 3) .append("document_field", new Document("k", 1.23)) ); documents.add( new Document("int32_field", 4) .append("document_field", new Document("k", "v")) ); documents.add( new Document("int32_field", 5) .append("document_field", new Document("k", format.parse("2015-02-02T23:13:45.000Z"))) ); return documents; }
protected abstract BsonBinary encode(T value);
protected abstract T decode(BsonBinary value);