/** * Deserialize some object if need. E.g. if the object is a simple type supported by most notations, e.g. numbers, * strings, it is left unchanged, but if it is a collection or a Map it will be processed to deserialize its * content. See {@link BiMapper#deserializeObject(Object)} for details * * @param object to deserialize * @param <T> * @param <U> * * @return restored object */ public <T, U> T deserialize(U object) { return (T) mapper.deserializeObject(object, this); }
/** * Deserialize a collection. Utility method that deserialize every object and return the result as an ArrayList. * * @param collection * @param <T> * * @return */ public <T> List<T> deserializeCollection(Collection<?> collection) { return collection.stream() .map(x -> (T) mapper.deserializeObject(x, this)) .collect(Collectors.toList()); }
public <T> T deserializeObject(Object x) { return x == null ? null : deserializeObject(x, new BiDeserializer(this)); }
/** * Deserialize any object, if possible. * <p> * <ul> <li>if the object is a simple type, e.g. number, string, boolean, it will be simply retured.</li> <li>if the * object is a collection, every item of it will be deserialized the sam way and the result will be returned as a * List with random access (like ArrayList)</li> <li>if the object is a map, it will be processed as with {@link * #deserialize(Map, BiDeserializer)}</li> </ul> * * @param obj to deserealize * @param deserializer to * @param <T> * * @return */ public <T> T deserializeObject(Object obj, BiDeserializer deserializer) { if (obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj instanceof ZonedDateTime || obj instanceof Bytes || obj == null) return (T) obj; if (obj instanceof Map) return deserialize((Map) obj, deserializer); if (obj instanceof Collection) { return (T) ((Collection) obj).stream() .map(x -> deserializeObject(x)) .collect(Collectors.toList()); } throw new IllegalArgumentException("don't know how to deserealize " + obj.getClass().getCanonicalName()); }
private boolean checkContractCorrespondsToConfig(Contract contract, List<NodeInfo> netNodes) { //check if contract contains net config if(!checkIfContractContainsNetConfig(contract)) { return false; } //check if net config equals to current network configuration List<NodeInfo> contractNodes = DefaultBiMapper.getInstance().deserializeObject(contract.getStateData().get("net_config")); if(contractNodes.size() != netNodes.size() || !contractNodes.stream().allMatch(nodeInfo -> netNodes.contains(nodeInfo))) { return false; } return true; }
@SuppressWarnings("deprecation") //outdated method. will be replaced with new one soon private void checkForNetConfig(Contract contract) { if (contract.getIssuer().getKeys().stream().anyMatch(key -> config.getNetworkReconfigKeyAddress().isMatchingKey(key))) { if(contract.getParent() == null) return; if(contract.getRevoking().size() == 0 || !contract.getRevoking().get(0).getId().equals(contract.getParent())) return; Contract parent = contract.getRevoking().get(0); if(!checkContractCorrespondsToConfig(parent,network.allNodes())) { return; } if(!checkIfContractContainsNetConfig(contract)) { return; } List<NodeInfo> networkNodes = network.allNodes(); List contractNodes = (List)DefaultBiMapper.getInstance().deserializeObject(contract.getStateData().get("net_config")); contractNodes.stream().forEach(nodeInfo -> { if(!networkNodes.contains(nodeInfo)) { addNode((NodeInfo) nodeInfo); } networkNodes.remove(nodeInfo); }); networkNodes.stream().forEach( nodeInfo -> removeNode(nodeInfo)); } }
Object obj = DefaultBiMapper.getInstance().deserializeObject(contract.getStateData().get("net_config")); if(!(obj instanceof List)) { return false;