private <T> JsonElement serializeAsElementOrArray( List<T> items, JsonSerializationContext context) { if (items.isEmpty()) { return null; } if (items.size() == 1) { return context.serialize(items.get(0)); } else { return context.serialize(items); } }
private void serializeBlock( SegmentBlock segment, JsonObject json, JsonSerializationContext context) { json.add("actions", serializeAsElementOrArray(segment.types, context)); json.addProperty("meta", segment.getMeta()); if (segment.clientUpdate != null) { JsonObject jsonUpdate = new JsonObject(); jsonUpdate.add("coords", context.serialize(segment.clientUpdate.relativeCoords)); json.add("clientUpdate", jsonUpdate); } }
private void serializeItem( SegmentItem segment, JsonObject json, JsonSerializationContext context) { json.add("actions", serializeAsElementOrArray(segment.types, context)); json.addProperty("isAdjacent", segment.isAdjacent); if (segment.clientUpdate != null) { JsonObject jsonUpdate = new JsonObject(); jsonUpdate.add("coords", context.serialize(segment.clientUpdate.relativeCoords)); jsonUpdate.addProperty("directional", segment.directionalClientUpdate); json.add("clientUpdate", jsonUpdate); } }
@Override public JsonElement serialize( Segment segment, Type typeOfSrc, JsonSerializationContext context) { JsonObject json = new JsonObject(); json.addProperty("class", segment.checkClass.getName()); if (segment instanceof SegmentSpecialBlock) { json.addProperty("type", "specialBlock"); serializeSpecialBlock((SegmentSpecialBlock) segment, json, context); } else { if (segment instanceof SegmentBlock) { json.addProperty("type", "block"); serializeBlock((SegmentBlock) segment, json, context); } else if (segment instanceof SegmentEntity) { json.addProperty("type", "entity"); serializeEntity((SegmentEntity) segment, json, context); } else if (segment instanceof SegmentItem) { json.addProperty("type", "item"); serializeItem((SegmentItem) segment, json, context); } else if (segment instanceof SegmentTileEntity) { json.addProperty("type", "tileEntity"); serializeTileEntity((SegmentTileEntity) segment, json, context); } json.add("flags", serializeAsElementOrArray(segment.flags, context)); if (segment.condition != null) { json.addProperty("condition", segment.condition.toString()); } if (segment.priority != Priority.NORMAL) { json.addProperty("priority", segment.priority.toString()); } for (Getter getter : segment.getters) { json.add(getter.getName(), context.serialize(getter, Getter.class)); } } return json; }