Java 类org.bson.BsonType 实例源码

项目:polymorphia    文件:SpecialFieldsMapCodec.java   
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
    T map = newInstance();
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String key = reader.readName();

        Object value;
        Codec fieldMappingCodec = fieldMappingCodecs.get(key);
        if (fieldMappingCodec != null) {
            value = fieldMappingCodec.decode(reader, decoderContext);
        } else {
            value = readValue(reader, decoderContext);
        }

        map.put(key, value);
    }
    reader.readEndDocument();
    return map;
}
项目:polymorphia    文件:SimpleMapTypeCodec.java   
@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;
}
项目:polymorphia    文件:EnumCodec.java   
@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;
}
项目:BsonMapper    文件:BsonArrayConverter.java   
private Object handleArrayForBsonReader(BsonReader bsonReader, Field field, BsonMapperConfig bsonMapperConfig) {
    Class<?> fieldType = field.getType();
    ArrayList<Object> arrayList = new ArrayList<Object>();
    bsonReader.readStartArray();
    while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        BsonType currentBsonType = bsonReader.getCurrentBsonType();
        if (currentBsonType == BsonType.NULL) {
            continue;
        }
        if (currentBsonType == BsonType.ARRAY) {
            arrayList.add(decode(bsonReader, field, bsonMapperConfig));
        } else {
            Object javaValue;
            if (currentBsonType == BsonType.DOCUMENT) {
                javaValue = BsonValueConverterRepertory.getBsonDocumentConverter().decode(bsonReader, fieldType.getComponentType(), bsonMapperConfig);
            } else {
                javaValue = BsonValueConverterRepertory.getByteIOConverterByBsonType(currentBsonType).decode(bsonReader);
            }
            arrayList.add(javaValue);
        }
    }
    bsonReader.readEndArray();
    return arrayList.toArray((Object[]) Array.newInstance(fieldType.getComponentType(), 0));
}
项目:GitHub    文件:BsonReader.java   
/**
 * Gson library reads numbers lazily when using generic {@link com.google.gson.internal.bind.TypeAdapters#JSON_ELEMENT} type adapter.
 * Number is read as string and then wrapped inside {@link LazilyParsedNumber}. This inefficiency should only occur if reading numbers with generic JSON element API
 * and not using generated type adapters.
 *
 * @see LazilyParsedNumber
 * @see com.google.gson.internal.bind.TypeAdapters#JSON_ELEMENT
 */
private String scalarToString() throws IOException {
  final BsonType type = delegate.getCurrentBsonType();

  if (type == BsonType.STRING) {
    return delegate.readString();
  } else if (type == BsonType.SYMBOL) {
    return delegate.readSymbol();
  } else if (type == BsonType.INT32) {
    return Integer.toString(nextInt());
  } else if (type == BsonType.INT64) {
    return Long.toString(nextLong());
  } else if (type == BsonType.DOUBLE) {
    return Double.toString(nextDouble());
  }

  throw new IllegalStateException(String.format("Unknown scalar type to be converted to string: %s", type));
}
项目:polymorphia    文件:MappedField.java   
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);
        }
    }
}
项目:polymorphia    文件:ComplexMapTypeCodec.java   
@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;
}
项目:polymorphia    文件:CodecResolverTest.java   
@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;
}
项目:otus-api    文件:UserCodecTest.java   
@Test
public void method_decode_should_evocate_functions_that_reade_user_attributes() throws Exception {
    DecoderContext decoderContext = DecoderContext.builder().build();
    PowerMockito.when(reader.readString("password")).thenReturn(PASSWORD);
    PowerMockito.when(reader.readString("phone")).thenReturn(PHONE);
    PowerMockito.when(reader.readBoolean("enable")).thenReturn(ENABLE);
    PowerMockito.when(reader.readBoolean("extraction")).thenReturn(EXTRACTIONENABLED);
    PowerMockito.when(reader.readString("surname")).thenReturn(SURNAME);
    PowerMockito.when(reader.readString("name")).thenReturn(NAME);
    PowerMockito.when(reader.readBoolean("adm")).thenReturn(ADMIN);
    PowerMockito.when(reader.readString("uuid")).thenReturn(UUIDString);
    PowerMockito.when(reader.readString("email")).thenReturn(EMAIL);
    PowerMockito.when(reader.readString("acronym")).thenReturn(FIELDCENTERACRONYM);
    PowerMockito.when(reader.readBsonType()).thenReturn(BsonType.END_OF_DOCUMENT);
    doThrow(new BsonInvalidOperationException("teste")).when(reader).readNull("fieldCenter");
    doThrow(new BsonInvalidOperationException("teste")).when(reader).readNull("extraction");
    User userLocal = UserCodec.decode(reader,  decoderContext);
    assertEquals(userLocal.getEmail(),EMAIL);
    assertEquals(userLocal.getPassword(),PASSWORD);
    assertEquals(userLocal.getPhone(),PHONE);
    assertEquals(userLocal.isEnable(),ENABLE);
    assertEquals(userLocal.isExtractionEnabled(),EXTRACTIONENABLED);
    assertEquals(userLocal.getName(),NAME);
    assertEquals(userLocal.getFieldCenter().getAcronym(),FIELDCENTERACRONYM);
    assertEquals(userLocal.getUuid().toString(),UUID.fromString(UUIDString).toString());
}
项目:mongo-obj-framework    文件:AbstractBooleanCodec.java   
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
    final BsonType type = reader.getCurrentBsonType();
    switch(type) {
    case INT32:
        return decode(reader.readInt32() != 0);
    case NULL:
        return null;
    case STRING:
        return decode(Boolean.parseBoolean(reader.readString()));
    case BOOLEAN:
        return decode(reader.readBoolean());
        //$CASES-OMITTED$
    default:
        final String errMsg = format("Invalid date type, found: %s", type);
        throw new BsonInvalidOperationException(errMsg); 
    }
}
项目:mongo-obj-framework    文件:AbstractTimeCodec.java   
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
    final BsonType type = reader.getCurrentBsonType();
    switch(type) {
    case DATE_TIME:
        return decode(reader.readDateTime());
    case DOUBLE:
        return decode((long) reader.readDouble());
    case INT32:
        return decode(reader.readInt32());
    case INT64:
        return decode(reader.readInt64());
    case NULL:
        return null;
    case STRING:
        return decode(Long.valueOf(reader.readString()));
        //$CASES-OMITTED$
    default:
        final String errMsg = format("Invalid date type, found: %s", type);
        throw new BsonInvalidOperationException(errMsg); 
    }
}
项目:java-code-gen    文件:TestBeanCodec.java   
@Override
public TestBean decode(BsonReader reader, DecoderContext decoderContext) {
    reader.readStartDocument();
    TestBean pojo = new TestBean();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        switch (reader.readName()) {
            case "stringField":
                pojo.setStringField(reader.getCurrentBsonType() != BsonType.NULL ? reader.readString() : null);
                break;
            case "longField":
                pojo.setLongField(reader.readInt64());
                break;
            case "listStringField":
                pojo.setListStringField(this.decodeGenericList(reader, decoderContext, String.class));
                break;
            case "setStringField":
                pojo.setSetStringField(this.decodeGenericSet(reader, decoderContext, String.class));
                break;
            default:
                Assert.fail("Unexpected name " + reader.getCurrentName());
        }
    }
    reader.readEndDocument();
    return pojo;
}
项目:core-ng-project    文件:EnumCodec.java   
public T read(BsonReader reader, String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.STRING) {
        String enumValue = reader.readString();
        T value = decodingMappings.get(enumValue);
        if (value == null) throw Exceptions.error("can not decode value to enum, enumClass={}, value={}", enumClass.getCanonicalName(), enumValue);
        return value;
    } else {
        logger.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
项目:bsoneer    文件:DefaultReader.java   
@SuppressWarnings("unchecked")
public <U> U readValue(final BsonReader reader,
        final DecoderContext decoderContext) {
    BsonType bsonType = reader.getCurrentBsonType();
    if (bsonType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (bsonType == BsonType.ARRAY) {
        return (U) readList(reader, decoderContext);
    } else if (bsonType == BsonType.BINARY) {
        byte bsonSubType = reader.peekBinarySubType();
        if (bsonSubType == BsonBinarySubType.UUID_STANDARD.getValue()
                || bsonSubType == BsonBinarySubType.UUID_LEGACY.getValue()) {
            return (U) registry.get().get(UUID.class).decode(reader,
                    decoderContext);
        }
    }
    return (U) registry.get().get(bsonTypeClassMap.get(bsonType)).decode(reader,
            decoderContext);
}
项目:bsoneer    文件:BaseBsoneerCodec.java   
/**
 * {@inhericDoc}
 */
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
    T instance = instantiate();
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String fieldName = reader.readName();
        BsoneeBaseSetter<T> bsoneeBaseSetter = settersByName.get(fieldName);
        if (bsoneeBaseSetter != null) {
            bsoneeBaseSetter.set(instance, reader, decoderContext);
        } else {
            // TODO Check options whether to throw an exception or not. By default we skip it :)
            logger.warn("No setter for " + fieldName);
            reader.skipValue();
        }
    }
    reader.readEndDocument();
    return instance;
}
项目:bsoncodec    文件:LocaleDocumentCodec.java   
@Override
public Locale decode(BsonReader reader, DecoderContext decoderContext) {
    reader.readStartDocument();

    String language = reader.readString(this.languageKey);

    String country = "";
    String variant = "";
    if (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        reader.readName();
        country = reader.readString();

        if (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            reader.readName();
            variant = reader.readString();
        }
    }

    reader.readEndDocument();
    return new Locale(language, country, variant);
}
项目:mongowg    文件:DefaultDomainCodec.java   
/**
 * {@inheritDoc}
 */
@Override
public DefaultDomain decode(BsonReader reader, DecoderContext decoderContext) {
    DefaultDomain domain = new DefaultDomain();

    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String name = reader.readName();
        if ("players".equals(name)) {
            reader.readStartArray();
            while (reader.readBsonType() != BsonType.END_OF_DOCUMENT)
                domain.addPlayer(UUID.fromString(reader.readString()));
            reader.readEndArray();
        } else if ("groups".equals(name)) {
            reader.readStartArray();
            while (reader.readBsonType() != BsonType.END_OF_DOCUMENT)
                domain.addGroup(reader.readString());
            reader.readEndArray();
        } else {
            reader.skipValue();
        }
    }
    reader.readEndDocument();

    return domain;
}
项目:mongowg    文件:BlockVector2DCodec.java   
/**
 * {@inheritDoc}
 */
@Override
public BlockVector2D decode(BsonReader reader, DecoderContext decoderContext) {
    int x = 0;
    int z = 0;

    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String name = reader.readName();
        if ("x".equals(name))
            x = reader.readInt32();
        else if ("z".equals(name))
            z = reader.readInt32();
        else
            reader.skipValue();
    }
    reader.readEndDocument();

    return new BlockVector2D(x, z);
}
项目:mongowg    文件:BlockVectorCodec.java   
/**
 * {@inheritDoc}
 */
@Override
public BlockVector decode(BsonReader reader, DecoderContext decoderContext) {
    int x = 0;
    int y = 0;
    int z = 0;

    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String name = reader.readName();
        if ("x".equals(name))
            x = reader.readInt32();
        else if ("y".equals(name))
            y = reader.readInt32();
        else if ("z".equals(name))
            z = reader.readInt32();
        else
            reader.skipValue();
    }
    reader.readEndDocument();

    return new BlockVector(x, y, z);
}
项目:bsoncodec-apt    文件:ScalarCodeGen.java   
@Override
public void addDecodeStatements(CodeGeneratorContext ctx) {
    FieldModel field = ctx.field();
    if (!hasParent()) {
        if (!field.disableDecodeNullCheck()) {
            ctx.builder().beginControlFlow("if (bsonType != $T.NULL)",
                    BsonType.class);
        }
    }

    this.delegate.addDecodeStatements(getType(), ctx);

    if (!hasParent()) {
        if (!field.disableDecodeNullCheck()) {
            ctx.builder().nextControlFlow("else").addStatement("reader.readNull()");
            if (!ctx.field().disableSetNullStatement()) {
                this.delegate.addSetNullStatements(getType(), ctx);
            }
            ctx.builder().endControlFlow();
        }
    }

}
项目:epcis    文件:MongoReaderUtil.java   
private static String convertBsonValueToString(BsonValue bsonValue) {
    BsonType type = bsonValue.getBsonType();
    String value = null;
    if (type == BsonType.STRING) {
        value = bsonValue.asString().getValue();
    } else if (type == BsonType.INT32) {
        value = String.valueOf(bsonValue.asInt32().getValue());
    } else if (type == BsonType.INT64) {
        value = String.valueOf(bsonValue.asInt64().getValue());
    } else if (type == BsonType.DOUBLE) {
        value = String.valueOf(bsonValue.asDouble().getValue());
    } else if (type == BsonType.BOOLEAN) {
        value = String.valueOf(bsonValue.asBoolean().getValue());
    }
    return value;
}
项目:vertx-mongo-client    文件:AbstractJsonCodec.java   
protected BsonType getBsonType(Object value) {
  if (value == null) {
    return BsonType.NULL;
  } else if (value instanceof Boolean) {
    return BsonType.BOOLEAN;
  } else if (value instanceof Double) {
    return BsonType.DOUBLE;
  } else if (value instanceof Integer) {
    return BsonType.INT32;
  } else if (value instanceof Long) {
    return BsonType.INT64;
  } else if (value instanceof String) {
    return BsonType.STRING;
  } else if (isObjectIdInstance(value)) {
    return BsonType.OBJECT_ID;
  } else if (isObjectInstance(value)) {
    return BsonType.DOCUMENT;
  } else if (isArrayInstance(value)) {
    return BsonType.ARRAY;
  } else {
    return null;
  }
}
项目:cherimodata    文件:ListCodec.java   
@Override
public List decode( final BsonReader reader, final DecoderContext decoderContext )
{
    reader.readStartArray();
    List list = new ArrayList();
    while ( reader.readBsonType() != BsonType.END_OF_DOCUMENT )
    {
        Object value;
        if ( reader.getCurrentBsonType() == BsonType.NULL )
        {
            reader.readNull();
            value = null;
        }
        else
        {
            value = registry.get( bsonTypeClassMap.get( reader.getCurrentBsonType() ) ).decode( reader,
                decoderContext );
        }
        list.add( value );
    }
    reader.readEndArray();
    return list;
}
项目:immutables    文件:BsonNumbersTest.java   
@Test
public void bigNumbers() throws Exception {
  JsonObject obj = new JsonObject();
  BigInteger bigInteger = new BigInteger(Long.toString(Long.MAX_VALUE)).multiply(new BigInteger("128"));
  obj.addProperty("bigInteger", bigInteger);
  BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(1024));
  obj.addProperty("bigDecimal", bigDecimal);

  BsonDocument bson = Jsons.toBson(obj);
  check(bson.get("bigInteger").getBsonType()).is(BsonType.DECIMAL128);
  check(bson.get("bigInteger").asDecimal128().decimal128Value().bigDecimalValue().toBigInteger()).is(bigInteger);
  check(bson.get("bigDecimal").getBsonType()).is(BsonType.DECIMAL128);
  check(bson.get("bigDecimal").asDecimal128().decimal128Value().bigDecimalValue()).is(bigDecimal);

  check(Jsons.toGson(bson)).is(obj);
}
项目:immutables    文件:BsonDecimal128Test.java   
@Test
public void write() throws Exception {
  JsonObject obj = new JsonObject();
  BigInteger bigInteger = new BigInteger(Long.toString(Long.MAX_VALUE)).multiply(new BigInteger("128"));
  obj.addProperty("bigInteger", bigInteger);

  BigDecimal bigDecimal = new BigDecimal(Long.MAX_VALUE).multiply(new BigDecimal(1024));
  obj.addProperty("bigDecimal", bigDecimal);

  BsonDocument doc = Jsons.toBson(obj);

  check(doc.get("bigInteger").getBsonType()).is(BsonType.DECIMAL128);
  check(doc.get("bigInteger").asDecimal128().decimal128Value().bigDecimalValue().toBigInteger()).is(bigInteger);

  check(doc.get("bigDecimal").getBsonType()).is(BsonType.DECIMAL128);
  check(doc.get("bigDecimal").asDecimal128().decimal128Value().bigDecimalValue()).is(bigDecimal);
}
项目:anycook-api    文件:StepDraftCodec.java   
@Override
public StepDraft decode(final BsonReader reader, final DecoderContext decoderContext) {
    final StepDraft stepDraft = new StepDraft();

    reader.readStartDocument();
    stepDraft.setId(reader.readInt32("id"));
    stepDraft.setText(reader.readString("text"));

    reader.readStartArray();
    final List<IngredientDraft> stepIngredients = new ArrayList<>();

    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        stepIngredients.add(ingredientDraftCodec.decode(reader, decoderContext));
    }

    stepDraft.setIngredients(stepIngredients);
    reader.readEndArray();

    reader.readEndDocument();

    return stepDraft;
}
项目:GitHub    文件:BsonReader.java   
@Override
public long nextLong() throws IOException {
  if (BsonType.INT32 == delegate.getCurrentBsonType()) {
    return delegate.readInt32();
  }
  return delegate.readInt64();
}
项目:GitHub    文件:BsonReader.java   
@Override
public int nextInt() throws IOException {
  if (BsonType.INT64 == delegate.getCurrentBsonType()) {
    return (int) delegate.readInt64();
  }
  return delegate.readInt32();
}
项目:GitHub    文件:BsonReaderTest.java   
@Test
public void gsonToBson() throws Exception {
    JsonObject obj = new JsonObject();
    obj.addProperty("boolean", true);
    obj.addProperty("int32", 32);
    obj.addProperty("int64", 64L);
    obj.addProperty("double", 42.42D);
    obj.addProperty("string", "foo");
    obj.add("null", JsonNull.INSTANCE);
    obj.add("array", new JsonArray());
    obj.add("object", new JsonObject());


    BsonDocument doc = new BsonDocument();
    TypeAdapters.JSON_ELEMENT.write(new BsonWriter(new BsonDocumentWriter(doc)), obj);

    check(doc.keySet()).notEmpty();

    check(doc.get("boolean").getBsonType()).is(BsonType.BOOLEAN);
    check(doc.get("boolean").asBoolean());
    check(doc.get("int32").getBsonType()).is(BsonType.INT32);
    check(doc.get("int32").asInt32().getValue()).is(32);
    check(doc.get("int64").getBsonType()).is(BsonType.INT64);
    check(doc.get("int64").asInt64().getValue()).is(64L);
    check(doc.get("double").getBsonType()).is(BsonType.DOUBLE);
    check(doc.get("double").asDouble().getValue()).is(42.42D);
    check(doc.get("string").getBsonType()).is(BsonType.STRING);
    check(doc.get("string").asString().getValue()).is("foo");
    check(doc.get("null").getBsonType()).is(BsonType.NULL);
    check(doc.get("null").isNull());
    check(doc.get("array").getBsonType()).is(BsonType.ARRAY);
    check(doc.get("array").asArray()).isEmpty();
    check(doc.get("object").getBsonType()).is(BsonType.DOCUMENT);
    check(doc.get("object").asDocument().keySet()).isEmpty();
}
项目:polymorphia    文件:PolymorphicCodec.java   
@Override
default T decode(BsonReader reader, DecoderContext decoderContext) {
    T newInstance;
    if (reader.getCurrentBsonType() == null || reader.getCurrentBsonType() == BsonType.DOCUMENT) {
        reader.readStartDocument();
        newInstance = decodeFields(reader, decoderContext, newInstance());
        reader.readEndDocument();
        return newInstance;
    } else {
        LOGGER.error("Expected to read document but reader is in state {}. Skipping value!", reader.getCurrentBsonType());
        reader.skipValue();
        return null;
    }
}
项目:polymorphia    文件:SpecialFieldsMapCodec.java   
private Object readValue(final BsonReader reader, final DecoderContext decoderContext) {
    BsonType bsonType = reader.getCurrentBsonType();
    if (bsonType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (bsonType == BsonType.ARRAY) {
        return readList(reader, decoderContext);
    } else if (bsonType == BsonType.BINARY && BsonBinarySubType.isUuid(reader.peekBinarySubType()) && reader.peekBinarySize() == 16) {
        return codecRegistry.get(UUID.class).decode(reader, decoderContext);
    }
    return bsonTypeCodecMap.get(bsonType).decode(reader, decoderContext);
}
项目:polymorphia    文件:SpecialFieldsMapCodec.java   
private List<Object> readList(final BsonReader reader, final DecoderContext decoderContext) {
    reader.readStartArray();
    List<Object> list = new ArrayList<>();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        list.add(readValue(reader, decoderContext));
    }
    reader.readEndArray();
    return list;
}
项目:polymorphia    文件:ArrayCodec.java   
@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
    List<Boolean> arrayList = new ArrayList();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        arrayList.add(reader.readBoolean());
    }
    boolean[] booleans = new boolean[arrayList.size()];
    int i = 0;
    for (boolean aPrimitiveBoolean : arrayList) {
        booleans[i++] = aPrimitiveBoolean;
    }
    return booleans;
}
项目:polymorphia    文件:ArrayCodec.java   
@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
    List<Character> arrayList = new ArrayList();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        arrayList.add((char) reader.readInt32());
    }
    char[] chars = new char[arrayList.size()];
    int i = 0;
    for (char aPrimitive : arrayList) {
        chars[i++] = aPrimitive;
    }
    return chars;
}
项目:polymorphia    文件:ArrayCodec.java   
@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
    List<Float> arrayList = new ArrayList();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        arrayList.add((float) reader.readDouble());
    }
    float[] floats = new float[arrayList.size()];
    int i = 0;
    for (float aPrimitive : arrayList) {
        floats[i++] = aPrimitive;
    }
    return floats;
}
项目:polymorphia    文件:ArrayCodec.java   
@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
    /**
     * Efficient way of decoding an int[] of unknown size
     * InstStream uses an primitive int buffer internally
     */
    IntStream.Builder builder = IntStream.builder();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        builder.add(reader.readInt32());
    }
    return builder.build().toArray();
}
项目:polymorphia    文件:ArrayCodec.java   
@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
    LongStream.Builder builder = LongStream.builder();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        builder.add(reader.readInt64());
    }
    return builder.build().toArray();
}
项目:polymorphia    文件:ArrayCodec.java   
@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
    List<Short> arrayList = new ArrayList();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        arrayList.add((short) reader.readInt32());
    }
    short[] shorts = new short[arrayList.size()];
    int i = 0;
    for (short aPrimitive : arrayList) {
        shorts[i++] = aPrimitive;
    }
    return shorts;
}
项目:polymorphia    文件:ArrayCodec.java   
@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
    List<Double> arrayList = new ArrayList();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        arrayList.add(reader.readDouble());
    }
    double[] doubles = new double[arrayList.size()];
    int i = 0;
    for (double aPrimitive : arrayList) {
        doubles[i++] = aPrimitive;
    }
    return doubles;
}
项目:polymorphia    文件:PojoContext.java   
public T decodeFields(BsonReader reader, DecoderContext decoderContext, T instance) {
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String fieldName = reader.readName();
        if ("enum".equals(fieldName)) {
            return codec.decode(reader, decoderContext);
        }
        else
        {
            reader.skipValue();
        }
    }
    // throw new EnumValueNotFoundException??? instead of returning null?
    return null;
}