/** * Convert some map to the binder. Do nothing if it is already a binder. * * @param x source map */ static public Binder of(Object x) { return from(x); }
cb.send(Binder.fromKeysValues("hello", "world")); cb.send(Binder.fromKeysValues("hello", "world2")); Binder res = Binder.from(cb.receive()); assertEquals("bar", res.getStringOrThrow("foo")); res = Binder.from(cb.receive()); res = Binder.from(cb.receive()); ca.send(Binder.fromKeysValues("foo", "bar2")); ca.send(Binder.fromKeysValues("foo3", "bar")); Binder res = Binder.from(ca.receive()); assertEquals("world", res.getStringOrThrow("hello")); res = Binder.from(ca.receive()); assertEquals("world2", res.getStringOrThrow("hello")); return null;
/** * Method creates smart contract from dsl file where contract is described. * @param fileName is path to dsl file with yaml structure of data for contract. */ public static NSmartContract fromDslFile(String fileName) throws IOException { Yaml yaml = new Yaml(); try (FileReader r = new FileReader(fileName)) { Binder binder = Binder.from(DefaultBiMapper.deserialize((Map) yaml.load(r))); return new NSmartContract().initializeWithDsl(binder); } }
/** * Method creates {@link UnsContract} contract from dsl file where contract is described. * @param fileName is path to dsl file with yaml structure of data for contract. */ public static UnsContract fromDslFile(String fileName) throws IOException { Yaml yaml = new Yaml(); try (FileReader r = new FileReader(fileName)) { Binder binder = Binder.from(DefaultBiMapper.deserialize((Map) yaml.load(r))); return new UnsContract().initializeWithDsl(binder); } }
/** * Create contract importing its parameters with passed .yaml file. No signatures are added automatically. It is required to add signatures before check. * @param fileName path to file containing Yaml representation of contract. */ public static Contract fromDslFile(String fileName) throws IOException { Yaml yaml = new Yaml(); try (FileReader r = new FileReader(fileName)) { Binder binder = Binder.from(DefaultBiMapper.deserialize((Map) yaml.load(r))); return new Contract().initializeWithDsl(binder); } }
/** * Method creates {@link SlotContract} contract from dsl file where contract is described. * @param fileName is path to dsl file with yaml structure of data for contract. * @return created and ready {@link SlotContract} contract. * @throws IOException if something went wrong */ public static SlotContract fromDslFile(String fileName) throws IOException { Yaml yaml = new Yaml(); try (FileReader r = new FileReader(fileName)) { Binder binder = Binder.from(DefaultBiMapper.deserialize((Map) yaml.load(r))); return new SlotContract().initializeWithDsl(binder); } }
/** * Method creates {@link FollowerContract} contract from dsl file where contract is described. * * @param fileName is path to dsl file with yaml structure of data for contract. * @return created and ready {@link FollowerContract} contract. * @throws IOException if something went wrong */ public static FollowerContract fromDslFile(String fileName) throws IOException { Yaml yaml = new Yaml(); try (FileReader r = new FileReader(fileName)) { Binder binder = Binder.from(DefaultBiMapper.deserialize((Map) yaml.load(r))); return new FollowerContract().initializeWithDsl(binder); } }
public static <T> T deserialize(Map map) { return new BiDeserializer().deserialize(Binder.from(map)); }
/** * Convert all maps in the object to binders and do it recursively. * * @param object source map */ static public <T> T convertAllMapsToBinders(Object object) { if(object != null) { if (object instanceof List) { List list = (List) object; for (int i = 0; i < list.size(); i++) { list.set(i, convertAllMapsToBinders(list.get(i))); } } if (object instanceof Map) { object = Binder.from(object); Map map = (Map) object; for (Object key : map.keySet()) { map.replace(key, convertAllMapsToBinders(map.get(key))); } } } return (T) object; }
/** * Try to deserialize map containing serialized object, e.g. having "__type" or "__t" key. Important. Is this * implementation can't deserialize the map, what usually means it has no known/properly serialized object in it, it * juts desearliazes it in place with {@link #deserializeInPlace(Map, BiDeserializer)} and return the same (but * updated) map instance. * * @param map possible containing the serialized object * @param <T> * * @return deserialized object or deserialized map */ public <T> T deserialize(Map map, BiDeserializer deserializer) { String typeName = (String) map.get("__type"); if (typeName == null) typeName = (String) map.get("__t"); if (typeName != null) { BiAdapter adapter = adapters.get(typeName); if (adapter != null) { return (T) adapter.deserialize(Binder.from(map), deserializer); } } deserializeInPlace(map, deserializer); return (T) map; }
/** * Update the map deserialziing it's values - e.g. in place. Saves memory but has strong side effects of discarding * source data. * * @param map to update * @param deserializer to use to deserialize map values */ public void deserializeInPlace(Map map, BiDeserializer deserializer) { map.forEach((key, value) -> { if (value instanceof Map) { String typeName = (String) ((Map) value).get("__type"); if (typeName == null) typeName = (String) ((Map) value).get("__t"); if (typeName == null) deserializeInPlace((Map) value, deserializer); else { BiAdapter adapter = adapters.get(typeName); if (adapter != null) { map.put(key, adapter.deserialize(Binder.from(value), deserializer)); } } } else if (value instanceof Collection) { map.put(key, ((Collection) value).stream() .map(x -> deserializer.deserialize(x)) .collect(Collectors.toList()) ); } }); }
byte[] answer = Do.read(httpConnection.getInputStream()); httpConnection.disconnect(); return new Answer(responseCode, Binder.from(Boss.load(answer)));
public static <T> T deserialize(Map map) { return new BiDeserializer(getInstance()).deserialize(Binder.from(map)); }
Class<Permission> cls = (Class<Permission>) Permission.class.getClassLoader().loadClass(className); return cls.getConstructor(Role.class, Binder.class) .newInstance(role, Binder.from(params)); } catch (ClassCastException | ClassNotFoundException e) { throw new IllegalArgumentException("unknown permission: " + name);
public static NodeInfo loadYaml(Path fileName) { try { Yaml yaml = new Yaml(); Binder b = Binder.from(yaml.load(new FileInputStream(fileName.toString())));
else { binderParams = Binder.from(params); Object x = binderParams.getOrThrow("role"); if (x instanceof Role) {
/** * The handshake and transport command processing * * @param command * * @return * * @throws Exception */ @Override public Object onCommand(Command command) throws Exception { // log.d(toString()+" cmd "+command.getName()); switch (command.getName()) { case "hello": return onHello(Binder.from(command.getKeyParams())); case "block": return decryptBlock(command); } return null; }
this.isTrustedKey = isTrustedKey; connection.start(this); Binder result = Binder.from( connection.sendKeyParams( "hello",
/** * Resolve object describing role and create either: - new role object - symlink to named role instance, ensure it * is register and return it, if it is a Map, tries to construct and register {@link Role} then return it. * * @param roleName is name of the role * @param roleObject is object for role creating * * @return created {@link Role} */ @NonNull public Role createRole(String roleName, Object roleObject) { if (roleObject instanceof CharSequence) { return registerRole(new RoleLink(roleName, roleObject.toString())); } if (roleObject instanceof Role) if(((Role)roleObject).getName() != null && ((Role)roleObject).getName().equals(roleName)) return registerRole(((Role) roleObject)); else return registerRole(((Role) roleObject).linkAs(roleName)); if (roleObject instanceof Map) { Role r = Role.fromDslBinder(roleName, Binder.from(roleObject)); return registerRole(r); } throw new IllegalArgumentException("cant make role from " + roleObject); }
@Test public void emptyStrings() throws Exception { Object xo = JsonTool.fromJson("{\"text\":\"\",\"type\":\"p2pchat\",\"from_party_id\":\"1271\"}"); Binder b = Binder.from(xo); Bytes bb = Boss.dump(b); String x = (String)Boss.unpack(bb.toArray()).getStringOrThrow("text"); assertEquals("", x); }