Package org.jongo.marshall.jackson

Source Code of org.jongo.marshall.jackson.JacksonMapperTest$DoeJsonSerializer

/*
* Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
*
* 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 org.jongo.marshall.jackson;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
import com.fasterxml.jackson.databind.module.SimpleDeserializers;
import com.mongodb.BasicDBObject;
import org.bson.types.ObjectId;
import org.jongo.Mapper;
import org.jongo.ObjectIdUpdater;
import org.jongo.bson.Bson;
import org.jongo.bson.BsonDocument;
import org.jongo.model.Friend;
import org.jongo.query.Query;
import org.jongo.query.QueryFactory;
import org.junit.Test;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;

public class JacksonMapperTest {

    @Test
    public void canAddDeserializer() throws Exception {

        BsonDocument document = Bson.createDocument(new BasicDBObject("name", "robert"));
        Mapper mapper = new JacksonMapper.Builder()
                .addDeserializer(String.class, new DoeJsonDeserializer())
                .build();

        Friend friend = mapper.getUnmarshaller().unmarshall(document, Friend.class);

        assertThat(friend.getName()).isEqualTo("Doe");
    }

    @Test
    public void canAddSerializer() throws Exception {

        Friend robert = new Friend("Robert");
        Mapper mapper = new JacksonMapper.Builder()
                .addSerializer(String.class, new DoeJsonSerializer())
                .build();

        BsonDocument document = mapper.getMarshaller().marshall(robert);

        assertThat(document.toString()).contains("{ \"name\" : \"Doe\"}");
    }

    @Test
    public void canSetVisibilityChecker() throws Exception {

        PojoWithGetter robert = new PojoWithGetter("Robert", "Sax");

        Mapper mapper = new JacksonMapper.Builder()
                .setVisibilityChecker(new VisibilityChecker.Std(JsonAutoDetect.Visibility.PUBLIC_ONLY).withFieldVisibility(JsonAutoDetect.Visibility.NONE))
                .build();

        BsonDocument document = mapper.getMarshaller().marshall(robert);

        assertThat(document.toString()).isEqualTo("{ \"firstName\" : \"Robert\"}");
    }


    @Test
    public void canAddModule() throws Exception {

        ObjectId oid = new ObjectId("504482e5e4b0d1b2c47fff66");
        Friend friend = new Friend(oid, "Robert");
        Mapper mapper = new JacksonMapper.Builder()
                .registerModule(new Module() {
                    @Override
                    public String getModuleName() {
                        return "test-module";
                    }

                    @Override
                    public Version version() {
                        return new Version(2, 0, 0, "", "org.jongo", "testmodule");
                    }

                    @Override
                    public void setupModule(SetupContext setupContext) {
                        SimpleDeserializers deserializers = new SimpleDeserializers();
                        deserializers.addDeserializer(String.class, new DoeJsonDeserializer());
                        setupContext.addDeserializers(deserializers);
                    }
                })
                .build();

        BsonDocument document = mapper.getMarshaller().marshall(friend);

        assertThat(document.toString()).contains("\"_id\" : { \"$oid\" : \"504482e5e4b0d1b2c47fff66\"}");
    }

    @Test
    public void canAddJongoInterfaces() throws Exception {

        ObjectIdUpdater objectIdUpdater = new ObjectIdUpdater() {
            public boolean mustGenerateObjectId(Object pojo) {
                return false;
            }

            public void setObjectId(Object newPojo, ObjectId id) {
            }

            public Object getId(Object pojo) {
                return null;
            }
        };

        QueryFactory factory = new QueryFactory() {
            public Query createQuery(String query, Object... parameters) {
                return null;
            }
        };
        Mapper mapper = new JacksonMapper.Builder()
                .withObjectIdUpdater(objectIdUpdater)
                .withQueryFactory(factory)
                .build();

        assertThat(mapper.getObjectIdUpdater()).isEqualTo(objectIdUpdater);
        assertThat(mapper.getQueryFactory()).isEqualTo(factory);
    }

    private static class DoeJsonSerializer extends JsonSerializer<String> {

        @Override
        public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
            jgen.writeString("Doe");
        }
    }

    private static class DoeJsonDeserializer extends JsonDeserializer<String> {
        @Override
        public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            return "Doe";
        }
    }

    private static class PojoWithGetter {
        private String firstName;
        private String lastName;

        private PojoWithGetter(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }
    }


}
TOP

Related Classes of org.jongo.marshall.jackson.JacksonMapperTest$DoeJsonSerializer

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.