use of org.bson.BsonType in project mongo-java-driver by mongodb.
the class NumberCodecHelper method decodeInt.
static int decodeInt(final BsonReader reader) {
int intValue;
BsonType bsonType = reader.getCurrentBsonType();
switch(bsonType) {
case INT32:
intValue = reader.readInt32();
break;
case INT64:
long longValue = reader.readInt64();
intValue = (int) longValue;
if (longValue != (long) intValue) {
throw invalidConversion(Integer.class, longValue);
}
break;
case DOUBLE:
double doubleValue = reader.readDouble();
intValue = (int) doubleValue;
if (doubleValue != (double) intValue) {
throw invalidConversion(Integer.class, doubleValue);
}
break;
case DECIMAL128:
Decimal128 decimal128 = reader.readDecimal128();
intValue = decimal128.intValue();
if (!decimal128.equals(new Decimal128(intValue))) {
throw invalidConversion(Integer.class, decimal128);
}
break;
default:
throw new BsonInvalidOperationException(format("Invalid numeric type, found: %s", bsonType));
}
return intValue;
}
use of org.bson.BsonType in project mongo-java-driver by mongodb.
the class NumberCodecHelper method decodeDouble.
static double decodeDouble(final BsonReader reader) {
double doubleValue;
BsonType bsonType = reader.getCurrentBsonType();
switch(bsonType) {
case INT32:
doubleValue = reader.readInt32();
break;
case INT64:
long longValue = reader.readInt64();
doubleValue = longValue;
if (longValue != (long) doubleValue) {
throw invalidConversion(Double.class, longValue);
}
break;
case DOUBLE:
doubleValue = reader.readDouble();
break;
case DECIMAL128:
Decimal128 decimal128 = reader.readDecimal128();
try {
doubleValue = decimal128.doubleValue();
if (!decimal128.equals(new Decimal128(new BigDecimal(doubleValue)))) {
throw invalidConversion(Double.class, decimal128);
}
} catch (NumberFormatException e) {
throw invalidConversion(Double.class, decimal128);
}
break;
default:
throw new BsonInvalidOperationException(format("Invalid numeric type, found: %s", bsonType));
}
return doubleValue;
}
use of org.bson.BsonType in project drill by axbaretto.
the class BsonRecordReader method writeToListOrMap.
private void writeToListOrMap(BsonReader reader, final MapOrListWriterImpl writer, boolean isList, String fieldName) {
writer.start();
// writing
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
if (!isList) {
fieldName = reader.readName();
}
BsonType currentBsonType = reader.getCurrentBsonType();
switch(currentBsonType) {
case INT32:
int readInt32 = reader.readInt32();
if (readNumbersAsDouble) {
writeDouble(readInt32, writer, fieldName, isList);
} else {
writeInt32(readInt32, writer, fieldName, isList);
}
atLeastOneWrite = true;
break;
case INT64:
long readInt64 = reader.readInt64();
if (readNumbersAsDouble) {
writeDouble(readInt64, writer, fieldName, isList);
} else {
writeInt64(readInt64, writer, fieldName, isList);
}
atLeastOneWrite = true;
break;
case ARRAY:
reader.readStartArray();
writeToListOrMap(reader, (MapOrListWriterImpl) writer.list(fieldName), true, fieldName);
atLeastOneWrite = true;
break;
case BINARY:
// handle types
writeBinary(reader, writer, fieldName, isList);
atLeastOneWrite = true;
break;
case BOOLEAN:
boolean readBoolean = reader.readBoolean();
writeBoolean(readBoolean, writer, fieldName, isList);
atLeastOneWrite = true;
break;
case DATE_TIME:
long readDateTime = reader.readDateTime();
writeDateTime(readDateTime, writer, fieldName, isList);
atLeastOneWrite = true;
break;
case DOCUMENT:
reader.readStartDocument();
// To handle nested Documents.
MapOrListWriterImpl _writer = writer;
if (!isList) {
_writer = (MapOrListWriterImpl) writer.map(fieldName);
} else {
_writer = (MapOrListWriterImpl) writer.listoftmap(fieldName);
}
writeToListOrMap(reader, _writer, false, fieldName);
atLeastOneWrite = true;
break;
case DOUBLE:
double readDouble = reader.readDouble();
writeDouble(readDouble, writer, fieldName, isList);
atLeastOneWrite = true;
break;
case JAVASCRIPT:
final String readJavaScript = reader.readJavaScript();
writeString(readJavaScript, writer, fieldName, isList);
atLeastOneWrite = true;
break;
case JAVASCRIPT_WITH_SCOPE:
final String readJavaScriptWithScopeString = reader.readJavaScriptWithScope();
writeString(readJavaScriptWithScopeString, writer, fieldName, isList);
atLeastOneWrite = true;
break;
case NULL:
// just read and ignore.
reader.readNull();
break;
case OBJECT_ID:
writeObjectId(reader, writer, fieldName, isList);
atLeastOneWrite = true;
break;
case STRING:
final String readString = reader.readString();
writeString(readString, writer, fieldName, isList);
atLeastOneWrite = true;
break;
case SYMBOL:
final String readSymbol = reader.readSymbol();
writeString(readSymbol, writer, fieldName, isList);
atLeastOneWrite = true;
break;
case TIMESTAMP:
int time = reader.readTimestamp().getTime();
writeTimeStamp(time, writer, fieldName, isList);
atLeastOneWrite = true;
break;
default:
// Didn't handled REGULAR_EXPRESSION and DB_POINTER types
throw new DrillRuntimeException("UnSupported Bson type: " + currentBsonType);
}
}
if (!isList) {
reader.readEndDocument();
} else {
reader.readEndArray();
}
}
use of org.bson.BsonType in project drill by axbaretto.
the class BsonRecordReader method write.
public void write(ComplexWriter writer, BsonReader reader) throws IOException {
reader.readStartDocument();
BsonType readBsonType = reader.getCurrentBsonType();
switch(readBsonType) {
case DOCUMENT:
writeToListOrMap(reader, new MapOrListWriterImpl(writer.rootAsMap()), false, null);
break;
default:
throw new DrillRuntimeException("Root object must be DOCUMENT type. Found: " + readBsonType);
}
}
use of org.bson.BsonType in project immutables by immutables.
the class BsonParser method readValue.
/**
* Read (parse) current bson value and stored it in local {@link ParseContext} cache.
*/
private void readValue() throws JsonParseException {
final BsonType type = type();
switch(type) {
case DOUBLE:
context.setValue(reader.readDouble());
break;
case INT32:
context.setValue(reader.readInt32());
break;
case INT64:
context.setValue(reader.readInt64());
break;
case DECIMAL128:
context.setValue(reader.readDecimal128().bigDecimalValue());
break;
case DATE_TIME:
context.setValue(reader.readDateTime());
break;
case TIMESTAMP:
context.setValue(reader.readTimestamp().getValue());
break;
case SYMBOL:
context.setValue(reader.readSymbol());
break;
case STRING:
context.setValue(reader.readString());
break;
case OBJECT_ID:
context.setValue(reader.readObjectId().toHexString());
break;
case REGULAR_EXPRESSION:
context.setValue(reader.readRegularExpression().getPattern());
break;
case BOOLEAN:
context.setValue(reader.readBoolean());
break;
case NULL:
reader.readNull();
context.setValue(null);
break;
case UNDEFINED:
reader.readUndefined();
context.setValue(null);
break;
case BINARY:
context.setValue(reader.readBinaryData().getData());
break;
default:
throw new JsonParseException(this, String.format("Unknown bson type %s (as json type %s)", type, currentToken()));
}
}
Aggregations