use of org.osgi.util.converter.TypeRule in project felix by apache.
the class JsonSerializerTest method testCodecWithAdapter.
@Test
public void testCodecWithAdapter() throws JSONException {
Map<String, Foo> m1 = new HashMap<>();
m1.put("f", new Foo("fofofo"));
Map<String, Map<String, Foo>> m = new HashMap<>();
m.put("submap", m1);
Converter ca = converter.newConverterBuilder().rule(new TypeRule<Foo, String>(Foo.class, String.class, Foo::tsFun)).rule(new TypeRule<String, Foo>(String.class, Foo.class, v -> Foo.fsFun(v))).build();
JsonSerializerImpl jsonCodec = new JsonSerializerImpl();
String json = jsonCodec.serialize(m).convertWith(ca).toString();
JSONObject jo = new JSONObject(json);
assertEquals(1, jo.length());
JSONObject jo1 = jo.getJSONObject("submap");
assertEquals("<fofofo>", jo1.getString("f"));
// And convert back
Map<String, Map<String, Foo>> m2 = jsonCodec.deserialize(new TypeReference<Map<String, Map<String, Foo>>>() {
}).convertWith(ca).from(json);
assertEquals(m, m2);
}
use of org.osgi.util.converter.TypeRule in project felix by apache.
the class YamlSerializerTest method testCodecWithAdapter.
@Test
public void testCodecWithAdapter() {
Map<String, Foo> m1 = new HashMap<>();
m1.put("f", new Foo("fofofo"));
Map<String, Map<String, Foo>> m = new HashMap<>();
m.put("submap", m1);
Converter ca = converter.newConverterBuilder().rule(new TypeRule<Foo, String>(Foo.class, String.class, Foo::tsFun)).rule(new TypeRule<String, Foo>(String.class, Foo.class, v -> Foo.fsFun(v))).build();
YamlSerializerImpl yamlCodec = new YamlSerializerImpl();
String yaml = yamlCodec.serialize(m).convertWith(ca).toString();
assertEquals("submap: \n" + " f: '<fofofo>'", yaml);
// And convert back
Map<String, Map<String, Foo>> m2 = yamlCodec.deserialize(new TypeReference<Map<String, Map<String, Foo>>>() {
}).convertWith(ca).from(yaml);
assertEquals(m, m2);
}