Author: davidb Date: Tue Jun 14 10:38:43 2016 New Revision: 1748381 URL: http://svn.apache.org/viewvc?rev=1748381&view=rev Log: Felix Converter Service - Initial YAML codec
Added: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java 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/JsonEncodingImpl.java felix/trunk/converter/src/main/java/org/osgi/service/converter/Encoding.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=1748381&r1=1748380&r2=1748381&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 Jun 14 10:38:43 2016 @@ -34,7 +34,7 @@ import org.osgi.service.converter.TypeRe public class JsonCodecImpl implements Codec { private Map<String, Object> configuration = new ConcurrentHashMap<>(); private ThreadLocal<Boolean> threadLocal = new ThreadLocal<>(); - private Converter converter = new ConverterImpl(); // TODO inject? + private Converter converter = new ConverterImpl(); @Override public Codec with(Converter c) { 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=1748381&r1=1748380&r2=1748381&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 Jun 14 10:38:43 2016 @@ -20,7 +20,6 @@ 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; @@ -47,9 +46,9 @@ public class JsonEncodingImpl implements } @Override - public void to(OutputStream os) { + public void to(OutputStream os, Charset charset) { try { - os.write(encode(object).getBytes(StandardCharsets.UTF_8)); + os.write(encode(object).getBytes(charset)); } catch (IOException e) { throw new RuntimeException(e); } @@ -61,7 +60,7 @@ public class JsonEncodingImpl implements } @SuppressWarnings("rawtypes") - public String encode(Object obj) { + private String encode(Object obj) { if (obj == null) { return ignoreNull() ? "" : "null"; } @@ -111,7 +110,7 @@ public class JsonEncodingImpl implements @SuppressWarnings({ "rawtypes", "unchecked" }) private String encodeMap(Map m) { StringBuilder sb = new StringBuilder("{"); - for (Entry<?,?> entry : (Set<Entry>) m.entrySet()) { + for (Entry entry : (Set<Entry>) m.entrySet()) { if (entry.getKey() == null || entry.getValue() == null) if (ignoreNull()) continue; @@ -135,12 +134,6 @@ public class JsonEncodingImpl implements } @Override - public void to(OutputStream out, Charset charset) { - // TODO Auto-generated method stub - - } - - @Override public Appendable to(Appendable out) { // TODO Auto-generated method stub return null; Added: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java?rev=1748381&view=auto ============================================================================== --- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java (added) +++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlCodecImpl.java Tue Jun 14 10:38:43 2016 @@ -0,0 +1,62 @@ +/* + * 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.converter.impl.yaml; + +import java.lang.reflect.Type; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.felix.converter.impl.ConverterImpl; +import org.osgi.service.converter.Codec; +import org.osgi.service.converter.Converter; +import org.osgi.service.converter.Decoding; +import org.osgi.service.converter.Encoding; +import org.osgi.service.converter.TypeReference; + +public class YamlCodecImpl implements Codec { + private Map<String, Object> configuration = new ConcurrentHashMap<>(); + private Converter converter = new ConverterImpl(); + + @Override + public Codec with(Converter c) { + converter = c; + return this; + } + + @Override + public <T> Decoding<T> decode(Class<T> cls) { + // TODO Auto-generated method stub + return null; + } + + @Override + public <T> Decoding<T> decode(TypeReference<T> ref) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Decoding<?> decode(Type type) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Encoding encode(Object obj) { + return new YamlEncodingImpl(converter, configuration, obj); + } +} Added: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java?rev=1748381&view=auto ============================================================================== --- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java (added) +++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/yaml/YamlEncodingImpl.java Tue Jun 14 10:38:43 2016 @@ -0,0 +1,116 @@ +/* + * 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.converter.impl.yaml; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.osgi.service.converter.Converter; +import org.osgi.service.converter.Encoding; + +public class YamlEncodingImpl implements Encoding { + private final Converter converter; + private final Map<String, Object> configuration; + private final Object object; + private final int indentation = 2; + + public YamlEncodingImpl(Converter c, Map<String, Object> cfg, Object obj) { + converter = c; + configuration = cfg; + object = obj; + } + + @Override + public Encoding pretty() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void to(OutputStream os, Charset charset) { + try { + os.write(encode(object).getBytes(charset)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public Appendable to(Appendable out) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String toString() { + return encode(object); + } + + private String encode(Object obj) { + return encode(obj, 0); + } + + @SuppressWarnings("rawtypes") + private String encode(Object obj, int level) { + if (obj == null) + return ""; + + if (obj instanceof Map) { + return encodeMap((Map) obj, level); + } else if (obj instanceof Number) { + return obj.toString(); + } else if (obj instanceof Boolean) { + return obj.toString(); + } + + return "'" + converter.convert(obj).to(String.class) + "'"; + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private String encodeMap(Map m, int level) { + StringBuilder sb = new StringBuilder(); + for (Entry entry : (Set<Entry>) m.entrySet()) { + sb.append(getIdentPrefix(level)); + sb.append(entry.getKey().toString()); + sb.append(": "); + sb.append(needsNewlineBefore(entry.getValue())); + sb.append(encode(entry.getValue(), level + 1)); + sb.append("\n"); + } + + return sb.toString(); + } + + private Object needsNewlineBefore(Object value) { + if (value instanceof Map) + return "\n"; + + return ""; + } + + private String getIdentPrefix(int level) { + int numSpaces = indentation * level; + StringBuilder sb = new StringBuilder(numSpaces); + for (int i=0; i < numSpaces; i++) + sb.append(' '); + return sb.toString(); + } +} Modified: felix/trunk/converter/src/main/java/org/osgi/service/converter/Encoding.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/osgi/service/converter/Encoding.java?rev=1748381&r1=1748380&r2=1748381&view=diff ============================================================================== --- felix/trunk/converter/src/main/java/org/osgi/service/converter/Encoding.java (original) +++ felix/trunk/converter/src/main/java/org/osgi/service/converter/Encoding.java Tue Jun 14 10:38:43 2016 @@ -17,6 +17,7 @@ package org.osgi.service.converter; import java.io.OutputStream; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; /** * Interface to specify the target of the encoding operation. @@ -39,7 +40,9 @@ public interface Encoding { * * @param out The output stream to use. */ - void to(OutputStream out); + default void to(OutputStream os) { + to(os, StandardCharsets.UTF_8); + } /** * Use an output stream as the target of the encoding operation. Added: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java?rev=1748381&view=auto ============================================================================== --- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java (added) +++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/yaml/YamlSerializationTest.java Tue Jun 14 10:38:43 2016 @@ -0,0 +1,45 @@ +/* + * 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.converter.impl.yaml; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class YamlSerializationTest { + @Test + public void testComplexMapSerialization() { + Map<String, Object> m = new LinkedHashMap<>(); + m.put("sKey", "a string"); + m.put("iKey", 42); + m.put("bKey", true); + m.put("noKey", null); + m.put("simpleArray", new int[] {1,2,3}); + + Map<String, Object> m1 = new HashMap<>(); + m1.put("a", 1L); + m1.put("b", "hello"); + m.put("simpleObject", m1); + + String expected = ""; + assertEquals(expected, new YamlCodecImpl().encode(m).toString()); + } +}