Search in sources :

Example 1 with ConverterFunction

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

the class TypeConverter method getConverter.

public static Converter getConverter() {
    return Converters.standardConverter().newConverterBuilder().rule(new TargetRule() {

        @Override
        public Type getTargetType() {
            return String.class;
        }

        @Override
        public ConverterFunction getFunction() {
            return new ConverterFunction() {

                @Override
                public Object apply(final Object obj, final Type targetType) throws Exception {
                    if (obj instanceof Map || obj instanceof List) {
                        final JsonStructure json = JSONUtil.build(obj);
                        final StringWriter w = new StringWriter();
                        Json.createWriter(w).write(json);
                        return w.toString();
                    }
                    return CANNOT_HANDLE;
                }
            };
        }
    }).build();
}
Also used : Type(java.lang.reflect.Type) StringWriter(java.io.StringWriter) ConverterFunction(org.osgi.util.converter.ConverterFunction) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TargetRule(org.osgi.util.converter.TargetRule) JsonStructure(javax.json.JsonStructure)

Aggregations

StringWriter (java.io.StringWriter)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 JsonStructure (javax.json.JsonStructure)1 ConverterFunction (org.osgi.util.converter.ConverterFunction)1 TargetRule (org.osgi.util.converter.TargetRule)1