本文整理汇总了Java中org.bson.codecs.DecoderContext类的典型用法代码示例。如果您正苦于以下问题:Java DecoderContext类的具体用法?Java DecoderContext怎么用?Java DecoderContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DecoderContext类属于org.bson.codecs包,在下文中一共展示了DecoderContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decode
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@Override
public MonthDay decode(
BsonReader reader,
DecoderContext decoderContext) {
BigDecimal value = reader
.readDecimal128()
.bigDecimalValue();
int month = value.intValue();
return of(
month,
value.subtract(new BigDecimal(month))
.scaleByPowerOfTen(2)
.intValue()
);
}
示例2: decode
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@Override
public YearMonth decode(
BsonReader reader,
DecoderContext decoderContext) {
BigDecimal value = reader
.readDecimal128()
.bigDecimalValue();
int year = value.intValue();
return of(
year,
value.subtract(new BigDecimal(year))
.scaleByPowerOfTen(2)
.abs()
.intValue()
);
}
示例3: decode
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@Override
public Duration decode(
BsonReader reader,
DecoderContext decoderContext) {
BigDecimal value = reader
.readDecimal128()
.bigDecimalValue();
long seconds = value.longValue();
return ofSeconds(
seconds,
value.subtract(new BigDecimal(seconds))
.scaleByPowerOfTen(9)
.abs()
.intValue()
);
}
示例4: decoderFor
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
/**
* Creates a "reader" which is able to deserialize binary data into a immutables instance (using GSON type adapter).
*/
public static <T> Decoder<T> decoderFor(final Class<T> type, final TypeAdapter<T> adapter) {
checkNotNull(type, "type");
checkNotNull(adapter, "adapter");
return new Decoder<T>() {
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
if (!(reader instanceof AbstractBsonReader)) {
throw new UnsupportedOperationException(String.format("Only readers of type %s supported. Yours is %s",
AbstractBsonReader.class.getName(), reader.getClass().getName()));
}
try {
return adapter.read(new org.immutables.mongo.repository.internal.BsonReader((AbstractBsonReader) reader));
} catch (IOException e) {
throw new RuntimeException(String.format("Couldn't encode %s", type), e);
}
}
};
}
示例5: decode
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
@Override
public SyncNodeDetails decode(BsonReader bsonReader, DecoderContext context) {
Document document = documentCodec.decode(bsonReader, context);
SyncNodeDetails nodeAndEvent = new SyncNodeDetails();
SyncNode node = SyncNodeDecoder.decodeNode((Document) document.get("node"));
nodeAndEvent.setNode(node);
SyncEvent event = null;
List<SyncEvent> eventList = new ArrayList<SyncEvent>();
SyncMapAndEventDecoder eventDecoder = new SyncMapAndEventDecoder();
@SuppressWarnings("unchecked")
List<Document> list = (List<Document>) document.get("events");
for(Document doc : list){
event = eventDecoder.decodeSyncEvent(doc);
eventList.add(event);
}
nodeAndEvent.setEvent(eventList);
return nodeAndEvent;
}
示例6: decode
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public O2MSyncEventLog decode(BsonReader arg0, DecoderContext arg1) {
Document document = documentCodec.decode(arg0, arg1);
O2MSyncEventLog log = new O2MSyncEventLog();
log.setLogId(document.getObjectId(_ID));
log.setEventId(document.getString(EVENT_ID));
log.setCrOn(document.getDate(CREATED_ON));
log.setSyOn(document.getDate(SYNCED_ON));
log.setStatus(document.getString(STATUS));
log.setOperation(document.getString(OPERATION));
List<Document> filterDocList = (List<Document>) document.get(EVENT_FILTERS);
if(filterDocList!=null){
List<O2MSyncEventInfo> filters = new ArrayList<O2MSyncEventInfo>(filterDocList.size());
O2MSyncEventInfo filter = null;
for(Document filterDoc : filterDocList){
filter= new O2MSyncEventInfo();
filter.setTableName(filterDoc.getString(TABLE_NAME));
filter.setColumnName(filterDoc.getString(COLUMN_NAME));
filter.setColumnValue(filterDoc.get(COLUMN_VALUE));
filters.add(filter);
}
log.setEventFilters(filters);
}
return log;
}
示例7: decode
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@Override
public Map<String, V> decode(BsonReader reader, DecoderContext decoderContext) {
Map<String, V> map = newInstance();
if (BsonType.DOCUMENT.equals(reader.getCurrentBsonType())) {
reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String key = reader.readName();
V value = valueTypeCodec.decode(reader, decoderContext);
map.put(key, value);
}
reader.readEndDocument();
} else {
LOGGER.warn("Expected {} from reader but got {}. Skipping value.", BsonType.DOCUMENT, reader.getCurrentBsonType());
reader.skipValue();
}
return map;
}
示例8: decode
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
if (BsonType.STRING.equals(reader.getCurrentBsonType())) {
String name = reader.readString();
if (name != null) {
try {
return Enum.valueOf(clazz, name);
} catch (IllegalArgumentException e) {
LOGGER.warn("Enum value {} could not be determined for enum type {}", name, clazz, e);
}
}
} else {
LOGGER.warn("Expected {} from reader but got {}. Skipping value.", BsonType.STRING, reader.getCurrentBsonType());
reader.skipValue();
}
return null;
}
示例9: testResilience
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
/**
* Testing if List<String> can be decoded into a
*/
@Test
public void testResilience() {
Codec<EncodingPojo> encodingPojoCodec = codecRegistry.get(EncodingPojo.class);
Codec<DecodingPojo> decodingPojoCodec = codecRegistry.get(DecodingPojo.class);
EncodingPojo encodingPojo = new EncodingPojo();
encodingPojo.someList = new ArrayList<>();
encodingPojo.someList.add("string1");
encodingPojo.someList.add("string2");
encodingPojo.someList.add("string3");
StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter, new JsonWriterSettings(true));
encodingPojoCodec.encode(writer, encodingPojo, EncoderContext.builder().build());
System.out.println(stringWriter.toString());
DecodingPojo decodingPojo = decodingPojoCodec.decode(new JsonReader(stringWriter.toString()), DecoderContext.builder().build());
Assert.assertNotNull(decodingPojo.someList);
assertThat(decodingPojo.someList, instanceOf(ListOfStrings.class));
}
示例10: decode
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@Override
public LocalDateTime decode(
BsonReader reader,
DecoderContext decoderContext) {
return ofEpochMilli(reader.readDateTime())
.atOffset(UTC)
.toLocalDateTime();
}
示例11: decode
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
public void decode(BsonReader reader, T instance, DecoderContext decoderContext) {
LOGGER.debug("Decode field : " + getMappedFieldName() + " Signature: " + fieldTypePair.getRealType());
if (field.getType().isPrimitive()) {
if (reader.getCurrentBsonType() == BsonType.NULL || reader.getCurrentBsonType() == BsonType.UNDEFINED) {
reader.skipValue();
} else {
primitiveType.decode(reader, instance, decoderContext, this);
}
} else if (codec != null) {
if (reader.getCurrentBsonType() == BsonType.NULL ) {
reader.readNull();
setFieldValue(instance, null);
}
else if (reader.getCurrentBsonType() == BsonType.UNDEFINED) {
reader.skipValue();
}
else {
F decoded = codec.decode(reader, decoderContext);
setFieldValue(instance, decoded);
}
}
}
示例12: decode
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@Override
public Map<K, V> decode(BsonReader reader, DecoderContext decoderContext) {
Map<K, V> map = newInstance();
if (BsonType.ARRAY.equals(reader.getCurrentBsonType())) {
reader.readStartArray();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
if (BsonType.DOCUMENT.equals(reader.getCurrentBsonType())) {
reader.readStartDocument();
reader.readName(); // don't need the key == "key"
K key = keyTypeCodec.decode(reader, decoderContext);
reader.readName(); // don't need the key == "value"
V value = valueTypeCodec.decode(reader, decoderContext);
map.put(key, value);
reader.readEndDocument();
} else {
LOGGER.warn("Expected {} from reader but got {}. Skipping value.", BsonType.DOCUMENT, reader.getCurrentBsonType());
reader.skipValue();
}
}
reader.readEndArray();
} else {
LOGGER.warn("Expected {} from reader but got {}. Skipping value.", BsonType.ARRAY, reader.getCurrentBsonType());
reader.skipValue();
}
return map;
}
示例13: decode
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@Override
public CustomType decode(BsonReader reader, DecoderContext decoderContext) {
reader.readStartDocument();
reader.readName("name");
String name = nameCodec.decode(reader, decoderContext);
reader.readName("list");
List<T> stringArrayList = listCodec.decode(reader, decoderContext);
reader.readName("innerType");
InnerType<T> innerType = innerTypeCodec.decode(reader, decoderContext);
reader.readEndDocument();
CustomType<T> customType = new CustomType(name);
customType.setInnerType(innerType);
customType.addAll(stringArrayList);
return customType;
}
示例14: decodeFields
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@Override
public T decodeFields(BsonReader reader, DecoderContext decoderContext, T instance) {
MetaData documentMeta = instance.getMeta();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String fieldName = reader.readName();
if ("data".equals(fieldName)) {
reader.readStartDocument();
super.decodeFields(reader, decoderContext, instance);
reader.readEndDocument();
} else {
MappedField field = documentMetaCodec.getMappedField(fieldName);
if (field != null) {
field.decode(reader, documentMeta, decoderContext);
} else {
reader.skipValue();
}
}
}
documentMetaCodec.postDecode(documentMeta);
return instance;
}
示例15: basicTest
import org.bson.codecs.DecoderContext; //导入依赖的package包/类
@Test
public void basicTest() {
BasePojo basePojo = new BasePojo();
basePojo.aString = STRING;
Codec<BasePojo> primitivePojoCodec = codecRegistry.get(BasePojo.class);
StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter, new JsonWriterSettings(true));
primitivePojoCodec.encode(writer, basePojo, EncoderContext.builder().build());
LOGGER.info("The encoded json looks like: {}", stringWriter);
BasePojo readBasePojo = primitivePojoCodec.decode(new JsonReader(stringWriter.toString()), DecoderContext.builder().build());
// assert that the modified version was actually written to the database
Assert.assertEquals(basePojo, readBasePojo);
Assert.assertEquals(MODIFIED_STRING, readBasePojo.aString);
}