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

com.microsoft.azure.iot.service.sdk.DeviceSerializer Maven / Gradle / Ivy

There is a newer version: 1.0.11
Show newest version
/*
 * Copyright (c) Microsoft. All rights reserved.
 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
 */

package com.microsoft.azure.iot.service.sdk;

import com.google.gson.*;
import com.microsoft.azure.iot.service.auth.SymmetricKey;

import java.lang.reflect.Type;

public class DeviceSerializer implements JsonSerializer
{
    private class Authentication
    {
        private SymmetricKey symmetricKey;

        public Authentication(SymmetricKey symmetricKey)
        {
            this.symmetricKey = symmetricKey;
        }
    }

    Gson gson = new Gson();
    @Override
    public JsonElement serialize(Device device, Type type, JsonSerializationContext jsonSerializationContext)
    {
        // CODES_SRS_SERVICE_SDK_JAVA_DEVICE_SERIALIZER_15_001: [The function shall serialize a Device object
        // into a JSON string recognized by the service]
        Authentication authentication = new Authentication(device.symmetricKey);
        JsonElement authenticationJsonElement = gson.toJsonTree(authentication, Authentication.class);
        JsonElement finalJson = gson.toJsonTree(device, type);
        finalJson.getAsJsonObject().add("authentication", authenticationJsonElement);

        return finalJson;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy