Skip to content

Serialize output to JSON in Java #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions images/examples/java/src/main/java/example/Hello.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ArrayList<UserInfo> myHandlerList(ArrayList<UserInfo> arrayList, Context
return arrayList;
}

public static void myHandlerPOJO(RequestClass request, Context context){
System.out.println(String.format("Hello %s %s" , request.firstName, request.lastName));
public static String myHandlerPOJO(RequestClass request, Context context){
return String.format("Hello %s %s" , request.firstName, request.lastName);
}
}
13 changes: 9 additions & 4 deletions images/java/src/main/java/io/iron/lambda/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ private void runMethod(Method lambdaMethod, String payload, Class cls, String ha
if (!processed) {
System.err.println(String.format("Handler %s with simple, POJO, or IO(input/output) types not found", handlerName));
}

try {
String jsonInString = ClassTypeHelper.gson.toJson(result);
System.out.println(jsonInString);
}
catch (Exception e) {
System.out.println("{\"error\": \"" + ClassTypeHelper.gson.toJson(e.toString()) + "\"}");
System.exit(1);
}
}

private void launchMethod(String[] packageHandler, String payload) throws Exception {
Expand Down Expand Up @@ -195,10 +204,6 @@ private void launchMethod(String[] packageHandler, String payload) throws Except
});

Class returnType = matchedArray[0].getReturnType();
if (!returnType.getTypeName().equals("void")) {
System.err.println(String.format("Handler can only have 'void' return type. Found '%s'.", returnType.getTypeName()));
System.exit(1);
}
runMethod(matchedArray[0], payload, cls, handlerName);
}

Expand Down