
com.amazonaws.services.lambda.runtime.serialization.util.SerializeUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-lambda-java-serialization Show documentation
Show all versions of aws-lambda-java-serialization Show documentation
Serialization logic for the AWS Lambda Java Runtime
The newest version!
/* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
package com.amazonaws.services.lambda.runtime.serialization.util;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Scanner;
/**
* Class with Utilities for serializing and deserializing customer classes
*/
public class SerializeUtil {
/**
* cached of classes being loaded for faster reflect loading
*/
private static final HashMap cachedClasses = new HashMap<>();
/**
* converts an input stream to a string
* @param inputStream InputStream object
* @return String with stream contents
*/
public static String convertStreamToString(InputStream inputStream) {
Scanner s = new Scanner(inputStream).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
/**
* load a customer class
* @param className name of class to load
* @return Class object
*/
public static Class loadCustomerClass(String className, ClassLoader customerClassLoader) {
Class cachedClass = cachedClasses.get(className);
if (cachedClass == null) {
cachedClass = ReflectUtil.loadClass(customerClassLoader, className);
cachedClasses.put(className, cachedClass);
}
return cachedClass;
}
/**
* deserialize a joda datetime object
* Underneath the reflection, this method does the following:
*
* DateTime.parse(jsonParser.getValueAsString());
*
* @param dateTimeClass DateTime class
* @param dateTimeString string to deserialize from
* @param DateTime type
* @return DateTime instance
*/
public static T deserializeDateTime(Class dateTimeClass, String dateTimeString) {
Functions.R1 parseMethod =
ReflectUtil.loadStaticR1(dateTimeClass, "parse", true, dateTimeClass, String.class);
return parseMethod.call(dateTimeString);
}
/**
* serialize a DateTime object
* Underneath the reflection, this method does the following:
*
* DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
* jsonGenerator.writeString(formatter.print(customerDateTime)
*
* @param dateTime DateTime object to serialize
* @param DateTime type
* @param classLoader ClassLoader used to load DateTime classes
* @return timestamp as formatted string
*/
@SuppressWarnings({"unchecked"})
public static String serializeDateTime(T dateTime, ClassLoader classLoader) {
// Workaround not to let maven shade plugin relocating string literals https://issues.apache.org/jira/browse/MSHADE-156
Class dateTimeFormatterClass = loadCustomerClass("com.amazonaws.lambda.unshade.thirdparty.org.joda.time.format.DateTimeFormatter", classLoader);
Class dateTimeFormatClass = loadCustomerClass("com.amazonaws.lambda.unshade.thirdparty.org.joda.time.format.ISODateTimeFormat", classLoader);
Class readableInstantInterface = loadCustomerClass("com.amazonaws.lambda.unshade.thirdparty.org.joda.time.ReadableInstant", classLoader);
return serializeDateTimeHelper(dateTime, dateTimeFormatterClass, dateTimeFormatClass, readableInstantInterface);
}
/**
* Helper method to serialize DateTime objects (We need some way to define generics to get code to compile)
* @param dateTime DAteTime object
* @param dateTimeFormatterClass DAteTime formatter class
* @param dateTimeFormatClass DateTime ISO format class
* @param readableInstantInterface DateTime readable instant interface (Needed because reflection is type specific)
* @param DAteTime type
* @param DateTimeFormatter type
* @param DateTimeFormat type
* @param ReadableInstant type
* @return String with serialized date time
*/
private static String serializeDateTimeHelper(S dateTime, Class dateTimeFormatterClass,
Class dateTimeFormatClass,
Class readableInstantInterface) {
Functions.R0 dateTimeFormatterConstructor =
ReflectUtil.loadStaticR0(dateTimeFormatClass, "dateTime", true, dateTimeFormatterClass);
T dateTimeFormatter = dateTimeFormatterConstructor.call();
Functions.R1 printMethod =
ReflectUtil.bindInstanceR1(dateTimeFormatter, "print", true, String.class, readableInstantInterface);
return printMethod.call(dateTime);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy