Author: davidb Date: Tue Jul 5 14:24:45 2016 New Revision: 1751487 URL: http://svn.apache.org/viewvc?rev=1751487&view=rev Log: Felix Converter Service - work on the JSON codec
Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonDecodingImpl.java felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonParser.java Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java?rev=1751487&r1=1751486&r2=1751487&view=diff ============================================================================== --- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java (original) +++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonCodecImpl.java Tue Jul 5 14:24:45 2016 @@ -56,6 +56,7 @@ public class JsonCodecImpl implements Co if (top == null) { threadLocal.set(Boolean.TRUE); + // TODO implement this properly encoding = new EncodingWrapper("{}{}{}{}{}", encoding, "{}{}{}{}{}"); } } Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonDecodingImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonDecodingImpl.java?rev=1751487&r1=1751486&r2=1751487&view=diff ============================================================================== --- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonDecodingImpl.java (original) +++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonDecodingImpl.java Tue Jul 5 14:24:45 2016 @@ -16,11 +16,15 @@ */ package org.apache.felix.converter.impl.json; +import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.Method; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Map; +import java.util.Scanner; +import org.apache.felix.converter.impl.Util; +import org.osgi.service.converter.ConversionException; import org.osgi.service.converter.Converter; import org.osgi.service.converter.Decoding; @@ -34,47 +38,37 @@ public class JsonDecodingImpl<T> impleme } @Override - public T from(CharSequence in) { - if (Map.class.isAssignableFrom(clazz)) { - return createMapFromJSONString(in); - } - return deserializeSingleJSONValue(clazz, in); - } - @SuppressWarnings("unchecked") - private T createMapFromJSONString(CharSequence in) { - JsonParser jp = new JsonParser((String) in); - return (T) jp.getParsed(); - } + public T from(CharSequence in) { + JsonParser jp = new JsonParser(in); + Map<?,?> m = jp.getParsed(); + if (Map.class.isAssignableFrom(clazz)) + return (T) m; - @SuppressWarnings("unchecked") - private T deserializeSingleJSONValue(Class<T> cls, CharSequence cs) { - try { - Method m = cls.getDeclaredMethod("valueOf", String.class); - if (m != null) { - return (T) m.invoke(null, cs); - } - } catch (Exception e) { - return null; - } - return null; + return converter.convert(m).to(clazz); } @Override public T from(InputStream in) { - // TODO Auto-generated method stub - return null; + return from(in, StandardCharsets.UTF_8); } @Override public T from(InputStream in, Charset charset) { - // TODO Auto-generated method stub - return null; + try { + byte[] bytes = Util.readStream(in); + String s = new String(bytes, charset); + return from(s); + } catch (IOException e) { + throw new ConversionException("Error reading inputstream", e); + } } @Override public T from(Readable in) { - // TODO Auto-generated method stub - return null; + try (Scanner s = new Scanner(in)) { + s.useDelimiter("\\Z"); + return from(s.next()); + } } } Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java?rev=1751487&r1=1751486&r2=1751487&view=diff ============================================================================== --- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java (original) +++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonEncodingImpl.java Tue Jul 5 14:24:45 2016 @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Array; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -27,6 +28,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import org.osgi.service.converter.ConversionException; import org.osgi.service.converter.Converter; import org.osgi.service.converter.Encoding; @@ -44,15 +46,31 @@ public class JsonEncodingImpl implements } @Override + public Appendable to(Appendable out) { + try { + out.append(encode(object)); + } catch (IOException e) { + throw new ConversionException("Problem converting to JSON", e); + } + + return out; + } + + @Override public void to(OutputStream os, Charset charset) { try { os.write(encode(object).getBytes(charset)); } catch (IOException e) { - throw new RuntimeException(e); + throw new ConversionException("Problem converting to JSON", e); } } @Override + public void to(OutputStream out) throws IOException { + to(out, StandardCharsets.UTF_8); + } + + @Override public String toString() { return encode(object); } @@ -137,15 +155,4 @@ public class JsonEncodingImpl implements return null; } - @Override - public Appendable to(Appendable out) { - // TODO Auto-generated method stub - return null; - } - - @Override - public void to(OutputStream out) throws IOException { - // TODO Auto-generated method stub - - } } Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonParser.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonParser.java?rev=1751487&r1=1751486&r2=1751487&view=diff ============================================================================== --- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonParser.java (original) +++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/json/JsonParser.java Tue Jul 5 14:24:45 2016 @@ -73,9 +73,10 @@ public class JsonParser { private final Map<String, Object> parsed; - public JsonParser(String json) { - json = json.trim().replace('\n', ' '); - parsed = parseObject(json); + public JsonParser(CharSequence json) { + String str = json.toString(); + str = str.trim().replace('\n', ' '); + parsed = parseObject(str); } public JsonParser(InputStream is) throws IOException {