All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mongovalues.JsArrayCodec Maven / Gradle / Ivy

Go to download

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.JsArray;
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;


class JsArrayCodec extends JsonCodec implements Codec {

    public JsArrayCodec(final CodecRegistry registry,
                        final BsonTypeClassMap bsonTypeClassMap) {
        super(registry, bsonTypeClassMap);
    }

    @Override
    public JsArray decode(final BsonReader reader,
                          final DecoderContext context) {
        reader.readStartArray();

        JsArray array = JsArray.empty();
        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            array = array.append(readValue(reader,
                    context));
        }

        reader.readEndArray();

        return array;
    }

    @Override
    public void encode(final BsonWriter writer,
                       final JsArray array,
                       final EncoderContext context) {

        writer.writeStartArray();

        for (JsValue value : array) {
            @SuppressWarnings("unchecked")
            Codec codec = (Codec) registry.get(value.getClass());
            if (codec == null) throw new IllegalStateException("No codec were found for " + value.getClass());
            context.encodeWithChildContext(codec,
                    writer,
                    value
            );
        }
        writer.writeEndArray();
    }

    @Override
    public Class getEncoderClass() {
        return JsArray.class;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy