forked from PingPlusPlus/pingpp-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPingppObject.java
More file actions
41 lines (35 loc) · 1 KB
/
PingppObject.java
File metadata and controls
41 lines (35 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.pingplusplus.model;
import java.lang.reflect.Field;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public abstract class PingppObject {
public static final Gson PRETTY_PRINT_GSON = new GsonBuilder().
setPrettyPrinting().
serializeNulls().
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).
create();
@Override public String toString() {
// return String.format(
// "<%s@%s id=%s> JSON: %s",
// this.getClass().getName(),
// System.identityHashCode(this),
// this.getIdString(),
// PRETTY_PRINT_GSON.toJson(this));
return PRETTY_PRINT_GSON.toJson(this);
}
private Object getIdString() {
try {
Field idField = this.getClass().getDeclaredField("id");
return idField.get(this);
} catch (SecurityException e) {
return "";
} catch (NoSuchFieldException e) {
return "";
} catch (IllegalArgumentException e) {
return "";
} catch (IllegalAccessException e) {
return "";
}
}
}