/** * 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); } }
default Binder toBinder() { return DefaultBiMapper.serialize(this); } }
/** * Create serialized to work with default mapper, {@link DefaultBiMapper#getInstance()}. */ public BiSerializer() { this.mapper = DefaultBiMapper.getInstance(); }
BiMapper biMapper = DefaultBiMapper.getInstance(); DefaultBiMapper.registerAdapter(dummy.getClass(), customByteArrayBiAdapter); DefaultBiMapper.registerAdapter(Bytes.class, customBytesBiAdapter); DefaultBiMapper.registerAdapter(dummy.getClass(), DefaultBiMapper.getByteArrayBiAdapter()); DefaultBiMapper.registerAdapter(Bytes.class, DefaultBiMapper.getBytesBiAdapter());
BiMapper biMapper = DefaultBiMapper.getInstance(); DefaultBiMapper.registerClass(Reference.class);
@Test public void processBytes() throws Exception { byte x[] = Do.randomBytes(10); Binder s = DefaultBiMapper.serialize(x); byte[] result = ((Bytes)DefaultBiMapper.deserialize(s)).getData(); assertArrayEquals(x, result); } }
@Test public void autoSerializableBinder() throws Exception { // if the object is instance of Map (for example Binder), it could also have // it's own serialization: Rebinder b = new Rebinder(); DefaultBiMapper.registerClass(Rebinder.class); b.put("foo", "bar"); Binder s = DefaultBiMapper.serialize(b); Object x = DefaultBiMapper.deserialize(s); assertThat(x, instanceOf(Rebinder.class)); assertEquals("bar", ((Map)x).get("foo")); }
@Test public void timeIssues() throws Exception { Binder x = Binder.fromKeysValues( "tt", ZonedDateTime.now() ); x = (Binder) DefaultBiMapper.serialize(x); // System.out.println(x); x.getBinderOrThrow("tt").put("seconds", 0); DefaultBiMapper.deserializeInPlace(x); ZonedDateTime t = x.getZonedDateTimeOrThrow("tt"); assertEquals( "1970-01-01T00:00Z", t.withZoneSameInstant(ZoneOffset.UTC).toString()); // System.out.println(""+Boss.serializeToBinder(x)); } }
@Test public void testAddressRole() throws Exception { Set<KeyAddress> keyAddresses = new HashSet<>(); keyAddresses.add(new KeyAddress(keys.get(0).getPublicKey(), 0, true)); SimpleRole sr = new SimpleRole("tr1", keyAddresses); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); Set<PublicKey> pubKeys = new HashSet<>(); pubKeys.add(keys.get(0).getPublicKey()); assertTrue(sr.isAllowedForKeys(pubKeys)); assertTrue(r1.isAllowedForKeys(pubKeys)); assertEquals(sr, r1); }
@Test public void autoSerializableInheritance() throws Exception { Test1 t1 = new Test2("foo"); DefaultBiMapper.registerClass(Test2.class); Binder s = DefaultBiMapper.serialize(t1); assertEquals(s.getStringOrThrow("__type"), "net.sergeych.biserializer.Test2"); Test2 t2 = DefaultBiMapper.deserialize(s); assertThat(t2, instanceOf(Test1.class) ); assertEquals("bad", t2.getValue()); assertEquals("foo", t2.getBar()); }
@Test public void serializeNone() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(r1.getReferences(Role.RequiredMode.ANY_OF).isEmpty()); assertTrue(r1.getReferences(Role.RequiredMode.ALL_OF).isEmpty()); }
/** * 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); } }
@Test public void autoSerializable() throws Exception { Test1 t1 = new Test1("foo"); DefaultBiMapper.registerClass(Test1.class); Binder s = DefaultBiMapper.serialize(t1); assertEquals(s.getStringOrThrow("__type"), "foobar1"); assertEquals(s.getStringOrThrow("value"), "foo"); Test1 t2 = DefaultBiMapper.deserialize(s); assertThat(t2, instanceOf(Test1.class) ); assertEquals("foo", t2.getValue()); }
public Binder toBinder() { return Binder.fromKeysValues( "state", state.name(), "haveCopy", haveCopy, "createdAt", createdAt, "expiresAt", expiresAt, "errors", DefaultBiMapper.serialize(errors), "isTestnet", isTestnet, "lockedById", lockedById, "extra", extraDataBinder ); }
/** * Construct deserializer with {@link DefaultBiMapper} */ public BiDeserializer() { mapper = DefaultBiMapper.getInstance(); }
@Test public void serializeMany() throws Exception { SimpleRole sr = new SimpleRole("tr1"); keys.forEach(k-> sr.addKeyRecord(new KeyRecord(k.getPublicKey()))); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); Set<PublicKey> kk = ((SimpleRole)r1).getSimpleKeys(); keys.forEach(k->assertTrue(kk.contains(k.getPublicKey()))); }
/** * 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); } }
@Test public void validPermissionIds() throws Exception { Contract c = Contract.fromDslFile(ROOT_CONTRACT); c.addSignerKeyFromFile(PRIVATE_KEY_PATH); byte[] sealed = c.seal(); assertTrue(c.check()); Binder s = DefaultBiMapper.serialize(c); s.getBinderOrThrow("definition","permissions"); }
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; }
@Test public void serializeAny() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.addRequiredReference("ref", Role.RequiredMode.ANY_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(sr.getReferences(Role.RequiredMode.ALL_OF).isEmpty()); assertEquals(sr.getReferences(Role.RequiredMode.ANY_OF).size(),1); assertEquals(sr.getReferences(Role.RequiredMode.ANY_OF).iterator().next(),"ref"); }