@SuppressWarnings("unused") public static LoadProfile fromJson(JSONObject jsonObject) { return new LoadProfile( JsonSerializables.deserialize(jsonObject.getJSONObject("cpu"), LoadEstimate.class), JsonSerializables.deserialize(jsonObject.getJSONObject("ram"), LoadEstimate.class), JsonSerializables.deserialize(jsonObject.optJSONObject("network"), LoadEstimate.class), JsonSerializables.deserialize(jsonObject.optJSONObject("disk"), LoadEstimate.class), jsonObject.getDouble("utilization"), jsonObject.getLong("overhead") ); } }
@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 ) ); } }
/** * Deserialize a {@link JSONArray} according to the rules of {@link #deserialize(JSONObject, JsonSerializer)}. * * @param jsonArray the {@link JSONArray} * @param serializer the {@link JsonSerializer} * @return the deserialization product */ @SuppressWarnings("unchecked") public static <T> List<T> deserializeAllAsList(JSONArray jsonArray, JsonSerializer<T> serializer) { List<T> result = new ArrayList<>(jsonArray.length()); for (Object jsonElement : jsonArray) { result.add(isJsonNull(jsonElement) ? null : deserialize((JSONObject) jsonElement, serializer)); } return result; }
/** * Deserialize a given JSON datatype. The following cases are supported: * <ul> * <li>{@code json} is a (JSON) {@code null} value;</li> * <li>{@code json} is a basic (JSON) datatype;</li> * <li>{@code json} is a {@link Class}-tagged {@link JSONObject} that corresponds to a {@link JsonSerializable};</li> * <li>{@code json} is a {@link JSONArray} with {@link Class}-tagged {@link JSONObject}s that correspond to a * {@link JsonSerializable}s - in this case, the result type is a {@link List}.</li> * </ul> * * @param json the JSON data * @return the deserialization result */ public static Object deserialize(Object json) { if (isJsonNull(json)) return null; else if (isUnconvertedInstance(json)) return json; else if (json instanceof JSONObject) return deserialize((JSONObject) json); else if (json instanceof JSONArray) return deserializeAllAsList((JSONArray) json); throw new SerializationException(String.format("Don't know how to deserialize %s.", json)); }
/** * Deserialize a {@link JSONArray} according to the rules of {@link #deserialize(Object)}. * * @param jsonArray the {@link JSONArray} * @return the deserialization product */ @SuppressWarnings("unchecked") public static <T> List<T> deserializeAllAsList(JSONArray jsonArray) { if (isJsonNull(jsonArray)) return null; List<T> result = new ArrayList<>(jsonArray.length()); for (Object jsonElement : jsonArray) { result.add((T) deserialize(jsonElement)); } return result; }
/** * Deserialize a {@link JSONArray} according to the rules of {@link #deserialize(JSONObject, JsonSerializer, Class)}. * * @param jsonArray the {@link JSONArray} * @param serializer the {@link JsonSerializer} * @param cls the {@link Class} of the elements in the {@code jsonArray} * @return the deserialization product */ @SuppressWarnings("unchecked") public static <T> List<T> deserializeAllAsList(JSONArray jsonArray, JsonSerializer<T> serializer, Class<T> cls) { List<T> result = new ArrayList<>(jsonArray.length()); for (Object jsonElement : jsonArray) { result.add(isJsonNull(jsonElement) ? null : deserialize((JSONObject) jsonElement, serializer, cls)); } return result; }
/** * Streams the contents of this instance. * * @return a {@link Stream} of the contained {@link PartialExecution}s * @throws IOException */ public Stream<PartialExecution> stream() throws IOException { IOUtils.closeQuietly(this.writer); this.writer = null; final PartialExecution.Serializer serializer = new PartialExecution.Serializer(this.configuration); return Files.lines(Paths.get(this.repositoryPath), Charset.forName("UTF-8")) .map(line -> { try { return JsonSerializables.deserialize(new JSONObject(line), serializer, PartialExecution.class); } catch (Exception e) { throw new RheemException(String.format("Could not parse \"%s\".", line), e); } }); }
/** * Deserialize a {@link Class}-tagged {@link JSONObject} that should correspond to a {@link JsonSerializable}. * * @param jsonObject the {@link JSONObject} * @return the deserialization product */ @SuppressWarnings("unchecked") public static <T> T deserialize(JSONObject jsonObject) { if (isJsonNull(jsonObject)) return null; return deserialize(jsonObject, JsonSerializable.uncheckedSerializer()); }
/** * Deserialize a {@link JSONObject} that should correspond to a {@link JsonSerializable}. * * @param jsonObject the {@link JSONObject} * @param cls the {@link Class} of the deserialization product * @return the deserialization product */ @SuppressWarnings("unchecked") public static <T> T deserialize(JSONObject jsonObject, Class<? extends T> cls) { if (isJsonNull(jsonObject)) return null; return deserialize(jsonObject, (JsonSerializer<T>) JsonSerializable.uncheckedSerializer(), cls); }
/** * Deserialize a {@link LoadProfileEstimator} according to {@link #serialize(LoadProfileEstimator, JSONArray)}. * * @param jsonObject that should be deserialized * @return the {@link LoadProfileEstimator} */ private LoadProfileEstimator deserializeEstimator(JSONObject jsonObject) { if (jsonObject.has("key")) { final String key = jsonObject.getString("key"); final LoadProfileEstimator estimator = LoadProfileEstimators.createFromSpecification(key, this.configuration); if (estimator == null) { throw new SerializationException("Could not create estimator for key " + key); } return estimator; } else if (jsonObject.has("load")) { final LoadProfile load = JsonSerializables.deserialize(jsonObject.getJSONObject("load"), LoadProfile.class); return new ConstantLoadProfileEstimator(load); } throw new SerializationException(String.format("Cannot deserialize load estimator from %s.", jsonObject)); } }