com.microsoft.azure.iot.service.sdk.DeviceSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of iothub-java-service-client Show documentation
Show all versions of iothub-java-service-client Show documentation
The Microsoft Azure IoT Service SDK for Java
/*
* 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