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

software.amazon.awssdk.transfer.s3.internal.serialization.TransferManagerJsonUnmarshaller Maven / Gradle / Ivy

Go to download

The S3 Transfer Manager allows customers to easily and optimally transfer objects and directories to and from S3.

There is a newer version: 2.28.4
Show newest version
/*
 * Copyright 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 software.amazon.awssdk.transfer.s3.internal.serialization;

import static software.amazon.awssdk.transfer.s3.internal.serialization.TransferManagerMarshallingUtils.getUnmarshaller;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.core.SdkField;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.traits.MapTrait;
import software.amazon.awssdk.core.traits.TraitType;
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
import software.amazon.awssdk.utils.BinaryUtils;
import software.amazon.awssdk.utils.DateUtils;

/**
 * Interface for unmarshalling a field from JSON.
 *
 * @param  Type to unmarshall into.
 */
@FunctionalInterface
@SdkInternalApi
public interface TransferManagerJsonUnmarshaller {

    TransferManagerJsonUnmarshaller STRING = (val, sdkField) -> val;
    TransferManagerJsonUnmarshaller SHORT = (val, sdkField) -> Short.parseShort(val);
    TransferManagerJsonUnmarshaller INTEGER = (val, sdkField) -> Integer.parseInt(val);
    TransferManagerJsonUnmarshaller LONG = (val, sdkField) -> Long.parseLong(val);
    TransferManagerJsonUnmarshaller NULL = (val, sdkField) -> null;
    TransferManagerJsonUnmarshaller FLOAT = (val, sdkField) -> Float.parseFloat(val);
    TransferManagerJsonUnmarshaller DOUBLE = (val, sdkField) -> Double.parseDouble(val);
    TransferManagerJsonUnmarshaller BIG_DECIMAL = (val, sdkField) -> new BigDecimal(val);
    TransferManagerJsonUnmarshaller BOOLEAN = (val, sdkField) -> Boolean.parseBoolean(val);
    TransferManagerJsonUnmarshaller SDK_BYTES =
        (content, sdkField) -> SdkBytes.fromByteArray(BinaryUtils.fromBase64(content));

    TransferManagerJsonUnmarshaller INSTANT = new TransferManagerJsonUnmarshaller() {
        @Override
        public Instant unmarshall(String value, SdkField field) {
            if (value == null) {
                return null;
            }
            return safeParseDate(DateUtils::parseUnixTimestampInstant).apply(value);
        }

        private Function safeParseDate(Function dateUnmarshaller) {
            return value -> {
                try {
                    return dateUnmarshaller.apply(value);
                } catch (NumberFormatException e) {
                    throw SdkClientException.builder()
                                            .message("Unable to parse date : " + value)
                                            .cause(e)
                                            .build();
                }
            };
        }
    };

    TransferManagerJsonUnmarshaller> MAP = new TransferManagerJsonUnmarshaller>() {

        @Override
        public Map unmarshall(JsonNode jsonContent, SdkField field) {
            if (jsonContent == null) {
                return null;
            }

            SdkField valueInfo = field.getTrait(MapTrait.class, TraitType.MAP_TRAIT).valueFieldInfo();

            Map map = new HashMap<>();
            jsonContent.asObject().forEach((fieldName, value) -> {
                TransferManagerJsonUnmarshaller unmarshaller = getUnmarshaller(valueInfo.marshallingType());
                map.put(fieldName, unmarshaller.unmarshall(value));
            });
            return map;
        }

        @Override
        public Map unmarshall(String content, SdkField field) {
            return unmarshall(JsonNode.parser().parse(content), field);
        }
    };

    TransferManagerJsonUnmarshaller> LIST_INT = new TransferManagerJsonUnmarshaller>() {
        @Override
        public List unmarshall(JsonNode jsonContent, SdkField field) {
            if (jsonContent == null) {
                return null;
            }
            return jsonContent.asArray().stream().map(INTEGER::unmarshall).collect(Collectors.toList());
        }

        @Override
        public List unmarshall(String content, SdkField field) {
            return unmarshall(JsonNode.parser().parse(content), field);
        }
    };

    default T unmarshall(JsonNode jsonContent, SdkField field) {
        return jsonContent != null && !jsonContent.isNull() ? unmarshall(jsonContent.text(), field) : null;
    }

    default T unmarshall(JsonNode jsonContent) {
        return unmarshall(jsonContent, null);
    }

    T unmarshall(String content, SdkField field);

}