private void mergeUpdate(JsonObject update, JsonObject master) { for (Map.Entry<String, JsonElement> attr : update.entrySet()) { if ((null == attr.getValue()) || attr.getValue().isJsonNull()) { // TODO use singleton to reduce memory footprint master.add(attr.getKey(), new JsonNull()); } assertCompatibility(master.get(attr.getKey()), attr.getValue()); if (attr.getValue() instanceof JsonPrimitive) { if (!master.has(attr.getKey()) || !master.get(attr.getKey()).equals(attr.getValue())) { master.add(attr.getKey(), attr.getValue()); } } else if (attr.getValue() instanceof JsonObject) { if (!master.has(attr.getKey()) || master.get(attr.getKey()).isJsonNull()) { // copy whole subtree master.add(attr.getKey(), attr.getValue()); } else { // recurse to merge attribute updates mergeUpdate( attr.getValue().getAsJsonObject(), master.get(attr.getKey()).getAsJsonObject()); } } else if (attr.getValue() instanceof JsonArray) { // TODO any way to correlate elements between arrays? will need concept of // element identity to merge elements master.add(attr.getKey(), attr.getValue()); } } }
/** * Gson invokes this call-back method during serialization when it encounters a field of the * specified type. * * <p> * * <p>In the implementation of this call-back method, you should consider invoking {@link * com.google.gson.JsonSerializationContext#serialize(Object, java.lang.reflect.Type)} method to * create JsonElements for any non-trivial field of the {@code wrappedStack} object. However, you * should never invoke it on the {@code wrappedStack} object itself since that will cause an * infinite loop (Gson will call your call-back method again). * * @param wrappedStack the object that needs to be converted to Json. * @param typeOfSrc the actual type (fully genericized version) of the source object. * @param context * @return a JsonElement corresponding to the specified object. */ @Override public JsonElement serialize( WrappedStack wrappedStack, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonWrappedStack = new JsonObject(); Gson gson = new Gson(); jsonWrappedStack.addProperty("type", wrappedStack.objectType); jsonWrappedStack.addProperty("stackSize", wrappedStack.stackSize); if (wrappedStack.wrappedStack instanceof ItemStack) { JsonItemStack jsonItemStack = new JsonItemStack(); jsonItemStack.itemName = Item.itemRegistry.getNameForObject(((ItemStack) wrappedStack.wrappedStack).getItem()); jsonItemStack.itemDamage = ((ItemStack) wrappedStack.wrappedStack).getItemDamage(); if (((ItemStack) wrappedStack.wrappedStack).stackTagCompound != null) { jsonItemStack.itemNBTTagCompound = ((ItemStack) wrappedStack.wrappedStack).stackTagCompound; } jsonWrappedStack.add( "objectData", JsonItemStack.jsonSerializer.toJsonTree(jsonItemStack, JsonItemStack.class)); } else if (wrappedStack.wrappedStack instanceof OreStack) { jsonWrappedStack.add( "objectData", gson.toJsonTree(wrappedStack.wrappedStack, OreStack.class)); } else if (wrappedStack.wrappedStack instanceof FluidStack) { JsonFluidStack jsonFluidStack = new JsonFluidStack((FluidStack) wrappedStack.wrappedStack); jsonWrappedStack.add( "objectData", JsonFluidStack.jsonSerializer.toJsonTree(jsonFluidStack, JsonFluidStack.class)); } return jsonWrappedStack; }
@Override public JsonElement serialize( WrappedStack wrappedStack, Type type, JsonSerializationContext context) { JsonObject jsonWrappedStack = new JsonObject(); Gson gsonWrappedStack = new Gson(); jsonWrappedStack.addProperty("className", wrappedStack.className); jsonWrappedStack.addProperty("stackSize", wrappedStack.stackSize); if (wrappedStack.wrappedStack instanceof ItemStack) { jsonWrappedStack.add( "wrappedStack", gsonWrappedStack.toJsonTree(wrappedStack.wrappedStack, ItemStack.class)); } else if (wrappedStack.wrappedStack instanceof OreStack) { jsonWrappedStack.add( "wrappedStack", gsonWrappedStack.toJsonTree(wrappedStack.wrappedStack, OreStack.class)); } else if (wrappedStack.wrappedStack instanceof EnergyStack) { jsonWrappedStack.add( "wrappedStack", gsonWrappedStack.toJsonTree(wrappedStack.wrappedStack, EnergyStack.class)); } else if (wrappedStack.wrappedStack instanceof FluidStack) { jsonWrappedStack.add( "wrappedStack", gsonWrappedStack.toJsonTree(wrappedStack.wrappedStack, FluidStack.class)); } return jsonWrappedStack; }
@Override public JsonElement serialize(Operator op, Type type, JsonSerializationContext jsc) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty(PlanExpressionConstants.CONTAINERNAME, op.containerName); jsonObject.addProperty(PlanExpressionConstants.OPERATOR, op.operator); jsonObject.addProperty(PlanExpressionConstants.OPERATORNAME, op.operatorName); if (op.queryString != null) { jsonObject.addProperty(PlanExpressionConstants.QUERYSTRING, op.queryString); } if (op.locations != null) { JsonParser parser = new JsonParser(); JsonArray locations = new JsonArray(); for (URL url : op.locations) { locations.add(parser.parse(url.toString())); } jsonObject.add(PlanExpressionConstants.LOCATIONS, locations); } if (op.paramList != null) { JsonParser parser = new JsonParser(); JsonArray params = new JsonArray(); for (Parameter param : op.paramList) { JsonArray p = new JsonArray(); p.add(parser.parse(param.name)); p.add(parser.parse(param.value)); params.add(p); } jsonObject.add(PlanExpressionConstants.PARAMETERS, params); } return jsonObject; }
@Override public JsonElement serialize(MoveStrategy src, Type typeOfSrc, JsonSerializationContext context) { JsonObject result = new JsonObject(); result.add("type", new JsonPrimitive(src.getClass().getSimpleName())); result.add("properties", context.serialize(src, src.getClass())); return result; }
@Override public JsonElement serialize( OutputDataRaw cellRaw, Type typeOfSrc, JsonSerializationContext context) { final JsonObject jsonObject = new JsonObject(); if (cellRaw.png != null) { jsonObject.addProperty("image/png", cellRaw.png); } if (cellRaw.html != null) { final JsonElement html = gson.toJsonTree(cellRaw.html); jsonObject.add("text/html", html); } if (cellRaw.svg != null) { final JsonElement svg = gson.toJsonTree(cellRaw.svg); jsonObject.add("image/svg+xml", svg); } if (cellRaw.jpeg != null) { final JsonElement jpeg = gson.toJsonTree(cellRaw.jpeg); jsonObject.add("image/jpeg", jpeg); } if (cellRaw.latex != null) { final JsonElement latex = gson.toJsonTree(cellRaw.latex); jsonObject.add("text/latex", latex); } if (cellRaw.text != null) { final JsonElement text = gson.toJsonTree(cellRaw.text); jsonObject.add("text/plain", text); } return jsonObject; }
@Override public JsonElement serialize(Throwable src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonObject = new JsonObject(); jsonObject.add("class", new JsonPrimitive(src.getClass().getName())); if (src.getMessage() != null) { jsonObject.add("message", new JsonPrimitive(src.getMessage())); } return jsonObject; }
public JsonObject toJson() { JsonObject res = new JsonObject(); JsonArray rows = new JsonArray(); for (int i = 0; i < _rows.length; ++i) rows.add(new JsonPrimitive(_rows[i])); JsonArray dist = new JsonArray(); for (int i = 0; i < _dist.length; ++i) dist.add(new JsonPrimitive(_dist[i])); res.add("rows_per_cluster", rows); res.add("sqr_error_per_cluster", dist); return res; }
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); } }
@Override public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationContext context) { JsonObject o = new JsonObject(); if (user.getUserId() != null) { o.addProperty("userid", user.getUserId()); } if (user.getName() != null) { o.addProperty("name", user.getName()); } if (user.getDepartIds() != null) { JsonArray jsonArray = new JsonArray(); for (Integer departId : user.getDepartIds()) { jsonArray.add(new JsonPrimitive(departId)); } o.add("department", jsonArray); } if (user.getPosition() != null) { o.addProperty("position", user.getPosition()); } if (user.getMobile() != null) { o.addProperty("mobile", user.getMobile()); } if (user.getGender() != null) { o.addProperty("gender", user.getGender().equals("男") ? 0 : 1); } if (user.getTel() != null) { o.addProperty("tel", user.getTel()); } if (user.getEmail() != null) { o.addProperty("email", user.getEmail()); } if (user.getWeiXinId() != null) { o.addProperty("weixinid", user.getWeiXinId()); } if (user.getAvatar() != null) { o.addProperty("avatar", user.getAvatar()); } if (user.getStatus() != null) { o.addProperty("status", user.getStatus()); } if (user.getExtAttrs().size() > 0) { JsonArray attrsJsonArray = new JsonArray(); for (WxCpUser.Attr attr : user.getExtAttrs()) { JsonObject attrJson = new JsonObject(); attrJson.addProperty("name", attr.getName()); attrJson.addProperty("value", attr.getValue()); attrsJsonArray.add(attrJson); } JsonObject attrsJson = new JsonObject(); attrsJson.add("attrs", attrsJsonArray); o.add("extattr", attrsJson); } return o; }
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); } }
private static JsonObject deepCopyObj(JsonObject from) { JsonObject result = new JsonObject(); for (Map.Entry<String, JsonElement> entry : from.entrySet()) { result.add(entry.getKey(), deepCopy(entry.getValue())); } return result; }
/** * Method to recursively convert a configuration tree, consisting of {@link * ConfigurationElement}s, to a to a Json tree, consisting of {@link JsonElement}s * * @param parentJsonObject {@link JsonObject} The parent of the new {@link JsonElement}s added in * this method call * @param parentConfigurationElement {@link ConfigurationElement} The parent of the * configurations, which will be converted to {@link JsonElement}s */ private void recConvertConfigurationTreeToJsonTree( JsonObject parentJsonObject, ConfigurationElement parentConfigurationElement) { for (ConfigurationElement configurationElement : parentConfigurationElement.getChildren()) { if (configurationElement.isConfigurationSection()) { JsonElement newJsonElement = new JsonObject(); parentJsonObject.add(configurationElement.getName(), newJsonElement); recConvertConfigurationTreeToJsonTree((JsonObject) newJsonElement, configurationElement); } else if (configurationElement.isConfigurationObject()) { if (configurationElement.getValue() instanceof JsonConfigurationValue) { JsonElement newJsonElement = ((JsonConfigurationValue) configurationElement.getValue()).getJsonElement(); parentJsonObject.add(configurationElement.getName(), newJsonElement); } } } }
private JsonObject transformGuysData(JsonArray array) { JsonObject result = new JsonObject(); for (JsonElement e : array) { String key = e.getAsJsonObject().get("sample").getAsString(); result.add(key, e.getAsJsonObject().get("Expression")); } return result; }
private final void Pair(JsonObject o) throws ParseException { JsonPrimitive property; JsonElement value; property = JsonString(); jj_consume_token(24); value = JsonValue(); o.add(property.getAsString(), value); }
@Override public JsonElement serialize( AssetReference asset, Type typeOfSrc, JsonSerializationContext context) { JsonElement jsonAsset = context.serialize(asset.asset, asset.asset.getClass()); GsonUtils.appendClassProperty(jsonAsset, asset.asset, context); JsonObject jsonObject = new JsonObject(); jsonObject.add("asset", jsonAsset); return jsonObject; }
@Override public JsonElement serialize( IpnbCellRaw cellRaw, Type typeOfSrc, JsonSerializationContext context) { final JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("cell_type", cellRaw.cell_type); if ("code".equals(cellRaw.cell_type)) { final Integer count = cellRaw.execution_count; if (count == null) { jsonObject.add("execution_count", JsonNull.INSTANCE); } else { jsonObject.addProperty("execution_count", count); } } if (cellRaw.metadata != null) { final JsonElement metadata = gson.toJsonTree(cellRaw.metadata); jsonObject.add("metadata", metadata); } if (cellRaw.level != null) { jsonObject.addProperty("level", cellRaw.level); } if (cellRaw.outputs != null) { final JsonElement outputs = gson.toJsonTree(cellRaw.outputs); jsonObject.add("outputs", outputs); } if (cellRaw.source != null) { final JsonElement source = gson.toJsonTree(cellRaw.source); jsonObject.add("source", source); } if (cellRaw.input != null) { final JsonElement input = gson.toJsonTree(cellRaw.input); jsonObject.add("input", input); } if (cellRaw.language != null) { jsonObject.addProperty("language", cellRaw.language); } if (cellRaw.prompt_number != null) { jsonObject.addProperty("prompt_number", cellRaw.prompt_number); } return jsonObject; }
@Override public JsonElement serialize( IpnbFileRaw fileRaw, Type typeOfSrc, JsonSerializationContext context) { final JsonObject jsonObject = new JsonObject(); if (fileRaw.worksheets != null) { final JsonElement worksheets = gson.toJsonTree(fileRaw.worksheets); jsonObject.add("worksheets", worksheets); } if (fileRaw.cells != null) { final JsonElement cells = gson.toJsonTree(fileRaw.cells); jsonObject.add("cells", cells); } final JsonElement metadata = gson.toJsonTree(fileRaw.metadata); jsonObject.add("metadata", metadata); jsonObject.addProperty("nbformat", fileRaw.nbformat); jsonObject.addProperty("nbformat_minor", fileRaw.nbformat_minor); return jsonObject; }
@Override public JsonElement serialize( DeviceIconType deviceIcon, Type typeOfSrc, JsonSerializationContext context) { JsonObject jsonDeviceIconType = new JsonObject(); for (Position.Status status : Position.Status.values()) { PositionIconType positionIcon = deviceIcon.getPositionIconType(status); JsonObject jsonPositionIconType = new JsonObject(); jsonPositionIconType.addProperty("width", positionIcon.getWidth()); jsonPositionIconType.addProperty("height", positionIcon.getHeight()); JsonArray urls = new JsonArray(); urls.add(new JsonPrimitive(positionIcon.getURL(false))); urls.add(new JsonPrimitive(positionIcon.getURL(true))); jsonPositionIconType.add("urls", urls); jsonDeviceIconType.add(status.name(), jsonPositionIconType); } return jsonDeviceIconType; }
static void modified() { try (FileWriter writer = new FileWriter(getFile())) { JsonObject obj = deepCopy(INSTANCE.everythingElse); obj.addProperty("clientToken", INSTANCE.clientToken); obj.add("authenticationDatabase", GSON.toJsonTree(INSTANCE.authenticationDatabase.profiles)); GSON.toJson(obj, writer); System.out.println("Profiles saved"); } catch (IOException e) { e.printStackTrace(); } }
@Override public JsonElement serialize( Sct013 sct013, Type type, JsonSerializationContext jsonSerializationContext) { JsonObject jo = new JsonObject(); SerializerUtil.serializeAbstractSensor(jo, sct013); // Spezifische Daten serialisieren jo.add("actualPower", new JsonPrimitive(sct013.getActualPower())); return jo; }
@Override public JsonElement serialize( WrappedStack wrappedStack, Type type, JsonSerializationContext context) { JsonObject jsonWrappedStack = new JsonObject(); Gson gsonWrappedStack = new Gson(); jsonWrappedStack.addProperty("className", wrappedStack.className); jsonWrappedStack.addProperty("stackSize", wrappedStack.stackSize); if (wrappedStack.wrappedStack instanceof ItemStack) { JsonItemStack jsonItemStack = new JsonItemStack(); jsonItemStack.itemID = ((ItemStack) wrappedStack.wrappedStack).itemID; jsonItemStack.itemDamage = ((ItemStack) wrappedStack.wrappedStack).getItemDamage(); jsonItemStack.stackSize = ((ItemStack) wrappedStack.wrappedStack).stackSize; if (((ItemStack) wrappedStack.wrappedStack).stackTagCompound != null) { try { jsonItemStack.compressedStackTagCompound = CompressedStreamTools.compress( ((ItemStack) wrappedStack.wrappedStack).stackTagCompound); } catch (IOException e) { e.printStackTrace(); } } jsonWrappedStack.add( "wrappedStack", gsonWrappedStack.toJsonTree(jsonItemStack, JsonItemStack.class)); } else if (wrappedStack.wrappedStack instanceof OreStack) { jsonWrappedStack.add( "wrappedStack", gsonWrappedStack.toJsonTree(wrappedStack.wrappedStack, OreStack.class)); } else if (wrappedStack.wrappedStack instanceof EnergyStack) { jsonWrappedStack.add( "wrappedStack", gsonWrappedStack.toJsonTree(wrappedStack.wrappedStack, EnergyStack.class)); } else if (wrappedStack.wrappedStack instanceof FluidStack) { jsonWrappedStack.add( "wrappedStack", gsonWrappedStack.toJsonTree(wrappedStack.wrappedStack, FluidStack.class)); } return jsonWrappedStack; }
@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; }
@Override protected Response serve() { JsonObject json = new JsonObject(); JsonArray succ = new JsonArray(); JsonArray fail = new JsonArray(); String bucket = _bucket.value(); AmazonS3 s3 = PersistS3.getClient(); ObjectListing currentList = s3.listObjects(bucket); processListing(currentList, succ, fail); while (currentList.isTruncated()) { currentList = s3.listNextBatchOfObjects(currentList); processListing(currentList, succ, fail); } json.add(NUM_SUCCEEDED, new JsonPrimitive(succ.size())); json.add(SUCCEEDED, succ); json.add(NUM_FAILED, new JsonPrimitive(fail.size())); json.add(FAILED, fail); DKV.write_barrier(); Response r = Response.done(json); r.setBuilder(SUCCEEDED + "." + KEY, new KeyCellBuilder()); return r; }
private static JsonElement dumpConfig(Table<String, String, IConfigPropertyHolder> properties) { JsonObject result = new JsonObject(); Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().serializeNulls().create(); for (Map.Entry<String, Map<String, IConfigPropertyHolder>> category : properties.rowMap().entrySet()) { JsonObject categoryNode = new JsonObject(); for (Map.Entry<String, IConfigPropertyHolder> property : category.getValue().entrySet()) { JsonObject propertyNode = new JsonObject(); IConfigPropertyHolder propertyHolder = property.getValue(); final String comment = propertyHolder.comment(); if (!Strings.isNullOrEmpty(comment)) propertyNode.addProperty(COMMENT_TAG, comment); try { Object value = propertyHolder.getValue(); JsonElement serialized = gson.toJsonTree(value); propertyNode.add(VALUE_TAG, serialized); } catch (Exception e) { Log.warn( e, "Failed to serialize property %s:%s", propertyHolder.category(), propertyHolder.name()); } categoryNode.add(property.getKey(), propertyNode); } result.add(category.getKey(), categoryNode); } return result; }
@Override public JsonElement serialize( DeviceOption[] src, Type typeOfSrc, JsonSerializationContext context) { JsonArray jsonObject = new JsonArray(); for (DeviceOption option : src) { JsonObject newObject = new JsonObject(); newObject.addProperty("property", option.getProperty()); newObject.addProperty("name", option.getName()); newObject.addProperty("description", option.getDescription()); newObject.addProperty("readonly", option.isReadOnly()); newObject.addProperty("type", option.getType().toString()); JsonArray newArray = new JsonArray(); for (String value : option.getValidValues()) { newArray.add(value); } newObject.add("validValues", newArray); if (option.isArray()) { newArray = new JsonArray(); for (String value : option.getArrayValue()) { newArray.add(value); } newObject.add("values", newArray); } else { newObject.addProperty("value", option.getValue()); } jsonObject.add(newObject); } return jsonObject; }
public static <K, V> JsonObject parse(Map<K, V> map) { if (map != null && !map.isEmpty()) { JsonObject jsonObject = new JsonObject(); Iterator<?> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, V> entry = (Map.Entry<String, V>) iterator.next(); String key = entry.getKey(); Object val = entry.getValue(); jsonObject.add(key, val); } return jsonObject; } return null; }
private void serializeChatStyle( ChatStyle style, JsonObject object, JsonSerializationContext ctx) { JsonElement var4 = ctx.serialize(style); if (var4.isJsonObject()) { JsonObject var5 = (JsonObject) var4; Iterator var6 = var5.entrySet().iterator(); while (var6.hasNext()) { Entry var7 = (Entry) var6.next(); object.add((String) var7.getKey(), (JsonElement) var7.getValue()); } } }
private JsonObject serializePrimitive(String key, Object value, boolean includeTypeInOutput) { JsonObject property = new JsonObject(); // id property mapping if (key.equals(idProperty)) { key = "id"; } property.add("key", new JsonPrimitive(key)); if (value != null) { property.add("value", primitive(value)); // include type? if (includeTypeInOutput) { String valueType = value.getClass().getSimpleName(); property.add("type", new JsonPrimitive(valueType)); } } else { property.add("value", new JsonNull()); // include type? if (includeTypeInOutput) { property.add("type", new JsonNull()); } } return property; }
@Override public JsonObject toJson() { JsonObject res = new JsonObject(); res.addProperty(Constants.VERSION, H2O.VERSION); res.addProperty(Constants.TYPE, KMeansModel.class.getName()); res.addProperty(Constants.ERROR, _error); JsonArray ary = new JsonArray(); for (double[] dd : clusters()) { JsonArray ary2 = new JsonArray(); for (double d : dd) ary2.add(new JsonPrimitive(d)); ary.add(ary2); } res.add(Constants.CLUSTERS, ary); return res; }