mongovalues.JsObjCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mongo-values Show documentation
Show all versions of mongo-values Show documentation
Set of codecs to add support for json-values in the Java MongoDB driver, abstracting the process of
decoding a BSON into a JsObj and encoding a JsObj into a BSON.
The newest version!
package mongovalues;
import jsonvalues.JsObj;
import jsonvalues.JsObjPair;
import jsonvalues.JsValue;
import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonWriter;
import org.bson.codecs.BsonTypeClassMap;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.types.ObjectId;
class JsObjCodec extends JsonCodec implements Codec {
public JsObjCodec(final CodecRegistry registry,
final BsonTypeClassMap bsonTypeClassMap
) {
super(registry,
bsonTypeClassMap
);
}
@Override
public JsObj decode(final BsonReader reader,
final DecoderContext context
) {
JsObj obj = JsObj.empty();
reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String fieldName = reader.readName();
obj = obj.set(fieldName,
readValue(reader,
context
)
);
}
reader.readEndDocument();
return obj;
}
@Override
public void encode(final BsonWriter writer,
final JsObj obj,
final EncoderContext context
) {
if (obj.containsKey("$oid")) {
writer.writeObjectId(new ObjectId(obj.getStr("$oid")));
}
else {
writer.writeStartDocument();
encodeObj(writer,
obj,
context
);
writer.writeEndDocument();
}
}
private void encodeObj(final BsonWriter writer,
final JsObj obj,
final EncoderContext context
) {
for (final JsObjPair entry : obj) {
writer.writeName(entry.key());
@SuppressWarnings("unchecked")
Codec codec = (Codec) registry.get(entry.value().getClass());
if (codec == null) throw new IllegalStateException("No codec were found for " + entry.value().getClass());
context.encodeWithChildContext(codec,
writer,
entry.value()
);
}
}
@Override
public Class getEncoderClass() {
return JsObj.class;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy