Skip to content

Commit

Permalink
Support for Jackson serialization in Mongo Adapter.
Browse files Browse the repository at this point in the history
Add separate JsonParser (for reading) and JsonGenerator (for writing) which delegates
streaming operations to existing BsonReader and BsonWriter (from mongo java driver).

Add custom CodecRegistry which instantiates Codec(s) based on
existing ObjectMapper (serializers / deserializers). All jackson
specific classes are located in `bson4jackson` package.

Move Gson specfic classes into separate package (`bson4gson`).

This feature is currently considered experimental (marked `@Beta`).

Closes #835
  • Loading branch information
asereda-gs committed Oct 18, 2018
1 parent 3d438d1 commit 6fc46ae
Show file tree
Hide file tree
Showing 14 changed files with 1,084 additions and 19 deletions.
16 changes: 16 additions & 0 deletions mongo/pom.xml
Expand Up @@ -90,5 +90,21 @@
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.11</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.11.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>2.8.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -14,11 +14,11 @@
limitations under the License.
*/

package org.immutables.mongo.bson4gson;
package org.immutables.mongo;

/**
* Allows retrieving delegates to access non-standard methods which
* are usually not exposed by standard interface.
* are usually not exposed by common interface.
*/
public interface Wrapper<T> {

Expand Down
1 change: 1 addition & 0 deletions mongo/src/org/immutables/mongo/bson4gson/BsonReader.java
Expand Up @@ -21,6 +21,7 @@
import org.bson.AbstractBsonReader;
import org.bson.AbstractBsonReader.State;
import org.bson.BsonType;
import org.immutables.mongo.Wrapper;

import javax.annotation.concurrent.NotThreadSafe;
import java.io.IOException;
Expand Down
9 changes: 3 additions & 6 deletions mongo/src/org/immutables/mongo/bson4gson/BsonWriter.java
Expand Up @@ -17,6 +17,7 @@

import com.google.gson.internal.LazilyParsedNumber;
import org.bson.types.Decimal128;
import org.immutables.mongo.Wrapper;

import javax.annotation.concurrent.NotThreadSafe;
import java.io.Closeable;
Expand Down Expand Up @@ -176,9 +177,7 @@ public com.google.gson.stream.JsonWriter value(Number value) throws IOException
final BigDecimal decimal = (BigDecimal) value;
try {
return value(new Decimal128(decimal));
} catch (NumberFormatException|AssertionError ex) {
// Decimal128 throws AssertionError instead of NumberFormatException for out of range values
// see https://jira.mongodb.org/browse/JAVA-2937
} catch (NumberFormatException ex) {
// fallback to serializing to string
return value(decimal.toPlainString());
}
Expand All @@ -189,9 +188,7 @@ public com.google.gson.stream.JsonWriter value(Number value) throws IOException
// BigDecimal is a wrapper for BigInteger anyway
BigDecimal decimal = new BigDecimal(integer);
return value(new Decimal128(decimal));
} catch (NumberFormatException|AssertionError ex) {
// Decimal128 throws AssertionError instead of NumberFormatException for out of range values
// see https://jira.mongodb.org/browse/JAVA-2937
} catch (NumberFormatException ex) {
// fallback to serializing to string
return value(integer.toString());
}
Expand Down
Expand Up @@ -36,9 +36,9 @@
* <a href="https://github.com/google/gson">Gson</a> standard classes like
* {@link TypeAdapter} / {@link Codec}(s).
*/
public final class Codecs {
public final class GsonCodecs {

private Codecs() {
private GsonCodecs() {
}

/**
Expand Down
182 changes: 182 additions & 0 deletions mongo/src/org/immutables/mongo/bson4jackson/BsonGenerator.java
@@ -0,0 +1,182 @@
/*
Copyright 2018 Immutables Authors and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.immutables.mongo.bson4jackson;

import com.fasterxml.jackson.core.Base64Variant;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.base.GeneratorBase;
import com.google.common.base.Preconditions;
import org.bson.BsonWriter;
import org.bson.types.Decimal128;
import org.immutables.mongo.Wrapper;

import javax.annotation.concurrent.NotThreadSafe;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
* Delegates all streaming API to {@link BsonWriter}.
*/
@NotThreadSafe
public class BsonGenerator extends GeneratorBase implements Wrapper<BsonWriter> {

private final BsonWriter writer;

BsonGenerator(int jsonFeatures, ObjectCodec codec, BsonWriter writer) {
super(jsonFeatures, codec);
this.writer = Preconditions.checkNotNull(writer, "writer");
}

@Override
public void writeStartArray() throws IOException {
writer.writeStartArray();
}

@Override
public void writeEndArray() throws IOException {
writer.writeEndArray();
}

@Override
public void writeStartObject() throws IOException {
writer.writeStartDocument();
}

@Override
public void writeEndObject() throws IOException {
writer.writeEndDocument();
}

@Override
public void writeFieldName(String name) throws IOException {
writer.writeName(name);
}

@Override
public void writeString(String text) throws IOException {
writer.writeString(text);
}

@Override
public void writeString(char[] text, int offset, int len) throws IOException {
writer.writeString(new String(text, offset, len));
}

@Override
public void writeRawUTF8String(byte[] text, int offset, int length) throws IOException {
writer.writeString(new String(text, offset, length));
}

@Override
public void writeUTF8String(byte[] text, int offset, int length) throws IOException {
writer.writeString(new String(text, offset, length));
}

@Override
public void writeRaw(String text) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void writeRaw(String text, int offset, int len) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void writeRaw(char[] text, int offset, int len) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void writeRaw(char c) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void writeBinary(Base64Variant bv, byte[] data, int offset, int len) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void writeNumber(int number) throws IOException {
writer.writeInt32(number);
}

@Override
public void writeNumber(long number) throws IOException {
writer.writeInt64(number);
}

@Override
public void writeNumber(BigInteger number) throws IOException {
writeNumber(new BigDecimal(number));
}

@Override
public void writeNumber(double number) throws IOException {
writer.writeDouble(number);
}

@Override
public void writeNumber(float number) throws IOException {
writer.writeDouble(number);
}

@Override
public void writeNumber(BigDecimal number) throws IOException {
try {
writer.writeDecimal128(new Decimal128(number));
} catch (NumberFormatException e) {
writer.writeString(number.toString());
}
}

@Override
public void writeNumber(String encodedValue) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void writeBoolean(boolean state) throws IOException {
writer.writeBoolean(state);
}

@Override
public void writeNull() throws IOException {
writer.writeNull();
}

@Override
public void flush() throws IOException {
writer.flush();
}

@Override
protected void _releaseBuffers() {

}

@Override
protected void _verifyValueWrite(String typeMsg) throws IOException {

}

@Override
public BsonWriter unwrap() {
return writer;
}
}

0 comments on commit 6fc46ae

Please sign in to comment.