@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 AtomicExecution deserialize(JSONObject json, Class<? extends AtomicExecution> cls) { final JSONArray estimators = json.getJSONArray("estimators"); if (estimators.length() < 1) { throw new IllegalStateException("Expected at least one serialized estimator."); } // De-serialize the main estimator. final JSONObject mainEstimatorJson = estimators.getJSONObject(0); LoadProfileEstimator mainEstimator = this.deserializeEstimator(mainEstimatorJson); // De-serialize nested estimators. for (int i = 1; i < estimators.length(); i++) { mainEstimator.nest(this.deserializeEstimator(estimators.getJSONObject(i))); } return new AtomicExecution(mainEstimator); }
@Override public JSONObject serialize(AtomicExecution atomicExecution) { JSONArray estimators = new JSONArray(); this.serialize(atomicExecution.loadProfileEstimator, estimators); return new JSONObject().put("estimators", JsonSerializables.serialize(estimators, false)); }
@Override public AtomicExecutionGroup deserialize(JSONObject json, Class<? extends AtomicExecutionGroup> cls) { return new AtomicExecutionGroup( JsonSerializables.deserialize( json.getJSONObject("ctx"), SimpleEstimationContext.jsonSerializer, SimpleEstimationContext.class ), JsonSerializables.deserialize( json.getJSONObject("platform"), Platform.jsonSerializer ), this.configuration, JsonSerializables.deserializeAllAsList( json.getJSONArray("executions"), new AtomicExecution.KeyOrLoadSerializer(this.configuration, null), AtomicExecution.class ) ); } }
@Override public AtomicExecution deserialize(JSONObject json) { return this.deserialize(json, AtomicExecution.class); }
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); } }