-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryObjectIdSerializer.java
35 lines (25 loc) · 1.02 KB
/
BinaryObjectIdSerializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package org.tkassembled.example.bson.serialize;
import static org.apache.commons.codec.binary.Hex.encodeHexString;
import java.io.IOException;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class BinaryObjectIdSerializer extends StdSerializer<ObjectId> {
private static final Logger logger = LoggerFactory.getLogger(BinaryObjectIdSerializer.class);
public BinaryObjectIdSerializer() {
super(ObjectId.class);
}
@Override
public void serialize(ObjectId value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonGenerationException {
byte[] bytes = value.toByteArray();
logger.debug("Writing {}-byte binary ObjectId: {}, hex: {}", bytes.length, bytes,
encodeHexString(bytes));
jgen.writeBinary(bytes);
}
}