@Override
  public com.google.gson.stream.JsonWriter value(Number value) throws IOException {
    if (value == null) {
      return nullValue();
    }
    if (value instanceof Double) {
      return value(value.doubleValue());
    }
    if (value instanceof Float) {
			return value(value.floatValue());
    }
    if (value instanceof Long){
      return value(value.longValue());
    }
    if (value instanceof Integer) {
      return value(value.intValue());
    }
    if (value instanceof Short) {
      return value(value.shortValue());
    }
    if (value instanceof Byte) {
     return value(value.byteValue());
    }
    if (value instanceof LazilyParsedNumber) {
      return value(value.longValue());
    }
    if (value instanceof BigDecimal) {
      final BigDecimal decimal = (BigDecimal) value;
      try {
        return value(new Decimal128(decimal));
      } catch (NumberFormatException ex) {
        // fallback to serializing to string
        return value(decimal.toPlainString());
      }
    }
    if (value instanceof BigInteger) {
      final BigInteger integer = (BigInteger) value;
      try {
        // BigDecimal is a wrapper for BigInteger anyway
        BigDecimal decimal = new BigDecimal(integer);
        return value(new Decimal128(decimal));
      } catch (NumberFormatException ex) {
        // fallback to serializing to string
        return value(integer.toString());
      }
    }
    // by default we resort to floating point 
    return value(value.doubleValue());
  }
Adapter of {@link com.google.gson.stream.JsonWriter GSON Writer} writing JSON documents in <a href="http://bsonspec.org/">BSON format</a>
It delegates most of the calls to {@link org.bson.BsonWriter} which can serialize document representation in BSON binary version (default wire protocol
between mongo server and client). This allows to write / persist immutable objects directly into binary, bypassing intermediate
object representation (typically Immutable -> DBObject-> byte[]} ). Generated Gson {@link com.google.gson.TypeAdapter} takes care of
reading the object and calling write methods as a stream API.

@see BsonReader
@see <a href="http://mongodb.github.io/mongo-java-driver/3.5/bson/">Mongo Driver (BSON)</a>
@see <a href="http://bsonspec.org/">BSON spec</a>
mongo/src/org/immutables/mongo/bson4gson/BsonWriter.java