Skip to content

Commit

Permalink
Update codegen service clients (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfod authored Mar 21, 2024
1 parent 99c5c45 commit f7dbe15
Show file tree
Hide file tree
Showing 44 changed files with 1,135 additions and 984 deletions.
16 changes: 16 additions & 0 deletions sdk/src/main/java/software/amazon/awssdk/iot/EnumSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,27 @@
* @param <E> the enumeration type the serializer should work with
*/
public class EnumSerializer<E> implements JsonSerializer<E>, JsonDeserializer<E> {

/**
* Serializes the given enum to a JsonElement
* @param enumValue The enum to convert
* @param typeOfEnum The enum to convert type
* @param context The JsonSerializationContext to use
* @return The enum as a JsonElement
*/
public JsonElement serialize(E enumValue, Type typeOfEnum, JsonSerializationContext context) {
return new JsonPrimitive(enumValue.toString());
}

private Method fromString;

/**
* Deserializes the JsonElement to an enum
* @param json The json to convert
* @param typeOfEnum The type of enum to convert to
* @param context The JsonDeserializationContext to use
* @return The enum from the JsonElement data
*/
public E deserialize(JsonElement json, Type typeOfEnum, JsonDeserializationContext context)
throws JsonParseException {
if (fromString == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* 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;
Expand All @@ -12,8 +17,14 @@
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 <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {

Class<T> rawType = (Class<T>)type.getRawType();
Expand All @@ -24,6 +35,12 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

return new TypeAdapter<T>() {

/**
* 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;
Expand All @@ -46,8 +63,13 @@ public void write(JsonWriter out, T shadowValue) throws IOException {
delegate.write(out, shadowValue);
}
}
public T read(JsonReader in) throws IOException {

/**
* 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;
}
Expand Down
27 changes: 26 additions & 1 deletion sdk/src/main/java/software/amazon/awssdk/iot/Timestamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,46 @@
import java.util.Date;

/**
* Extension of Java date class to support Json serialization. Used in IoT service models.
* Extension of Java date class to support Json serialization. Used in IoT service models.
*/
public class Timestamp extends java.util.Date {
/**
* Serializer to convert Timestamp to JSON
*/
public static class Serializer implements JsonSerializer<Timestamp> {
/**
* Serializes a Timestamp to JSON
* @param src The Timestamp to convert
* @param typeOfSrc The Type to use
* @param context The JsonSerializationContext to use
* @return A JsonElement containing the Timestamp data
*/
public JsonElement serialize(Timestamp src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.getTime() / 1000); // convert from ms to seconds
}
}

/**
* Deserializer to convert JSON to Timestamp
*/
public static class Deserializer implements JsonDeserializer<Timestamp> {
/**
* Deserializes JSON to a Timestamp
* @param json The JsonElement containing the Timestamp
* @param typeOfT The Type to use
* @param context The JsonDeserializationContext to use
* @return A Timestamp containing the data in the JsonElement
*/
public Timestamp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
return new Timestamp(new Date(json.getAsJsonPrimitive().getAsLong() * 1000));
}
}

/**
* Timestamp constructor
* @param date The date to use
*/
public Timestamp(Date date) {
super(date.getTime());
}
Expand Down
Loading

0 comments on commit f7dbe15

Please sign in to comment.