Java Code Examples for org.bson.BsonType#NULL

The following examples show how to use org.bson.BsonType#NULL . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CoreStitchAuth.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Performs a request against Stitch using the provided {@link StitchAuthRequest} object, and
 * decodes the JSON body of the response into a T value as specified by the provided class type.
 * The type will be decoded using the codec found for T in the codec registry given.
 * If the provided type is not supported by the codec registry to be used, the method will throw
 * a {@link org.bson.codecs.configuration.CodecConfigurationException}.
 *
 * @param stitchReq     the request to perform.
 * @param resultClass   the class that the JSON response should be decoded as.
 * @param codecRegistry the codec registry used for de/serialization.
 * @param <T>           the type into which the JSON response will be decoded into.
 * @return the decoded value.
 */
public <T> T doAuthenticatedRequest(
    final StitchAuthRequest stitchReq,
    final Class<T> resultClass,
    final CodecRegistry codecRegistry
) {
  final Response response = doAuthenticatedRequest(stitchReq);

  try {
    final String bodyStr = IoUtils.readAllToString(response.getBody());
    final JsonReader bsonReader = new JsonReader(bodyStr);

    // We must check this condition because the decoder will throw trying to decode null
    if (bsonReader.readBsonType() == BsonType.NULL) {
      return null;
    }

    final CodecRegistry newReg =
            CodecRegistries.fromRegistries(BsonUtils.DEFAULT_CODEC_REGISTRY, codecRegistry);
    return newReg.get(resultClass).decode(bsonReader, DecoderContext.builder().build());
  } catch (final Exception e) {
    throw new StitchRequestException(e, StitchRequestErrorCode.DECODING_ERROR);
  }
}
 
Example 2
Source File: MorphiaMapPropertyCodecProvider.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public Map<K, V> decode(final BsonReader reader, final DecoderContext context) {
    reader.readStartDocument();
    Map<K, V> map = getInstance();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        final K key = (K) Conversions.convert(reader.readName(), keyType);
        if (reader.getCurrentBsonType() == BsonType.NULL) {
            map.put(key, null);
            reader.readNull();
        } else {
            map.put(key, codec.decode(reader, context));
        }
    }
    reader.readEndDocument();
    return map;
}
 
Example 3
Source File: AbstractJsonCodec.java    From vertx-mongo-client with Apache License 2.0 6 votes vote down vote up
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;
  }
}
 
Example 4
Source File: EnumCodec.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
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 new Error(format("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;
    }
}
 
Example 5
Source File: CoreStitchAuth.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Performs a request against Stitch using the provided {@link StitchAuthRequest} object,
 * and decodes the response using the provided result decoder.
 *
 * @param stitchReq The request to perform.
 * @return The response to the request, successful or not.
 */
public <T> T doAuthenticatedRequest(final StitchAuthRequest stitchReq,
                                    final Decoder<T> resultDecoder) {
  final Response response = doAuthenticatedRequest(stitchReq);
  try {
    final String bodyStr = IoUtils.readAllToString(response.getBody());
    final JsonReader bsonReader = new JsonReader(bodyStr);

    // We must check this condition because the decoder will throw trying to decode null
    if (bsonReader.readBsonType() == BsonType.NULL) {
      return null;
    }
    return resultDecoder.decode(bsonReader, DecoderContext.builder().build());
  } catch (final Exception e) {
    throw new StitchRequestException(e, StitchRequestErrorCode.DECODING_ERROR);
  }
}
 
Example 6
Source File: BsonReaderWrapper.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
public Long readLong(String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.INT32) {
        return (long) reader.readInt32();
    } else if (currentType == BsonType.INT64) {
        return reader.readInt64();
    } else {
        logger.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example 7
Source File: BsonReaderWrapper.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
public String readString(String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.STRING) {
        return reader.readString();
    } else {
        logger.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example 8
Source File: BsonReaderWrapper.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
public Double readDouble(String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.DOUBLE) {
        return reader.readDouble();
    } else {
        logger.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example 9
Source File: BsonReaderWrapper.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
public Boolean readBoolean(String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.BOOLEAN) {
        return reader.readBoolean();
    } else {
        logger.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example 10
Source File: BsonReaderWrapper.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
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<>();
}
 
Example 11
Source File: BsonReaderWrapper.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
public Map<String, ?> startReadMap(String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    }
    if (currentType != BsonType.DOCUMENT) {
        logger.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
    return new LinkedHashMap<>();
}
 
Example 12
Source File: BsonReaderWrapper.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: LocalDateTimeCodec.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
static LocalDateTime read(BsonReader reader, String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.DATE_TIME) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(reader.readDateTime()), ZoneId.systemDefault());
    } else {
        LOGGER.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example 14
Source File: BsonReaderWrapper.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
public ObjectId readObjectId(String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.OBJECT_ID) {
        return reader.readObjectId();
    } else {
        logger.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example 15
Source File: ZonedDateTimeCodec.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
static ZonedDateTime read(BsonReader reader, String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.DATE_TIME) {
        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(reader.readDateTime()), ZoneId.systemDefault());
    } else {
        LOGGER.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example 16
Source File: BsonReaderWrapper.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
public Integer readInteger(String field) {
    BsonType currentType = reader.getCurrentBsonType();
    if (currentType == BsonType.NULL) {
        reader.readNull();
        return null;
    } else if (currentType == BsonType.INT32) {
        return reader.readInt32();
    } else {
        logger.warn("unexpected field type, field={}, type={}", field, currentType);
        reader.skipValue();
        return null;
    }
}
 
Example 17
Source File: BsonReader.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Override
public void nextNull() throws IOException {
  BsonType type = delegate.getCurrentBsonType();
  if (type == BsonType.NULL) {
    delegate.readNull();
  } else if (type == BsonType.UNDEFINED) {
    delegate.readUndefined();
  } else {
    throw new IllegalStateException(String.format("Expected bson type %s or %s but got %s", BsonType.NULL, BsonType.UNDEFINED, type));
  }
}
 
Example 18
Source File: OptionalProvider.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Override
public final O decode(BsonReader reader, DecoderContext decoderContext) {
  if (reader.getCurrentBsonType() == BsonType.NULL) {
    reader.readNull();
    return empty();
  }
  return nullable(delegate.decode(reader, decoderContext));
}
 
Example 19
Source File: ArrayCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
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.BINARY && BsonBinarySubType.isUuid(reader.peekBinarySubType()) && reader.peekBinarySize() == 16) {
        return mapper.getCodecRegistry().get(UUID.class).decode(reader, decoderContext);
    }
    return mapper.getCodecRegistry().get(type.getComponentType()).decode(reader, decoderContext);
}
 
Example 20
Source File: CollectionCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<T> decode(final BsonReader reader, final DecoderContext context) {
    Collection<T> collection = getInstance();
    reader.readStartArray();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        if (reader.getCurrentBsonType() == BsonType.NULL) {
            collection.add(null);
            reader.readNull();
        } else {
            collection.add(codec.decode(reader, context));
        }
    }
    reader.readEndArray();
    return collection;
}