Added:
/trunk/jjson/src/test/java/de/grobmeier/jjson/convert/MultilineAnnotatedTestClass.java
Modified:
/trunk/jjson/src/main/java/de/grobmeier/jjson/convert/JSON.java
/trunk/jjson/src/main/java/de/grobmeier/jjson/convert/JSONAnnotationEncoder.java
/trunk/jjson/src/test/java/de/grobmeier/jjson/convert/JSONAnnotationEncoderTest.java
=======================================
--- /dev/null
+++
/trunk/jjson/src/test/java/de/grobmeier/jjson/convert/MultilineAnnotatedTestClass.java
Mon Aug 1 08:32:20 2011
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2007 Christian Grobmeier
+ *
+ * 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 de.grobmeier.jjson.convert;
+
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import de.grobmeier.jjson.convert.JSON;
+
+@JSON
+public class MultilineAnnotatedTestClass {
+ @JSON
+ private String mys = "bla\ntest";
+ /**
+ * @return the mys
+ */
+ public String getMys() {
+ return mys;
+ }
+}
=======================================
--- /trunk/jjson/src/main/java/de/grobmeier/jjson/convert/JSON.java Fri
Jul 1 02:22:16 2011
+++ /trunk/jjson/src/main/java/de/grobmeier/jjson/convert/JSON.java Mon
Aug 1 08:32:20 2011
@@ -24,4 +24,5 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface JSON {
public String dateFormat() default "";
-}
+ public boolean encodeLinebreaks() default false;
+}
=======================================
---
/trunk/jjson/src/main/java/de/grobmeier/jjson/convert/JSONAnnotationEncoder.java
Wed Jul 13 07:00:00 2011
+++
/trunk/jjson/src/main/java/de/grobmeier/jjson/convert/JSONAnnotationEncoder.java
Mon Aug 1 08:32:20 2011
@@ -71,7 +71,7 @@
if(result == null) {
builder.append(NULL);
} else if(result.getClass().isAssignableFrom(String.class)) {
- encodeString(((String)result), builder);
+ encodeString(((String)result), builder, annotation);
} else if(result.getClass().isAssignableFrom(Integer.class)) {
encodeInteger((Integer)result, builder);
} else if(result.getClass().isAssignableFrom(Long.class)) {
@@ -89,7 +89,7 @@
} else if(hasInterface(result, List.class)) {
encodeList((List<Object>)result, builder);
} else if(hasInterface(result, Map.class)) {
- encodeMap((Map<Object, Object>)result, builder);
+ encodeMap((Map<Object, Object>)result, builder, annotation);
} else if(result.getClass().isAssignableFrom(Date.class)) {
encodeDate((Date)result, builder, annotation);
} else if(result.getClass().isArray()) {
@@ -119,14 +119,14 @@
format = new SimpleDateFormat(customFormat);
formatterPool.put(customFormat, format);
}
- encodeString(format.format(result), builder);
+ encodeString(format.format(result), builder, annotation);
} else {
- encodeString(DEFAULT_FORMAT.format(result), builder);
+ encodeString(DEFAULT_FORMAT.format(result), builder, annotation);
}
}
- private void encodeMap(Map<Object, Object> result, StringBuilder
builder) throws JSONException {
+ private void encodeMap(Map<Object, Object> result, StringBuilder
builder, JSON annotation) throws JSONException {
boolean first = true;
builder.append(BRACKET_LEFT);
Set<Entry<Object, Object>> entries = result.entrySet();
@@ -137,7 +137,7 @@
first = false;
}
Entry<Object, Object> entry = (Entry<Object, Object>) iterator.next();
- encodeString(entry.getKey().toString(), builder);
+ encodeString(entry.getKey().toString(), builder, annotation);
builder.append(COLON);
encode(entry.getValue(), builder,null);
}
@@ -204,9 +204,9 @@
Method method = c.getClass().getMethod(methodName,
(Class[])null);
Object result = method.invoke(c, (Object[])null);
- encodeString(field.getName(), builder);
+ encodeString(field.getName(), builder,
(JSON)annotation);
builder.append(COLON);
- encode(result, builder,(JSON)annotation);
+ encode(result, builder, (JSON)annotation);
} catch (SecurityException e) {
throw new JSONException(e);
} catch (NoSuchMethodException e) {
@@ -247,7 +247,7 @@
name = name.replaceFirst("get", "");
name = name.substring(0, 1).toLowerCase() +
name.substring(1);
}
- encodeString(name, builder);
+ encodeString(name, builder, (JSON)annotation);
builder.append(COLON);
encode(result, builder, (JSON)annotation);
} catch (SecurityException e) {
@@ -264,12 +264,18 @@
}
}
- private void encodeString(String string, StringBuilder result) {
+ private void encodeString(String string, StringBuilder result, JSON
annotation) {
if(string == null) {
result.append(NULL);
} else {
result.append(QUOTE);
- result.append(string);
+ if(annotation != null && annotation.encodeLinebreaks()) {
+ String replaced = string.replaceAll("\r\n","\\\\r");
+ replaced = string.replaceAll("\n","\\\\n");
+ result.append(replaced);
+ } else {
+ result.append(string);
+ }
result.append(QUOTE);
}
}
=======================================
---
/trunk/jjson/src/test/java/de/grobmeier/jjson/convert/JSONAnnotationEncoderTest.java
Wed Jul 13 07:00:00 2011
+++
/trunk/jjson/src/test/java/de/grobmeier/jjson/convert/JSONAnnotationEncoderTest.java
Mon Aug 1 08:32:20 2011
@@ -38,4 +38,12 @@
String json = encoder.encode(c);
TestCase.assertEquals(expected, json);
}
-}
+
+ @Test
+ public void testMultilineStringClass() throws Exception {
+ MultilineAnnotatedTestClass c = new MultilineAnnotatedTestClass();
+ JSONAnnotationEncoder encoder = new JSONAnnotationEncoder();
+ String json = encoder.encode(c);
+ TestCase.assertEquals("{\"mys\":\"bla\ntest\"}", json);
+ }
+}