Skip to content

Commit

Permalink
removed custom linebreak handling. replaced escaping with commons lan…
Browse files Browse the repository at this point in the history
…g code
  • Loading branch information
grobmeier committed Mar 16, 2012
1 parent 7d7619d commit 8437dd1
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 18 deletions.
2 changes: 0 additions & 2 deletions jjson/src/main/java/de/grobmeier/jjson/convert/JSON.java
Expand Up @@ -24,6 +24,4 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface JSON {
public String dateFormat() default "";
public boolean encodeLinebreaks() default false;
public String replaceLinebreaksWith() default "";
}
Expand Up @@ -30,13 +30,14 @@
import java.util.Set;

import de.grobmeier.jjson.JSONException;
import de.grobmeier.jjson.translator.StringEscapeUtils;

/**
*
*/
public class JSONAnnotationEncoder {
// Key signs
// TODO: use allready defined Openener/Closer Enums from basic decoder
// TODO: use already defined Openener/Closer Enums from basic decoder
private static final String QUOTE = "\"";
private static final String PRIMITIVE_BOOLEAN = "boolean";
private static final String ARRAY_RIGHT = "]";
Expand Down Expand Up @@ -267,22 +268,14 @@ private void serializeMethods(Object c, StringBuilder builder) throws JSONExcept
}
}

private void encodeString(String string, StringBuilder result, JSON annotation) {
private void encodeString(String string, StringBuilder result, JSON annotation) {
if(string == null) {
result.append(NULL);
} else {
result.append(QUOTE);
if(annotation != null && annotation.encodeLinebreaks()) {
String replaced = string.replaceAll("\r", CARRIAGE_RETURN);
replaced = replaced.replaceAll("\n", LINE_FEED);
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);
}

result.append(StringEscapeUtils.escapeJava(string));

result.append(QUOTE);
}
}
Expand Down
Expand Up @@ -79,6 +79,15 @@ public void testReplaceLineBreaksWithClass() throws Exception {
MultilineAnnotatedTestClass2 c = new MultilineAnnotatedTestClass2();
JSONAnnotationEncoder encoder = new JSONAnnotationEncoder();
String json = encoder.encode(c);
TestCase.assertEquals("{\"mys\":\"bla%0Atest\"}", json);
TestCase.assertEquals("{\"mys\":\"bla\\ntest\"}", json);
}

@Test
public void testSpecialChar() throws Exception {
SpecialCharAnnotatedTestClass c = new SpecialCharAnnotatedTestClass();
JSONAnnotationEncoder encoder = new JSONAnnotationEncoder();
String json = encoder.encode(c);
TestCase.assertEquals("{\"value\":\"b[l]u{b}\\\"test\"}", json);
System.out.println(json);
}
}
Expand Up @@ -17,7 +17,7 @@

@JSON
public class MultilineAnnotatedTestClass {
@JSON(encodeLinebreaks = true)
@JSON()
private String mys = "bla\ntest";
/**
* @return the mys
Expand Down
Expand Up @@ -17,7 +17,7 @@

@JSON
public class MultilineAnnotatedTestClass2 {
@JSON(replaceLinebreaksWith = "%0A")
@JSON()
private String mys = "bla\ntest";
/**
* @return the mys
Expand Down
@@ -0,0 +1,33 @@
/*
* 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 de.grobmeier.jjson.convert.JSON;

@JSON
public class SpecialCharAnnotatedTestClass {

@JSON
private String value = "b[l]u{b}\"test";

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}
@@ -0,0 +1,39 @@
/*
* Copyright 2012 Christian Grobmeier Software.
*
* 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.translator;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
*
*/
public class StringEscapeUtilsTest {

public StringEscapeUtilsTest() {
}

/**
* Test of escapeJava method, of class StringEscapeUtils.
*/
@Test
public void testEscapeJava() {
String input = "hello\"test";
String expResult = "hello\\\"test";
String result = StringEscapeUtils.escapeJava(input);
assertEquals(expResult, result);
}
}

0 comments on commit 8437dd1

Please sign in to comment.