Author: dleangen
Date: Thu Aug 10 22:17:48 2017
New Revision: 1804736

URL: http://svn.apache.org/viewvc?rev=1804736&view=rev
Log:
[FELIX-5674] Inject JSON Parser

Added:
    
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/DefaultParser.java
   (with props)
    
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/DefaultParser.java
   (with props)
    
felix/trunk/converter/serializer/src/main/java/org/osgi/service/serializer/Parser.java
   (with props)
Modified:
    
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonDeserializingImpl.java
    
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonSerializerImpl.java
    
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/YamlDeserializingImpl.java
    
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/YamlSerializerImpl.java
    
felix/trunk/converter/serializer/src/main/java/org/osgi/service/serializer/Deserializing.java
    
felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonParserTest.java

Added: 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/DefaultParser.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/DefaultParser.java?rev=1804736&view=auto
==============================================================================
--- 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/DefaultParser.java
 (added)
+++ 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/DefaultParser.java
 Thu Aug 10 22:17:48 2017
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.serializer.impl.json;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.utils.json.JSONParser;
+import org.osgi.service.serializer.Parser;
+
+public class DefaultParser implements Parser {
+
+    @Override
+    public Map<String, Object> parse(InputStream in)
+    {
+        try {
+            JSONParser parser = new JSONParser(in);
+            return parser.getParsed();
+        } catch (IOException e) {
+            Map<String, Object> report = new HashMap<>();
+            report.put("error", e.getMessage());
+            return report;
+        }
+    }
+
+    @Override
+    public Map<String, Object> parse(CharSequence in) {
+        JSONParser parser = new JSONParser(in);
+        return parser.getParsed();
+    }
+}

Propchange: 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/DefaultParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonDeserializingImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonDeserializingImpl.java?rev=1804736&r1=1804735&r2=1804736&view=diff
==============================================================================
--- 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonDeserializingImpl.java
 (original)
+++ 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonDeserializingImpl.java
 Thu Aug 10 22:17:48 2017
@@ -25,25 +25,26 @@ import java.util.Map;
 import java.util.Scanner;
 
 import org.apache.felix.serializer.impl.Util;
-import org.apache.felix.utils.json.JSONParser;
 import org.osgi.service.serializer.Deserializing;
+import org.osgi.service.serializer.Parser;
 import org.osgi.util.converter.ConversionException;
 import org.osgi.util.converter.Converter;
 
 public class JsonDeserializingImpl<T> implements Deserializing<T> {
     private final Type type;
     private volatile Converter converter;
+    private volatile Parser parser;
 
-    public JsonDeserializingImpl(Converter c, Type t) {
+    public JsonDeserializingImpl(Converter c, Parser p, Type t) {
         converter = c;
+        parser = p;
         type = t;
     }
 
     @Override
     @SuppressWarnings("unchecked")
     public T from(CharSequence in) {
-        JSONParser jp = new JSONParser(in);
-        Map<?,?> m = jp.getParsed();
+        Map<?,?> m = parser.parse(in);
         if (type instanceof Class)
             if (m.getClass().isAssignableFrom((Class<?>) type))
                 return (T) m;
@@ -80,4 +81,10 @@ public class JsonDeserializingImpl<T> im
         converter = c;
         return this;
     }
+
+    @Override
+    public Deserializing<T> with(Parser p) {
+        parser = p;
+        return this;
+    }
 }

Modified: 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonSerializerImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonSerializerImpl.java?rev=1804736&r1=1804735&r2=1804736&view=diff
==============================================================================
--- 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonSerializerImpl.java
 (original)
+++ 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/json/JsonSerializerImpl.java
 Thu Aug 10 22:17:48 2017
@@ -26,6 +26,7 @@ import java.util.concurrent.ConcurrentHa
 
 import org.apache.felix.serializer.impl.AbstractSpecifying;
 import org.osgi.service.serializer.Deserializing;
+import org.osgi.service.serializer.Parser;
 import org.osgi.service.serializer.Serializer;
 import org.osgi.service.serializer.Serializing;
 import org.osgi.util.converter.Converter;
@@ -36,10 +37,11 @@ public class JsonSerializerImpl implemen
     private final Map<String, Object> configuration = new 
ConcurrentHashMap<>();
     private final ThreadLocal<Boolean> threadLocal = new ThreadLocal<>();
     private final Converter converter = Converters.standardConverter();
+    private final Parser parser = new DefaultParser();
 
     @Override
     public <T> Deserializing<T> deserialize(Class<T> cls) {
-        return new JsonDeserializingImpl<T>(converter, cls);
+        return new JsonDeserializingImpl<T>(converter, parser, cls);
     }
 
     @Override
@@ -121,11 +123,11 @@ public class JsonSerializerImpl implemen
 
     @Override
     public <T> Deserializing<T> deserialize(TypeReference<T> ref) {
-        return new JsonDeserializingImpl<T>(converter, ref.getType());
+        return new JsonDeserializingImpl<T>(converter, parser, ref.getType());
     }
 
     @Override @SuppressWarnings("rawtypes")
     public Deserializing<?> deserialize(Type type) {
-        return new JsonDeserializingImpl(converter, type);
+        return new JsonDeserializingImpl(converter, parser, type);
     }
 }

Added: 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/DefaultParser.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/DefaultParser.java?rev=1804736&view=auto
==============================================================================
--- 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/DefaultParser.java
 (added)
+++ 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/DefaultParser.java
 Thu Aug 10 22:17:48 2017
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.serializer.impl.yaml;
+
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.osgi.service.serializer.Parser;
+import org.yaml.snakeyaml.Yaml;
+
+public class DefaultParser implements Parser {
+
+    @Override
+    public Map<String, Object> parse(InputStream in)
+    {
+        Yaml yaml = new Yaml();
+        return toMap(yaml.load(in));
+    }
+
+    @Override
+    public Map<String, Object> parse(CharSequence in) {
+        Yaml yaml = new Yaml();
+        return toMap(yaml.load(in.toString()));
+    }
+
+    @SuppressWarnings( { "unchecked", "rawtypes" } )
+    private Map<String, Object> toMap(Object obj) {
+        if (obj instanceof Map)
+            return (Map)obj;
+
+        Map<String, Object> map = new HashMap<>();
+        map.put("parsed", obj);
+        return map;
+    }
+}

Propchange: 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/DefaultParser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/YamlDeserializingImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/YamlDeserializingImpl.java?rev=1804736&r1=1804735&r2=1804736&view=diff
==============================================================================
--- 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/YamlDeserializingImpl.java
 (original)
+++ 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/YamlDeserializingImpl.java
 Thu Aug 10 22:17:48 2017
@@ -21,20 +21,23 @@ import java.io.InputStream;
 import java.lang.reflect.Type;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
+import java.util.Map;
 import java.util.Scanner;
 
 import org.apache.felix.serializer.impl.Util;
 import org.osgi.service.serializer.Deserializing;
+import org.osgi.service.serializer.Parser;
 import org.osgi.util.converter.ConversionException;
 import org.osgi.util.converter.Converter;
-import org.yaml.snakeyaml.Yaml;
 
 public class YamlDeserializingImpl<T> implements Deserializing<T> {
     private volatile Converter converter;
+    private volatile Parser parser;
     private final Type type;
 
-    YamlDeserializingImpl(Converter c, Type cls) {
+    YamlDeserializingImpl(Converter c, Parser p, Type cls) {
         converter = c;
+        parser = p;
         type = cls;
     }
 
@@ -65,14 +68,12 @@ public class YamlDeserializingImpl<T> im
     @Override
     @SuppressWarnings("unchecked")
     public T from(CharSequence in) {
-        Yaml yaml = new Yaml();
-        Object res = yaml.load(in.toString());
-
+        Map<?,?> m = parser.parse(in);
         if (type instanceof Class)
-            if (res.getClass().isAssignableFrom((Class<?>) type))
-                return (T) res;
+            if (m.getClass().isAssignableFrom((Class<?>) type))
+                return (T) m;
 
-        return (T) converter.convert(res).to(type);
+        return (T) converter.convert(m).to(type);
     }
 
     @Override
@@ -80,4 +81,10 @@ public class YamlDeserializingImpl<T> im
         converter = c;
         return this;
     }
+
+    @Override
+    public Deserializing<T> with(Parser p) {
+        parser = p;
+        return this;
+    }
 }

Modified: 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/YamlSerializerImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/YamlSerializerImpl.java?rev=1804736&r1=1804735&r2=1804736&view=diff
==============================================================================
--- 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/YamlSerializerImpl.java
 (original)
+++ 
felix/trunk/converter/serializer/src/main/java/org/apache/felix/serializer/impl/yaml/YamlSerializerImpl.java
 Thu Aug 10 22:17:48 2017
@@ -21,6 +21,7 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.osgi.service.serializer.Deserializing;
+import org.osgi.service.serializer.Parser;
 import org.osgi.service.serializer.Serializer;
 import org.osgi.service.serializer.Serializing;
 import org.osgi.util.converter.Converter;
@@ -30,20 +31,21 @@ import org.osgi.util.converter.TypeRefer
 public class YamlSerializerImpl implements Serializer {
     private final Map<String, Object> configuration = new 
ConcurrentHashMap<>();
     private final Converter converter = Converters.standardConverter();
+    private final Parser parser = new DefaultParser();
 
     @Override
     public <T> Deserializing<T> deserialize(Class<T> cls) {
-        return new YamlDeserializingImpl<T>(converter, cls);
+        return new YamlDeserializingImpl<T>(converter, parser, cls);
     }
 
     @Override
     public <T> Deserializing<T> deserialize(TypeReference<T> ref) {
-        return new YamlDeserializingImpl<T>(converter, ref.getType());
+        return new YamlDeserializingImpl<T>(converter, parser, ref.getType());
     }
 
     @Override @SuppressWarnings("rawtypes")
     public Deserializing<?> deserialize(Type type) {
-        return new YamlDeserializingImpl(converter, type);
+        return new YamlDeserializingImpl(converter, parser, type);
     }
 
     @Override

Modified: 
felix/trunk/converter/serializer/src/main/java/org/osgi/service/serializer/Deserializing.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/main/java/org/osgi/service/serializer/Deserializing.java?rev=1804736&r1=1804735&r2=1804736&view=diff
==============================================================================
--- 
felix/trunk/converter/serializer/src/main/java/org/osgi/service/serializer/Deserializing.java
 (original)
+++ 
felix/trunk/converter/serializer/src/main/java/org/osgi/service/serializer/Deserializing.java
 Thu Aug 10 22:17:48 2017
@@ -72,4 +72,13 @@ public interface Deserializing<T> {
         * @return This Deserializing object to allow further invocations on it.
         */
        Deserializing<T> with(Converter converter);
+
+    /**
+     * Specify the parser to be used, if an alternative to the default internal
+     * parser is required.
+     * 
+     * @param parser the parser to use.
+     * @return This Serializing object to allow further invocations on it.
+     */
+       Deserializing<T> with(Parser parser);
 }

Added: 
felix/trunk/converter/serializer/src/main/java/org/osgi/service/serializer/Parser.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/main/java/org/osgi/service/serializer/Parser.java?rev=1804736&view=auto
==============================================================================
--- 
felix/trunk/converter/serializer/src/main/java/org/osgi/service/serializer/Parser.java
 (added)
+++ 
felix/trunk/converter/serializer/src/main/java/org/osgi/service/serializer/Parser.java
 Thu Aug 10 22:17:48 2017
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) OSGi Alliance (2016). All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.osgi.service.serializer;
+
+import java.io.InputStream;
+import java.util.Map;
+
+/**
+ * Common interface for a parser, which can be provided by the client.
+ */
+public interface Parser {
+    Map<String, Object> parse(InputStream in);
+    Map<String, Object> parse(CharSequence in);
+}

Propchange: 
felix/trunk/converter/serializer/src/main/java/org/osgi/service/serializer/Parser.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonParserTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonParserTest.java?rev=1804736&r1=1804735&r2=1804736&view=diff
==============================================================================
--- 
felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonParserTest.java
 (original)
+++ 
felix/trunk/converter/serializer/src/test/java/org/apache/felix/serializer/impl/json/JsonParserTest.java
 Thu Aug 10 22:17:48 2017
@@ -22,18 +22,25 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.felix.utils.json.JSONParser;
+import org.junit.Before;
 import org.junit.Test;
+import org.osgi.service.serializer.Parser;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 public class JsonParserTest {
+    private Parser parser;
+
+    @Before
+    public void setup() {
+        parser = new DefaultParser();        
+    }
+
     @Test
     public void testJsonSimple() {
         String json = "{\"hi\": \"ho\", \"ha\": true}";
-        JSONParser jp = new JSONParser(json);
-        Map<String, Object> m = jp.getParsed();
+        Map<String, Object> m = parser.parse(json);
         assertEquals(2, m.size());
         assertEquals("ho", m.get("hi"));
         assertTrue((Boolean) m.get("ha"));
@@ -51,8 +58,7 @@ public class JsonParserTest {
                 + "  ]\n"
                 + "}\n"
                 + "\n";
-        JSONParser jp = new JSONParser(json);
-        Map<String, Object> m = jp.getParsed();
+        Map<String, Object> m = parser.parse(json);
         assertEquals(2, m.size());
         assertEquals("ho", m.get("hi"));
         assertEquals(3, ((List<?>)m.get("ha")).size());
@@ -70,8 +76,7 @@ public class JsonParserTest {
                 + "  ]\r\n"
                 + "}\r\n"
                 + "\r\n";
-        JSONParser jp = new JSONParser(json);
-        Map<String, Object> m = jp.getParsed();
+        Map<String, Object> m = parser.parse(json);
         assertEquals(2, m.size());
         assertEquals("ho", m.get("hi"));
         assertEquals(3, ((List<?>)m.get("ha")).size());
@@ -81,8 +86,7 @@ public class JsonParserTest {
     @SuppressWarnings("unchecked")
     public void testJsonComplex() {
         String json = "{\"a\": [1,2,3,4,5], \"b\": {\"x\": 12, \"y\": 42, 
\"z\": {\"test test\": \"hello hello\"}}, \"ddd\": 12.34}";
-        JSONParser jp = new JSONParser(json);
-        Map<String, Object> m = jp.getParsed();
+        Map<String, Object> m = parser.parse(json);
         assertEquals(3, m.size());
         assertEquals(Arrays.asList(1L, 2L, 3L, 4L, 5L), m.get("a"));
         Map<String, Object> mb = (Map<String, Object>) m.get("b");
@@ -98,8 +102,7 @@ public class JsonParserTest {
     @Test
     public void testJsonArray() {
         String json = "{\"abc\": [\"x\", \"y\", \"z\"]}";
-        JSONParser jp = new JSONParser(json);
-        Map<String, Object> m = jp.getParsed();
+        Map<String, Object> m = parser.parse(json);
         assertEquals(1, m.size());
         assertEquals(Arrays.asList("x", "y", "z"), m.get("abc"));
     }
@@ -107,8 +110,7 @@ public class JsonParserTest {
     @Test
     public void testEmptyJsonArray() {
         String json = "{\"abc\": {\"def\": []}}";
-        JSONParser jp = new JSONParser(json);
-        Map<String, Object> m = jp.getParsed();
+        Map<String, Object> m = parser.parse(json);
         assertEquals(1, m.size());
         Map<String, Object> result = new HashMap<>();
         result.put("def", Collections.emptyList());


Reply via email to