io.vertx.ext.mongo.impl.codec.json.JsonObjectCodec Maven / Gradle / Ivy
package io.vertx.ext.mongo.impl.codec.json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.bson.*;
import org.bson.codecs.CollectibleCodec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;
import org.bson.types.ObjectId;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;
/**
* @author Nick Scavelli
*/
public class JsonObjectCodec extends AbstractJsonCodec implements CollectibleCodec {
public static final String ID_FIELD = "_id";
public static final String DATE_FIELD = "$date";
public static final String BINARY_FIELD = "$binary";
public static final String TYPE_FIELD = "$type";
public static final String OID_FIELD = "$oid";
//https://docs.mongodb.org/manual/reference/mongodb-extended-json/#timestamp
public static final String TIMESTAMP_FIELD = "$timestamp";
public static final String TIMESTAMP_TIME_FIELD = "t";
public static final String TIMESTAMP_INCREMENT_FIELD = "i";
private boolean useObjectId = false;
public JsonObjectCodec(JsonObject config) {
useObjectId = config.getBoolean("useObjectId", false);
}
@Override
public JsonObject generateIdIfAbsentFromDocument(JsonObject json) {
if (!documentHasId(json)) {
String value = generateHexObjectId();
if (useObjectId) json.put(ID_FIELD, new JsonObject().put(OID_FIELD, value));
else json.put(ID_FIELD, value);
}
return json;
}
public static String generateHexObjectId() {
ObjectId id = new ObjectId();
return id.toHexString();
}
@Override
public boolean documentHasId(JsonObject json) {
return json.containsKey(ID_FIELD);
}
@Override
public BsonValue getDocumentId(JsonObject json) {
if (!documentHasId(json)) {
throw new IllegalStateException("The document does not contain an _id");
}
Object id = json.getValue(ID_FIELD);
if (id instanceof String) {
return new BsonString((String) id);
}
BsonDocument idHoldingDocument = new BsonDocument();
BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
writer.writeStartDocument();
writer.writeName(ID_FIELD);
writeValue(writer, null, id, EncoderContext.builder().build());
writer.writeEndDocument();
return idHoldingDocument.get(ID_FIELD);
}
@Override
public Class getEncoderClass() {
return JsonObject.class;
}
@Override
protected boolean isObjectIdInstance(Object instance) {
if (instance instanceof JsonObject && ((JsonObject) instance).containsKey(OID_FIELD)) {
return true;
}
return false;
}
@Override
protected void beforeFields(JsonObject object, BiConsumer objectConsumer) {
if (object.containsKey(ID_FIELD)) {
objectConsumer.accept(ID_FIELD, object.getValue(ID_FIELD));
}
}
@Override
protected JsonObject newObject() {
return new JsonObject();
}
@Override
protected void add(JsonObject object, String name, Object value) {
object.put(name, value);
}
@Override
protected boolean isObjectInstance(Object instance) {
return instance instanceof JsonObject;
}
@Override
protected void forEach(JsonObject object, BiConsumer objectConsumer) {
object.forEach(entry -> {
objectConsumer.accept(entry.getKey(), entry.getValue());
});
}
@Override
protected JsonArray newArray() {
return new JsonArray();
}
@Override
protected void add(JsonArray array, Object value) {
if (value != null) {
array.add(value);
} else {
array.addNull();
}
}
@Override
protected boolean isArrayInstance(Object instance) {
return instance instanceof JsonArray;
}
@Override
protected void forEach(JsonArray array, Consumer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy