/** * Deserialize a {@link JSONArray} according to the rules of {@link #deserialize(JSONObject, Class)}. * * @param jsonArray the {@link JSONArray} * @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, Class<T> cls) { return deserializeAllAsList(jsonArray, (JsonSerializer<T>) JsonSerializable.uncheckedSerializer(), cls); }
@Override public PartialExecution deserialize(JSONObject json, Class<? extends PartialExecution> cls) { final long measuredExecutionTime = json.getLong("millis"); final double lowerCost = json.optDouble("lowerCost", -1); final double uppserCost = json.optDouble("upperCost", -1); final Collection<AtomicExecutionGroup> atomicExecutionGroups = JsonSerializables.deserializeAllAsList( json.getJSONArray("execGroups"), new AtomicExecutionGroup.Serializer(this.configuration), AtomicExecutionGroup.class ); final Collection<Platform> initializedPlatforms = JsonSerializables.deserializeAllAsList(json.optJSONArray("initPlatforms"), Platform.jsonSerializer); final PartialExecution partialExecution = new PartialExecution( atomicExecutionGroups, measuredExecutionTime, lowerCost, uppserCost ); partialExecution.initializedPlatforms.addAll(initializedPlatforms); return partialExecution; } }
@Override public SimpleEstimationContext deserialize(JSONObject json, Class<? extends SimpleEstimationContext> cls) { final List<CardinalityEstimate> inCards = JsonSerializables.deserializeAllAsList( json.getJSONArray("inCards"), CardinalityEstimate.class ); final List<CardinalityEstimate> outCards = JsonSerializables.deserializeAllAsList( json.getJSONArray("outCards"), CardinalityEstimate.class ); final TObjectDoubleHashMap<String> doubleProperties = new TObjectDoubleHashMap<>(); final JSONObject doublePropertiesJson = json.optJSONObject("properties"); if (doublePropertiesJson != null) { for (String key : doublePropertiesJson.keySet()) { doubleProperties.put(key, doublePropertiesJson.getDouble(key)); } } final int numExecutions = json.getInt("executions"); return new SimpleEstimationContext( inCards.toArray(new CardinalityEstimate[inCards.size()]), outCards.toArray(new CardinalityEstimate[outCards.size()]), doubleProperties, numExecutions ); } };
@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 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)); }