Search in sources :

Example 6 with Converter

use of org.osgi.util.converter.Converter in project felix by apache.

the class JsonBackingObjectSerializationTest method testComplexMapSerializationUsingRule.

@Test
public void testComplexMapSerializationUsingRule() {
    final Converter converter = Converters.newConverterBuilder().rule(new MapTargetRule()).build();
    final JsonWriterFactory factory = new JsonWriterFactory();
    final String actual = new JsonSerializerImpl().serialize(MyDTOishObject.factory("A", "B")).convertWith(converter).writeWith(factory.newDebugWriter(converter)).toString();
    assertEquals(EXPECTED, actual);
}
Also used : Converter(org.osgi.util.converter.Converter) Test(org.junit.Test)

Example 7 with Converter

use of org.osgi.util.converter.Converter 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);
}
Also used : HashMap(java.util.HashMap) Converter(org.osgi.util.converter.Converter) Map(java.util.Map) HashMap(java.util.HashMap) TypeRule(org.osgi.util.converter.TypeRule) Test(org.junit.Test)

Aggregations

Converter (org.osgi.util.converter.Converter)7 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 LinkedHashMap (java.util.LinkedHashMap)2 SchematizerImpl (org.apache.felix.schematizer.impl.SchematizerImpl)2 TypeReference (org.osgi.util.converter.TypeReference)2 TypeRule (org.osgi.util.converter.TypeRule)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JSONObject (org.apache.sling.commons.json.JSONObject)1