use of com.google.gson.JsonElement in project Smartcity-Smarthouse by TechnionYP5777.
the class Savable method populate.
/**
* Populates the object with data from the jsonString.
* <p>
* Only fields without {@link Expose} Annotation will be populated.
* <p>
* <code>Populate</code> will be called recursively for fields that also
* implement {@link Savable}<br>
* (not including collections).
* <p>
* Override this function in order to define what to do after
* populating.<br>
* You should still call <code>Savable.super.populate(jsonString);</code>
*
* @param jsonString
* @throws Exception
*/
default default void populate(final String jsonString) throws Exception {
for (final Entry<String, JsonElement> e : new JsonParser().parse(jsonString).getAsJsonObject().entrySet()) {
final Field f = getClass().getDeclaredField(e.getKey());
f.setAccessible(true);
if (Savable.class.isAssignableFrom(f.getType()))
((Savable) f.get(this)).populate(e.getValue() + "");
else
f.set(this, gsonBuilder.create().fromJson(e.getValue(), f.getGenericType()));
}
}
use of com.google.gson.JsonElement in project groupme-utils by TheGuyWithTheFace.
the class GroupDeserializer method deserialize.
@Override
public Group deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject json = getResponse(jsonElement).getAsJsonObject();
GroupBuilder g = new GroupBuilder();
g.setId(json.get("id").getAsString());
g.setName(json.get("name").getAsString());
g.setType(json.get("type").getAsString());
g.setDescription(json.get("description").getAsString());
if (!json.get("image_url").isJsonNull()) {
g.setImageUrl(json.get("image_url").getAsString());
}
g.setCreatorUserId(json.get("creator_user_id").getAsString());
long createTime = json.get("created_at").getAsLong();
g.setCreatedAt(Instant.ofEpochSecond(createTime));
long updateTime = json.get("updated_at").getAsLong();
g.setUpdatedAt(Instant.ofEpochSecond(updateTime));
if (!json.get("share_url").isJsonNull()) {
g.setShareUrl(json.get("share_url").getAsString());
}
// Get members of the group
JsonArray response = json.get("members").getAsJsonArray();
int numMembers = response.size();
Member[] members = new Member[numMembers];
for (int i = 0; i < numMembers; i++) {
JsonElement elem = response.get(i);
members[i] = context.deserialize(elem, Member.class);
}
g.setMembers(members);
// TODO this will be reworked later, see note in Group Class.
JsonObject messages = json.get("messages").getAsJsonObject();
g.setMessageCount(messages.get("count").getAsInt());
g.setLastMessageId(messages.get("last_message_id").getAsString());
long lastCreate = messages.get("last_message_created_at").getAsLong();
g.setLastMessageCreatedAt(Instant.ofEpochSecond(lastCreate));
return g.createGroup();
}
use of com.google.gson.JsonElement in project groupme-utils by TheGuyWithTheFace.
the class GroupArrayDeserializer method deserialize.
public Group[] deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonArray response = getResponse(jsonElement).getAsJsonArray();
// Deserialize one group at a time, add to new groups array.
int numGroups = response.size();
Group[] groups = new Group[numGroups];
for (int i = 0; i < numGroups; i++) {
JsonElement elem = response.get(i);
groups[i] = context.deserialize(elem, Group.class);
}
return groups;
}
use of com.google.gson.JsonElement in project Engine by VoltzEngine-Project.
the class RenderJsonProcessor method process.
@Override
public RenderData process(JsonElement element) {
final JsonObject object = element.getAsJsonObject();
ensureValuesExist(object, "contentID", "states", "type");
String contentID = object.get("contentID").getAsString();
String overAllRenderType = object.get("type").getAsString();
RenderData data;
if (overAllRenderType.equalsIgnoreCase("tile")) {
data = new TileRenderData(this, contentID, overAllRenderType);
if (object.has("tileClass")) {
try {
((TileRenderData) data).tileClass = (Class<? extends TileEntity>) Class.forName(object.get("tileClass").getAsString());
} catch (Exception e) {
throw new IllegalArgumentException("Failed to load class for name '" + object.get("tileClass").getAsString() + "'");
}
}
} else {
data = new RenderData(this, contentID, overAllRenderType);
}
JsonArray array = object.get("states").getAsJsonArray();
for (JsonElement arrayElement : array) {
if (arrayElement instanceof JsonObject) {
JsonObject renderStateObject = arrayElement.getAsJsonObject();
//For loop handling for the lazy
if (renderStateObject.has("for")) {
renderStateObject = renderStateObject.getAsJsonObject("for");
ensureValuesExist(renderStateObject, "start", "end", "state");
int start = renderStateObject.getAsJsonPrimitive("start").getAsInt();
int end = renderStateObject.getAsJsonPrimitive("end").getAsInt();
if (start >= end) {
throw new IllegalArgumentException("Start can not be greater than or equal to end for a for loop.");
}
JsonObject template = renderStateObject.getAsJsonObject("state");
for (int i = start; i <= end; i++) {
JsonObject state = new JsonObject();
//Copy template and rename values as needed
for (Map.Entry<String, JsonElement> entry : template.entrySet()) {
if (entry.getValue() instanceof JsonPrimitive && ((JsonPrimitive) entry.getValue()).isString()) {
String s = entry.getValue().getAsString();
s = s.replace("%number%", "" + i);
state.add(entry.getKey(), new JsonPrimitive(s));
} else {
state.add(entry.getKey(), entry.getValue());
}
}
//Load state
handle(state, data, overAllRenderType);
}
} else {
handle(renderStateObject, data, overAllRenderType);
}
}
}
//Handle post calls
for (IRenderState state : data.renderStatesByName.values()) {
//Handles post processor actions
if (stateToProcessor.containsKey(state)) {
stateToProcessor.get(state).postHandle(state, data);
}
}
//Clear run data
stateToProcessor.clear();
//TODO ensure modelID exists if model state
return data;
}
use of com.google.gson.JsonElement in project Engine by VoltzEngine-Project.
the class JsonBlockListenerProcessor method process.
@Override
public void process(BlockBase block, JsonElement arrayElement, List<IJsonGenObject> objectList) {
JsonArray array = arrayElement.getAsJsonArray();
for (JsonElement element : array) {
String key = null;
JsonObject data = null;
if (element.isJsonPrimitive()) {
key = element.getAsString().toLowerCase();
} else if (element.isJsonObject()) {
JsonObject object = element.getAsJsonObject();
JsonProcessor.ensureValuesExist(object, "id");
key = object.get("id").getAsString().toLowerCase();
data = object;
}
if (builders.containsKey(key)) {
ITileEventListener listener = builders.get(key).createListener(block);
if (listener != null) {
if (data != null) {
if (!injectionMaps.containsKey(listener.getClass())) {
injectionMaps.put(listener.getClass(), new JsonProcessorInjectionMap(listener.getClass()));
}
JsonProcessorInjectionMap injectionMap = injectionMaps.get(listener.getClass());
for (Map.Entry<String, JsonElement> entry : data.entrySet()) {
injectionMap.handle(listener, entry.getKey(), entry.getValue());
}
}
block.addListener(listener);
}
}
}
}
Aggregations