/** * Serialize the given {@link JsonSerializable}. * * @param serializable the {@link JsonSerializable} to serialize * @param isPolymorph in case a {@link JSONObject} is created, whether it should be tagged with the {@link Class} * of {@code serializable} * @return the serialization result */ public static JSONObject serialize(JsonSerializable serializable, boolean isPolymorph) { return serialize(serializable, isPolymorph, JsonSerializable.uncheckedSerializer); }
@Override public JSONObject serialize(AtomicExecutionGroup aeg) { AtomicExecution.KeyOrLoadSerializer atomicExecutionSerializer = new AtomicExecution.KeyOrLoadSerializer(null, aeg.estimationContext); return new JSONObject() .put("ctx", JsonSerializables.serialize(aeg.estimationContext, false, EstimationContext.defaultSerializer)) .put("platform", JsonSerializables.serialize(aeg.platform, true, Platform.jsonSerializer)) .put("executions", JsonSerializables.serializeAll(aeg.atomicExecutions, false, atomicExecutionSerializer)); }
@Override public JSONObject toJson() { JSONObject json = new JSONObject(); json.put("cpu", JsonSerializables.serialize(this.cpuUsage, false)); json.put("ram", JsonSerializables.serialize(this.ramUsage, false)); json.putOpt("network", JsonSerializables.serialize(this.networkUsage, false)); json.putOpt("disk", JsonSerializables.serialize(this.diskUsage, false)); json.put("utilization", this.resourceUtilization); json.put("overhead", this.overheadMillis); return json; }
/** * Serialize the given {@link Object}s using a specific {@link JsonSerializer}. * * @param collection the {@link Object}s to serialize * @param isPolymorph in case a {@link JSONObject} is created, whether it should be tagged with the {@link Class} * of {@code serializable} * @param serializer the {@link JsonSerializer} * @return the serialization result */ public static <T> JSONArray serializeAll(Collection<T> collection, boolean isPolymorph, JsonSerializer<T> serializer) { if (collection == null) return null; JSONArray jsonArray = new JSONArray(); for (T serializable : collection) { jsonArray.put(serialize(serializable, isPolymorph, serializer)); } return jsonArray; }
/** * Stores the given {@link PartialExecution} in this instance. * * @param partialExecution that should be stored * @param partialExecutionJsonSerializer serializes {@link PartialExecution}s */ private void store(PartialExecution partialExecution, JsonSerializer<PartialExecution> partialExecutionJsonSerializer) throws IOException { this.write(JsonSerializables.serialize(partialExecution, false, partialExecutionJsonSerializer)); }
@Override public JSONObject toJson() { JSONObject json = new JSONObject(); json.put("lower", JsonSerializables.serialize(this.getLowerEstimate(), false)); json.put("upper", JsonSerializables.serialize(this.getUpperEstimate(), false)); json.put("prob", JsonSerializables.serialize(this.getCorrectnessProbability(), false)); return json; }
@Override public JSONObject serialize(AtomicExecution atomicExecution) { JSONArray estimators = new JSONArray(); this.serialize(atomicExecution.loadProfileEstimator, estimators); return new JSONObject().put("estimators", JsonSerializables.serialize(estimators, false)); }
/** * Try to serialize the given {@link Object}. It must be JSON-compatible or a {@link JsonSerializable}. * * @param obj the {@link Object} to serialize * @param isPolymorph in case a {@link JSONObject} is created, whether it should be tagged with the {@link Class} * of {@code obj} * @return the serialization result * @see #isJsonCompatible(Object) */ public static Object serialize(Object obj, boolean isPolymorph) { if (obj == null) return null; if (isJsonCompatible(obj)) { return obj; } if (obj instanceof JsonSerializable) return serialize((JsonSerializable) obj, isPolymorph); throw new IllegalArgumentException(String.format("Cannot serialize %s.", obj)); }
/** * Try to serialize the given {@link Object}s. They must be JSON-compatible or a {@link JsonSerializable}s. * * @param collection the {@link Object}s to serialize * @param isPolymorph in case a {@link JSONObject} is created, whether it should be tagged with the {@link Class} * of the according {@link Object} * @return the serialization result * @see #isJsonCompatible(Object) */ public static JSONArray serializeAll(Collection<?> collection, boolean isPolymorph) { if (isJsonNull(collection)) return null; JSONArray jsonArray = new JSONArray(); for (Object obj : collection) { jsonArray.put(serialize(obj, isPolymorph)); } return jsonArray; }
private void serialize(LoadProfileEstimator estimator, JSONArray collector) { JSONObject json = new JSONObject(); if (estimator.getConfigurationKey() != null) { json.put("key", estimator.getConfigurationKey()); } else { final LoadProfile loadProfile = estimator.estimate(this.estimationContext); json.put("load", JsonSerializables.serialize(loadProfile, false)); } collector.put(json); for (LoadProfileEstimator nestedEstimator : estimator.getNestedEstimators()) { this.serialize(nestedEstimator, collector); } }