| 8749 |
amit.gupta |
1 |
package in.shop2020.storage.mongo.adapters;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.metamodel.core.BulletDataObject;
|
|
|
4 |
|
|
|
5 |
import java.lang.reflect.Type;
|
|
|
6 |
|
|
|
7 |
import com.google.gson.JsonDeserializationContext;
|
|
|
8 |
import com.google.gson.JsonDeserializer;
|
|
|
9 |
import com.google.gson.JsonElement;
|
|
|
10 |
import com.google.gson.JsonObject;
|
|
|
11 |
import com.google.gson.JsonParseException;
|
|
|
12 |
import com.google.gson.JsonPrimitive;
|
|
|
13 |
import com.google.gson.JsonSerializationContext;
|
|
|
14 |
import com.google.gson.JsonSerializer;
|
|
|
15 |
|
|
|
16 |
public class BDOAdapter implements JsonSerializer<BulletDataObject>, JsonDeserializer<BulletDataObject> {
|
|
|
17 |
@Override
|
|
|
18 |
public JsonElement serialize(BulletDataObject src, Type typeOfSrc, JsonSerializationContext context) {
|
|
|
19 |
JsonObject result = new JsonObject();
|
|
|
20 |
result.add("type", new JsonPrimitive(src.getClass().getName()));
|
|
|
21 |
result.add("properties", context.serialize(src, src.getClass()));
|
|
|
22 |
|
|
|
23 |
return result;
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
@Override
|
|
|
27 |
public BulletDataObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
|
|
|
28 |
throws JsonParseException {
|
|
|
29 |
JsonObject jsonObject = json.getAsJsonObject();
|
|
|
30 |
String type = jsonObject.get("type").getAsString();
|
|
|
31 |
JsonElement element = jsonObject.get("properties");
|
|
|
32 |
|
|
|
33 |
try {
|
|
|
34 |
return context.deserialize(element, Class.forName(type));
|
|
|
35 |
} catch (ClassNotFoundException cnfe) {
|
|
|
36 |
throw new JsonParseException("Unknown element type: " + type, cnfe);
|
|
|
37 |
}
|
|
|
38 |
}
|
|
|
39 |
}
|