Search in sources :

Example 1 with BsonBinarySubType

use of org.bson.BsonBinarySubType in project mongo-java-driver by mongodb.

the class JsonReader method visitBinDataExtendedJson.

private BsonBinary visitBinDataExtendedJson() {
    verifyToken(JsonTokenType.COLON);
    JsonToken bytesToken = popToken();
    if (bytesToken.getType() != JsonTokenType.STRING) {
        throw new JsonParseException("JSON reader expected a string but found '%s'.", bytesToken.getValue());
    }
    verifyToken(JsonTokenType.COMMA);
    verifyString("$type");
    verifyToken(JsonTokenType.COLON);
    JsonToken subTypeToken = popToken();
    if (subTypeToken.getType() != JsonTokenType.STRING && subTypeToken.getType() != JsonTokenType.INT32) {
        throw new JsonParseException("JSON reader expected a string or number but found '%s'.", subTypeToken.getValue());
    }
    verifyToken(JsonTokenType.END_OBJECT);
    byte subType;
    if (subTypeToken.getType() == JsonTokenType.STRING) {
        subType = (byte) Integer.parseInt(subTypeToken.getValue(String.class), 16);
    } else {
        subType = subTypeToken.getValue(Integer.class).byteValue();
    }
    for (final BsonBinarySubType st : BsonBinarySubType.values()) {
        if (st.getValue() == subType) {
            return new BsonBinary(st, DatatypeConverter.parseBase64Binary(bytesToken.getValue(String.class)));
        }
    }
    return new BsonBinary(DatatypeConverter.parseBase64Binary(bytesToken.getValue(String.class)));
}
Also used : BsonBinarySubType(org.bson.BsonBinarySubType) BsonBinary(org.bson.BsonBinary)

Example 2 with BsonBinarySubType

use of org.bson.BsonBinarySubType in project mongo-java-driver by mongodb.

the class JsonReader method visitUUIDConstructor.

private BsonBinary visitUUIDConstructor(final String uuidConstructorName) {
    //TODO verify information related to https://jira.mongodb.org/browse/SERVER-3168
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken bytesToken = popToken();
    if (bytesToken.getType() != JsonTokenType.STRING) {
        throw new JsonParseException("JSON reader expected a string but found '%s'.", bytesToken.getValue());
    }
    verifyToken(JsonTokenType.RIGHT_PAREN);
    String hexString = bytesToken.getValue(String.class).replaceAll("\\{", "").replaceAll("\\}", "").replaceAll("-", "");
    byte[] bytes = DatatypeConverter.parseHexBinary(hexString);
    BsonBinarySubType subType = BsonBinarySubType.UUID_STANDARD;
    if (!"UUID".equals(uuidConstructorName) || !"GUID".equals(uuidConstructorName)) {
        subType = BsonBinarySubType.UUID_LEGACY;
    }
    return new BsonBinary(subType, bytes);
}
Also used : BsonBinarySubType(org.bson.BsonBinarySubType) BsonBinary(org.bson.BsonBinary)

Example 3 with BsonBinarySubType

use of org.bson.BsonBinarySubType in project mongo-java-driver by mongodb.

the class JsonReader method visitHexDataConstructor.

private BsonBinary visitHexDataConstructor() {
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken subTypeToken = popToken();
    if (subTypeToken.getType() != JsonTokenType.INT32) {
        throw new JsonParseException("JSON reader expected a binary subtype but found '%s'.", subTypeToken.getValue());
    }
    verifyToken(JsonTokenType.COMMA);
    String hex = readStringFromExtendedJson();
    verifyToken(JsonTokenType.RIGHT_PAREN);
    if ((hex.length() & 1) != 0) {
        hex = "0" + hex;
    }
    for (final BsonBinarySubType subType : BsonBinarySubType.values()) {
        if (subType.getValue() == subTypeToken.getValue(Integer.class)) {
            return new BsonBinary(subType, decodeHex(hex));
        }
    }
    return new BsonBinary(decodeHex(hex));
}
Also used : BsonBinarySubType(org.bson.BsonBinarySubType) BsonBinary(org.bson.BsonBinary)

Aggregations

BsonBinary (org.bson.BsonBinary)3 BsonBinarySubType (org.bson.BsonBinarySubType)3