001package ball.databind;
002/*-
003 * ##########################################################################
004 * Data Binding Utilities
005 * $Id: JSONBeanSerializerModifier.java 6118 2020-06-04 19:31:45Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-databind/trunk/src/main/java/ball/databind/JSONBeanSerializerModifier.java $
007 * %%
008 * Copyright (C) 2016 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import com.fasterxml.jackson.core.JsonGenerator;
024import com.fasterxml.jackson.databind.BeanDescription;
025import com.fasterxml.jackson.databind.JsonSerializer;
026import com.fasterxml.jackson.databind.SerializationConfig;
027import com.fasterxml.jackson.databind.SerializerProvider;
028import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
029import java.io.IOException;
030import lombok.NoArgsConstructor;
031import lombok.ToString;
032
033import static java.util.Objects.requireNonNull;
034
035/**
036 * {@link JSONBean} {@link BeanSerializerModifier} implementation.
037 *
038 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
039 * @version $Revision: 6118 $
040 */
041@NoArgsConstructor @ToString
042public class JSONBeanSerializerModifier extends BeanSerializerModifier {
043    @Override
044    public JsonSerializer<?> modifySerializer(SerializationConfig config,
045                                              BeanDescription description,
046                                              JsonSerializer<?> serializer) {
047        return ((JSONBean.class.isAssignableFrom(description.getBeanClass()))
048                    ? new SerializerImpl(serializer)
049                    : serializer);
050    }
051
052    @ToString
053    private class SerializerImpl extends JsonSerializer<Object> {
054        private final JsonSerializer<Object> serializer;
055
056        @SuppressWarnings("unchecked")
057        public SerializerImpl(JsonSerializer<?> serializer) {
058            super();
059
060            this.serializer =
061                (JsonSerializer<Object>) requireNonNull(serializer, "serializer");
062        }
063
064        @Override
065        public void serialize(Object value, JsonGenerator generator,
066                              SerializerProvider serializers) throws IOException {
067            if (value instanceof JSONBean && ((JSONBean) value).node != null) {
068                generator.writeTree(((JSONBean) value).node);
069            } else {
070                serializer.serialize(value, generator, serializers);
071            }
072        }
073    }
074}