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

software.amazon.awssdk.iot.ShadowStateFactory Maven / Gradle / Ivy

There is a newer version: 1.23.0
Show newest version
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

package software.amazon.awssdk.iot;

import software.amazon.awssdk.iot.iotshadow.model.ShadowState;

import java.io.IOException;
import java.util.HashMap;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

/**
 * Factory class for converting ShadowStates to and from packet payloads
 */
public class ShadowStateFactory implements TypeAdapterFactory {

    /**
     * Creates a new TypeAdapter for conversion to and from packet payloads
     */
    public  TypeAdapter create(Gson gson, TypeToken type) {

        Class rawType = (Class)type.getRawType();
        if (rawType != ShadowState.class) {
            return null;
        }

        final TypeAdapter delegate = gson.getDelegateAdapter(this, type);

        return new TypeAdapter() {

            /**
             * Writes the type to the packet payload (JsonWriter)
             * @param out The JsonWriter to output the type data to
             * @param shadowValue The shadow value containing the data to convert
             */
            public void write(JsonWriter out, T shadowValue) throws IOException {
                // Are null values present? If so, we need to process this differently
                ShadowState shadow = (ShadowState)shadowValue;
                if (shadow.desiredIsNullable == true || shadow.reportedIsNullable == true) {
                    out.setSerializeNulls(true);

                    // If a property is null but null is not valid for it, then just send an empty HashMap
                    if (shadow.desired == null && shadow.desiredIsNullable == false) {
                        shadow.desired = new HashMap();
                    }
                    if (shadow.reported == null && shadow.reportedIsNullable == false) {
                        shadow.reported = new HashMap();
                    }

                    delegate.write(out, shadowValue);
                    out.setSerializeNulls(false);
                }
                else
                {
                    delegate.write(out, shadowValue);
                }
            }

            /**
             * Reads the type from the packet payload (JsonReader)
             * @param in The JsonReader containing the packet payload data
             * @return The type created from the packet payload data
             */
            public T read(JsonReader in) throws IOException {
                T returnType = delegate.read(in);
                return returnType;
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy