Package net.vz.mongodb.jackson.internal.stream

Source Code of net.vz.mongodb.jackson.internal.stream.JacksonDBEncoder

/*
* Copyright 2011 VZ Netzwerke Ltd
*
* 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 net.vz.mongodb.jackson.internal.stream;

import com.mongodb.DBEncoder;
import com.mongodb.MongoException;
import de.undercouch.bson4jackson.BsonGenerator;
import net.vz.mongodb.jackson.MongoJsonMappingException;
import org.bson.BSONObject;
import org.bson.io.OutputBuffer;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

/**
* DBEncoder that uses bson4jackson if the object is a JacksonDBObject
*/
public class JacksonDBEncoder implements DBEncoder {
    private final ObjectMapper objectMapper;
    private final DBEncoder defaultDBEncoder;

    public JacksonDBEncoder(ObjectMapper objectMapper, DBEncoder defaultDBEncoder) {
        this.objectMapper = objectMapper;
        this.defaultDBEncoder = defaultDBEncoder;
    }

    public int writeObject(OutputBuffer buf, BSONObject object) {
        if (object instanceof JacksonDBObject) {
            Object actualObject = ((JacksonDBObject) object).getObject();
            OutputBufferOutputStream stream = new OutputBufferOutputStream(buf);
            BsonGenerator generator = new DBEncoderBsonGenerator(JsonGenerator.Feature.collectDefaults(), stream);
            try {
                objectMapper.writeValue(generator, actualObject);
                // The generator buffers everything so that it can write the number of bytes to the stream
                generator.close();
            } catch (JsonMappingException e) {
                throw new MongoJsonMappingException(e);
            } catch (IOException e) {
                throw new MongoException("Error writing object out", e);
            }
            return stream.getCount();
        } else {
            return defaultDBEncoder.writeObject(buf, object);
        }
    }
}
TOP

Related Classes of net.vz.mongodb.jackson.internal.stream.JacksonDBEncoder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.