Skip to content
New issue

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

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

Already on GitHub? # to your account

add method RenderJson.getResponse() #1072

Merged
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
28 changes: 19 additions & 9 deletions framework/src/play/mvc/results/RenderJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,39 @@ public class RenderJson extends Result {
private static final Gson GSON = new Gson();

private final String json;
private final Object response;

public RenderJson(Object o) {
json = GSON.toJson(o);
public RenderJson(Object response) {
this.response = response;
json = GSON.toJson(response);
}

public RenderJson(Object o, Type type) {
json = GSON.toJson(o, type);
public RenderJson(Object response, Type type) {
this.response = response;
json = GSON.toJson(response, type);
}

public RenderJson(Object o, JsonSerializer<?>... adapters) {
public RenderJson(Object response, JsonSerializer<?>... adapters) {
this.response = response;
GsonBuilder gson = new GsonBuilder();
for (Object adapter : adapters) {
Type t = getMethod(adapter.getClass(), "serialize").getParameterTypes()[0];
gson.registerTypeAdapter(t, adapter);
}
json = gson.create().toJson(o);
json = gson.create().toJson(response);
}

public RenderJson(String jsonString) {
json = jsonString;
this.response = null;
}

public RenderJson(Object o, Gson gson) {
public RenderJson(Object response, Gson gson) {
this.response = response;
if (gson != null) {
json = gson.toJson(o);
json = gson.toJson(response);
} else {
json = GSON.toJson(o);
json = GSON.toJson(response);
}
}

Expand All @@ -63,6 +69,10 @@ public String getJson() {
return json;
}

public Object getResponse() {
return response;
}

private static Method getMethod(Class clazz, String methodName) {
Method bestMatch = null;
for (Method m : clazz.getDeclaredMethods()) {
Expand Down