Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for Jackson serialization in Mongo Adapter.
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
1 parent
3d438d1
commit 6fc46ae
Showing
14 changed files
with
1,084 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
mongo/src/org/immutables/mongo/bson4jackson/BsonGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.