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

org.apache.inlong.sort.filesystem.shaded.com.amazonaws.protocol.json.internal.SimpleTypeJsonMarshallers Maven / Gradle / Ivy

There is a newer version: 1.13.0
Show newest version
/*
 * Copyright 2011-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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 com.amazonaws.protocol.json.internal;

import com.amazonaws.annotation.SdkInternalApi;
import com.amazonaws.internal.SdkInternalList;
import com.amazonaws.internal.SdkInternalMap;
import com.amazonaws.protocol.MarshallLocation;
import com.amazonaws.protocol.MarshallingInfo;
import com.amazonaws.protocol.StructuredPojo;
import com.amazonaws.protocol.json.StructuredJsonGenerator;
import com.amazonaws.protocol.json.StructuredJsonMarshaller;
import com.amazonaws.util.TimestampFormat;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.List;
import java.util.Map;

@SdkInternalApi
public class SimpleTypeJsonMarshallers {

    /**
     * If the marshallingInfo is null, we are in a container (not a top level member) and should preserve
     * JSON null.
     *
     * Otherwise, we're at the top level and if it's an explicit payload member (which also means that
     * marshallLocationName is null) the member should be rendered using the empty body marshaller.
     *
     * In other cases, do nothing.
     */
    public static final JsonMarshaller NULL = new JsonMarshaller() {
        @Override
        public void marshall(Void val, JsonMarshallerContext context, MarshallingInfo marshallingInfo) {
            if (marshallingInfo == null) {
                context.jsonGenerator().writeNull();
            }
            else if (marshallingInfo.isExplicitPayloadMember()) {
                if (marshallingInfo.marshallLocationName() != null) {
                    throw new IllegalStateException("Expected marshalling location name to be null if explicit member is null");
                }
                context.emptyBodyJsonMarshaller().marshall(context.jsonGenerator());
            }
        }
    };

    public static final JsonMarshaller STRING = new BaseJsonMarshaller() {
        @Override
        public void marshall(String val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {
            jsonGenerator.writeValue(val);
        }
    };

    public static final JsonMarshaller INTEGER = new BaseJsonMarshaller() {
        @Override
        public void marshall(Integer val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {
            jsonGenerator.writeValue(val);
        }
    };

    public static final JsonMarshaller LONG = new BaseJsonMarshaller() {
        @Override
        public void marshall(Long val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {
            jsonGenerator.writeValue(val);
        }
    };

    public static final JsonMarshaller SHORT = new BaseJsonMarshaller() {
        @Override
        public void marshall(Short val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {
            jsonGenerator.writeValue(val);
        }
    };

    public static final JsonMarshaller FLOAT = new BaseJsonMarshaller() {
        @Override
        public void marshall(Float val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {
            jsonGenerator.writeValue(val);
        }
    };

    public static final JsonMarshaller BIG_DECIMAL = new BaseJsonMarshaller() {
        @Override
        public void marshall(BigDecimal val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {
            jsonGenerator.writeValue(val);
        }
    };

    public static final JsonMarshaller DOUBLE = new BaseJsonMarshaller() {
        @Override
        public void marshall(Double val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {
            jsonGenerator.writeValue(val);
        }
    };

    public static final JsonMarshaller BOOLEAN = new BaseJsonMarshaller() {
        @Override
        public void marshall(Boolean val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {
            jsonGenerator.writeValue(val);
        }
    };

    public static final JsonMarshaller DATE = new BaseJsonMarshaller() {
        @Override
        public void marshall(Date val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {

            TimestampFormat timestampFormat = TimestampFormat.UNIX_TIMESTAMP;
            if (marshallingInfo != null && marshallingInfo.timestampFormat() != null) {
                timestampFormat = marshallingInfo.timestampFormat();
            }
            jsonGenerator.writeValue(val, timestampFormat);
        }
    };

    public static final JsonMarshaller BYTE_BUFFER = new BaseJsonMarshaller() {
        @Override
        public void marshall(ByteBuffer val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {
            jsonGenerator.writeValue(val);
        }
    };

    public static final JsonMarshaller STRUCTURED = new BaseJsonMarshaller() {
        @Override
        public void marshall(StructuredPojo val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {
            jsonGenerator.writeStartObject();
            val.marshall(context.protocolHandler());
            jsonGenerator.writeEndObject();

        }
    };

    public static final JsonMarshaller LIST = new BaseJsonMarshaller() {
        @Override
        public void marshall(List list, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo marshallingInfo) {
            jsonGenerator.writeStartArray();
            for (Object listValue : list) {
                context.marshall(MarshallLocation.PAYLOAD, listValue);
            }
            jsonGenerator.writeEndArray();
        }

        @Override
        protected boolean shouldEmit(List list) {
            return !(list.isEmpty() && list instanceof SdkInternalList && ((SdkInternalList) list).isAutoConstruct());
        }
    };

    /**
     * Marshalls a Map as a JSON object where each key becomes a field.
     */
    public static final JsonMarshaller MAP = new BaseJsonMarshaller() {
        @Override
        public void marshall(Map map, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                             MarshallingInfo mapMarshallingInfo) {
            jsonGenerator.writeStartObject();
            for (Map.Entry entry : ((Map) map).entrySet()) {
                if (entry.getValue() != null) {
                    final Object value = entry.getValue();
                    jsonGenerator.writeFieldName(entry.getKey());
                    context.marshall(MarshallLocation.PAYLOAD, value);
                }
            }
            jsonGenerator.writeEndObject();
        }

        @Override
        protected boolean shouldEmit(Map map) {
            return !(map.isEmpty() && map instanceof SdkInternalMap && ((SdkInternalMap) map).isAutoConstruct());
        }
    };

    /**
     * Adapt a {@link StructuredJsonMarshaller} to a {@link JsonMarshaller}. {@link JsonMarshaller} has a lot of internal
     * stuff so we don't want to expose all that across module boundaries.
     *
     * @param toAdapt Marshaller to adapt.
     * @param      Type of thing being marshalled.
     * @return Adapted marshaller.
     */
    public static  JsonMarshaller adapt(final StructuredJsonMarshaller toAdapt) {
        return new BaseJsonMarshaller() {
            @Override
            public void marshall(T val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
                                 MarshallingInfo marshallingInfo) {
                toAdapt.marshall(val, jsonGenerator);
            }
        };
    }

    /**
     * Base marshaller that emits the field name if present. The field name may be null in cases like
     * marshalling something inside a list or if the object is the explicit payload member.
     *
     * @param  Type to marshall.
     */
    private abstract static class BaseJsonMarshaller implements JsonMarshaller {

        @Override
        public final void marshall(T val, JsonMarshallerContext context, MarshallingInfo marshallingInfo) {
            if (!shouldEmit(val)) {
                return;
            }
            if (marshallingInfo != null && marshallingInfo.marshallLocationName() != null) {
                context.jsonGenerator().writeFieldName(marshallingInfo.marshallLocationName());
            }
            marshall(val, context.jsonGenerator(), context, marshallingInfo);
        }

        public abstract void marshall(T val, StructuredJsonGenerator jsonGenerator, JsonMarshallerContext context,
        MarshallingInfo marshallingInfo);

        /**
         * Hook to prevent emitting the field name. Used for maps and lists since we treat empty auto constructed
         * containers as null.
         */
        protected boolean shouldEmit(T val) {
            return true;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy