use of org.bson.BsonType in project core-ng-project by neowu.
the class BsonReaderWrapper method startReadEntity.
public boolean startReadEntity(String field) {
BsonType currentType = reader.getCurrentBsonType();
if (currentType != null && currentType == BsonType.NULL) {
reader.readNull();
return false;
}
if (currentType != null && currentType != BsonType.DOCUMENT) {
logger.warn("unexpected field type, field={}, type={}", field, currentType);
reader.skipValue();
return false;
}
return true;
}
use of org.bson.BsonType in project core-ng-project by neowu.
the class BsonReaderWrapper method startReadList.
public List<?> startReadList(String field) {
BsonType currentType = reader.getCurrentBsonType();
if (currentType == BsonType.NULL) {
reader.readNull();
return null;
}
if (currentType != BsonType.ARRAY) {
logger.warn("unexpected field type, field={}, type={}", field, currentType);
reader.skipValue();
return null;
}
return new ArrayList<>();
}
use of org.bson.BsonType in project debezium by debezium.
the class MongoDataConverter method addFieldSchema.
public static void addFieldSchema(Entry<String, BsonValue> keyValuesforSchema, SchemaBuilder builder) {
String key = keyValuesforSchema.getKey();
BsonType type = keyValuesforSchema.getValue().getBsonType();
switch(type) {
case NULL:
LOG.warn("Data type {} not currently supported", type);
break;
case STRING:
case JAVASCRIPT:
case OBJECT_ID:
case DECIMAL128:
builder.field(key, Schema.OPTIONAL_STRING_SCHEMA);
break;
case DOUBLE:
builder.field(key, Schema.OPTIONAL_FLOAT64_SCHEMA);
break;
case BINARY:
builder.field(key, Schema.OPTIONAL_BYTES_SCHEMA);
break;
case INT32:
case TIMESTAMP:
builder.field(key, Schema.OPTIONAL_INT32_SCHEMA);
break;
case INT64:
case DATE_TIME:
builder.field(key, Schema.OPTIONAL_INT64_SCHEMA);
break;
case BOOLEAN:
builder.field(key, Schema.OPTIONAL_BOOLEAN_SCHEMA);
break;
case JAVASCRIPT_WITH_SCOPE:
SchemaBuilder jswithscope = SchemaBuilder.struct();
jswithscope.field("code", Schema.OPTIONAL_STRING_SCHEMA);
SchemaBuilder scope = SchemaBuilder.struct();
BsonDocument jwsDocument = keyValuesforSchema.getValue().asJavaScriptWithScope().getScope().asDocument();
for (Entry<String, BsonValue> jwsDocumentKey : jwsDocument.entrySet()) {
addFieldSchema(jwsDocumentKey, scope);
}
Schema scopeBuild = scope.build();
jswithscope.field("scope", scopeBuild).build();
builder.field(key, jswithscope);
break;
case REGULAR_EXPRESSION:
SchemaBuilder regexwop = SchemaBuilder.struct();
regexwop.field("regex", Schema.OPTIONAL_STRING_SCHEMA);
regexwop.field("options", Schema.OPTIONAL_STRING_SCHEMA);
builder.field(key, regexwop.build());
break;
case DOCUMENT:
SchemaBuilder builderDoc = SchemaBuilder.struct();
BsonDocument docs = keyValuesforSchema.getValue().asDocument();
for (Entry<String, BsonValue> doc : docs.entrySet()) {
addFieldSchema(doc, builderDoc);
}
builder.field(key, builderDoc.build());
break;
case ARRAY:
if (keyValuesforSchema.getValue().asArray().isEmpty()) {
builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_STRING_SCHEMA).build());
break;
} else {
BsonType valueType = keyValuesforSchema.getValue().asArray().get(0).getBsonType();
switch(valueType) {
case NULL:
LOG.warn("Data type {} not currently supported", valueType);
break;
case STRING:
case JAVASCRIPT:
case OBJECT_ID:
case DECIMAL128:
builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_STRING_SCHEMA).build());
break;
case DOUBLE:
builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_FLOAT64_SCHEMA).build());
break;
case BINARY:
builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_BYTES_SCHEMA).build());
break;
case INT32:
case TIMESTAMP:
builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_INT32_SCHEMA).build());
break;
case INT64:
case DATE_TIME:
builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_INT64_SCHEMA).build());
break;
case BOOLEAN:
builder.field(key, SchemaBuilder.array(Schema.OPTIONAL_BOOLEAN_SCHEMA).build());
break;
case DOCUMENT:
SchemaBuilder documentSchemaBuilder = SchemaBuilder.struct();
BsonDocument arrayDocs = keyValuesforSchema.getValue().asArray().get(0).asDocument();
for (Entry<String, BsonValue> arrayDoc : arrayDocs.entrySet()) {
addFieldSchema(arrayDoc, documentSchemaBuilder);
}
Schema build = documentSchemaBuilder.build();
builder.field(key, SchemaBuilder.array(build).build());
break;
default:
break;
}
break;
}
default:
break;
}
}
use of org.bson.BsonType in project morphia by mongodb.
the class ObjectCodec method decode.
@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
BsonType bsonType = reader.getCurrentBsonType();
Class<?> clazz;
if (bsonType == BsonType.DOCUMENT) {
clazz = Document.class;
String discriminatorField = datastore.getMapper().getOptions().getDiscriminatorKey();
BsonReaderMark mark = reader.getMark();
reader.readStartDocument();
while (clazz.equals(Document.class) && reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
if (reader.readName().equals(discriminatorField)) {
try {
clazz = datastore.getMapper().getClass(reader.readString());
} catch (CodecConfigurationException e) {
throw new MappingException(e.getMessage(), e);
}
} else {
reader.skipValue();
}
}
mark.reset();
} else {
clazz = bsonTypeClassMap.get(bsonType);
}
return datastore.getCodecRegistry().get(clazz).decode(reader, decoderContext);
}
use of org.bson.BsonType in project drill by apache.
the class BsonRecordReader method write.
public void write(ComplexWriter writer, BsonReader reader) throws IOException {
this.reader = reader;
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);
}
}
Aggregations