Search in sources :

Example 1 with Converter

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

the class JsonDeserializationTest method testDeserializationWithCollection.

@Test
public void testDeserializationWithCollection() {
    MyEmbeddedDTO2<String> embedded = new MyEmbeddedDTO2<>();
    embedded.value = "one million dollars";
    MyDTO2<MyEmbeddedDTO2<String>> dto = new MyDTO2<>();
    dto.ping = "pppping";
    dto.pong = 42;
    dto.embedded = new ArrayList<>();
    dto.embedded.add(embedded);
    String serialized = new JsonSerializerImpl().serialize(dto).toString();
    Converter c = new SchematizerImpl().type("MyDTO", "/embedded", new TypeReference<MyEmbeddedDTO2<String>>() {
    }).schematize("MyDTO", new TypeReference<MyDTO2<MyEmbeddedDTO2<String>>>() {
    }).converterFor("MyDTO");
    MyDTO2<MyEmbeddedDTO2<String>> result = new JsonSerializerImpl().deserialize(new TypeReference<MyDTO2<MyEmbeddedDTO2<String>>>() {
    }).convertWith(c).from(serialized);
    assertEquals(dto.ping, result.ping);
    assertEquals(dto.pong, result.pong);
    assertEquals(dto.embedded.get(0).value, result.embedded.get(0).value);
}
Also used : SchematizerImpl(org.apache.felix.schematizer.impl.SchematizerImpl) Converter(org.osgi.util.converter.Converter) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 2 with Converter

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

the class JsonDeserializationTest method testDeserialization.

@Test
public void testDeserialization() {
    MyEmbeddedDTO embedded = new MyEmbeddedDTO();
    embedded.marco = "mmmm";
    embedded.polo = 24;
    embedded.alpha = Alpha.B;
    MyDTO dto = new MyDTO();
    dto.ping = "pppping";
    dto.pong = 42;
    dto.count = Count.TWO;
    dto.embedded = embedded;
    Converter c = new SchematizerImpl().schematize("MyDTO", new TypeReference<MyDTO>() {
    }).converterFor("MyDTO");
    String serialized = new JsonSerializerImpl().serialize(dto).convertWith(c).toString();
    MyDTO result = new JsonSerializerImpl().deserialize(MyDTO.class).convertWith(c).from(serialized);
    assertEquals(dto.ping, result.ping);
    assertEquals(dto.pong, result.pong);
    assertEquals(dto.count, result.count);
    assertEquals(dto.embedded.marco, result.embedded.marco);
    assertEquals(dto.embedded.polo, result.embedded.polo);
    assertEquals(dto.embedded.alpha, result.embedded.alpha);
}
Also used : SchematizerImpl(org.apache.felix.schematizer.impl.SchematizerImpl) Converter(org.osgi.util.converter.Converter) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 3 with Converter

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

Example 4 with Converter

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

the class SchemaImpl method valuesAt.

@SuppressWarnings({ "rawtypes", "unchecked" })
private Collection<?> valuesAt(String context, Map<String, Object> objectMap, List<String> contexts, int currentIndex) {
    List<Object> result = new ArrayList<>();
    String currentContext = contexts.get(currentIndex);
    if (objectMap == null)
        return result;
    Object o = objectMap.get(currentContext);
    if (o instanceof List) {
        List<Object> l = (List<Object>) o;
        if (currentIndex == contexts.size() - 1) {
            // We are at the end, so just add the collection
            result.add(convertToType(pathFrom(contexts, 0), l));
            return result;
        }
        currentContext = pathFrom(contexts, ++currentIndex);
        for (Object o2 : l) {
            final Converter converter = Converters.standardConverter();
            final Map<String, Object> m = (Map<String, Object>) converter.convert(o2).sourceAsDTO().to(Map.class);
            result.addAll(valuesAt(currentContext, m, contexts, currentIndex));
        }
    } else if (o instanceof Map) {
        if (currentIndex == contexts.size() - 1) {
            // We are at the end, so just add the result
            result.add(convertToType(pathFrom(contexts, 0), (Map) o));
            return result;
        }
        result.addAll(valuesAt(currentContext, (Map) o, contexts, ++currentIndex));
    } else if (currentIndex < contexts.size() - 1) {
        final Converter converter = Converters.standardConverter();
        final Map<String, Object> m = (Map<String, Object>) converter.convert(o).sourceAsDTO().to(Map.class);
        currentContext = pathFrom(contexts, ++currentIndex);
        result.addAll(valuesAt(currentContext, m, contexts, currentIndex));
    } else {
        result.add(o);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Converter(org.osgi.util.converter.Converter) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with Converter

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

the class SchemaImpl method valuesAt.

@Override
public Collection<?> valuesAt(String path, Object object) {
    final Converter converter = Converters.standardConverter();
    @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) converter.convert(object).sourceAsDTO().to(Map.class);
    if (map == null || map.isEmpty())
        return Collections.emptyList();
    if (path.startsWith("/"))
        path = path.substring(1);
    String[] pathParts = path.split("/");
    if (pathParts.length <= 0)
        return Collections.emptyList();
    List<String> contexts = Arrays.asList(pathParts);
    return valuesAt("", map, contexts, 0);
}
Also used : Converter(org.osgi.util.converter.Converter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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