[jjson] r69 committed - added replace line breaks with method, useful for f. e. jquery seriali...

5 views
Skip to first unread message

jj...@googlecode.com

unread,
Aug 2, 2011, 7:03:07 AM8/2/11
to jjso...@googlegroups.com
Revision: 69
Author: grobmeier
Date: Tue Aug 2 03:58:59 2011
Log: added replace line breaks with method, useful for f. e. jquery
serialization
http://code.google.com/p/jjson/source/detail?r=69

Added:

/trunk/jjson/src/test/java/de/grobmeier/jjson/convert/MultilineAnnotatedTestClass2.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/MultilineAnnotatedTestClass2.java
Tue Aug 2 03:58:59 2011
@@ -0,0 +1,28 @@
+/*
+ * 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;
+
+@JSON
+public class MultilineAnnotatedTestClass2 {
+ @JSON(replaceLinebreaksWith = "%0A")
+ private String mys = "bla\ntest";
+ /**
+ * @return the mys
+ */
+ public String getMys() {
+ return mys;
+ }
+}
=======================================
--- /trunk/jjson/src/main/java/de/grobmeier/jjson/convert/JSON.java Mon
Aug 1 08:32:20 2011
+++ /trunk/jjson/src/main/java/de/grobmeier/jjson/convert/JSON.java Tue
Aug 2 03:58:59 2011
@@ -25,4 +25,5 @@
public @interface JSON {
public String dateFormat() default "";
public boolean encodeLinebreaks() default false;
-}
+ public String replaceLinebreaksWith() default "";
+}
=======================================
---
/trunk/jjson/src/main/java/de/grobmeier/jjson/convert/JSONAnnotationEncoder.java
Mon Aug 1 08:32:20 2011
+++
/trunk/jjson/src/main/java/de/grobmeier/jjson/convert/JSONAnnotationEncoder.java
Tue Aug 2 03:58:59 2011
@@ -273,6 +273,10 @@
String replaced = string.replaceAll("\r\n","\\\\r");
replaced = string.replaceAll("\n","\\\\n");
result.append(replaced);
+ } else if (annotation != null
&& !"".equals(annotation.replaceLinebreaksWith())) {
+ String replaced = string.replaceAll("\r\n",
annotation.replaceLinebreaksWith());
+ replaced = replaced.replaceAll("\n",
annotation.replaceLinebreaksWith());
+ result.append(replaced);
} else {
result.append(string);
}
=======================================
---
/trunk/jjson/src/test/java/de/grobmeier/jjson/convert/JSONAnnotationEncoderTest.java
Mon Aug 1 08:32:20 2011
+++
/trunk/jjson/src/test/java/de/grobmeier/jjson/convert/JSONAnnotationEncoderTest.java
Tue Aug 2 03:58:59 2011
@@ -44,6 +44,16 @@
MultilineAnnotatedTestClass c = new MultilineAnnotatedTestClass();
JSONAnnotationEncoder encoder = new JSONAnnotationEncoder();
String json = encoder.encode(c);
- TestCase.assertEquals("{\"mys\":\"bla\ntest\"}", json);
+ TestCase.assertEquals("{\"mys\":\"bla\\ntest\"}", json);
+ System.out.println(json);
+ }
+
+ @Test
+ public void testReplaceLineBreaksWithClass() throws Exception {
+ MultilineAnnotatedTestClass2 c = new MultilineAnnotatedTestClass2();
+ JSONAnnotationEncoder encoder = new JSONAnnotationEncoder();
+ String json = encoder.encode(c);
+ TestCase.assertEquals("{\"mys\":\"bla%0Atest\"}", json);
+ System.out.println(json);
}
}

Reply all
Reply to author
Forward
0 new messages