d74ecbfe82
enables Gson to handle much larger strings (~10s of MB) than previously possible (<100kb). This also reduces memory and stack requirements, and increases Gson performance as well.
151 lines
4.9 KiB
Java
151 lines
4.9 KiB
Java
package com.google.gson.functional;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.JsonParseException;
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
/**
|
|
* Functional tests for Json serialization and deserialization of strings.
|
|
*
|
|
* @author Inderjeet Singh
|
|
* @author Joel Leitch
|
|
*/
|
|
public class StringTest extends TestCase {
|
|
private Gson gson;
|
|
|
|
@Override
|
|
protected void setUp() throws Exception {
|
|
super.setUp();
|
|
gson = new Gson();
|
|
}
|
|
|
|
public void testStringValueSerialization() throws Exception {
|
|
String value = "someRandomStringValue";
|
|
assertEquals('"' + value + '"', gson.toJson(value));
|
|
}
|
|
|
|
public void testStringValueDeserialization() throws Exception {
|
|
String value = "someRandomStringValue";
|
|
String actual = gson.fromJson("\"" + value + "\"", String.class);
|
|
assertEquals(value, actual);
|
|
}
|
|
|
|
public void testSingleQuoteInStringSerialization() throws Exception {
|
|
String valueWithQuotes = "beforeQuote'afterQuote";
|
|
String jsonRepresentation = gson.toJson(valueWithQuotes);
|
|
assertEquals(valueWithQuotes, gson.fromJson(jsonRepresentation, String.class));
|
|
}
|
|
|
|
public void testEscapedCtrlNInStringSerialization() throws Exception {
|
|
String value = "a\nb";
|
|
String json = gson.toJson(value);
|
|
assertEquals("\"a\\nb\"", json);
|
|
}
|
|
|
|
public void testEscapedCtrlNInStringDeserialization() throws Exception {
|
|
String json = "'a\\nb'";
|
|
String actual = gson.fromJson(json, String.class);
|
|
assertEquals("a\nb", actual);
|
|
}
|
|
|
|
public void testEscapedCtrlRInStringSerialization() throws Exception {
|
|
String value = "a\rb";
|
|
String json = gson.toJson(value);
|
|
assertEquals("\"a\\rb\"", json);
|
|
}
|
|
|
|
public void testEscapedCtrlRInStringDeserialization() throws Exception {
|
|
String json = "'a\\rb'";
|
|
String actual = gson.fromJson(json, String.class);
|
|
assertEquals("a\rb", actual);
|
|
}
|
|
|
|
public void testEscapedBackslashInStringSerialization() throws Exception {
|
|
String value = "a\\b";
|
|
String json = gson.toJson(value);
|
|
assertEquals("\"a\\\\b\"", json);
|
|
}
|
|
|
|
public void testEscapedBackslashInStringDeserialization() throws Exception {
|
|
String actual = gson.fromJson("'a\\\\b'", String.class);
|
|
assertEquals("a\\b", actual);
|
|
}
|
|
|
|
public void testSingleQuoteInStringDeserialization() throws Exception {
|
|
String value = "beforeQuote'afterQuote";
|
|
String actual = gson.fromJson("\"" + value + "\"", String.class);
|
|
assertEquals(value, actual);
|
|
}
|
|
|
|
public void testEscapingQuotesInStringSerialization() throws Exception {
|
|
String valueWithQuotes = "beforeQuote\"afterQuote";
|
|
String jsonRepresentation = gson.toJson(valueWithQuotes);
|
|
String target = gson.fromJson(jsonRepresentation, String.class);
|
|
assertEquals(valueWithQuotes, target);
|
|
}
|
|
|
|
public void testEscapingQuotesInStringDeserialization() throws Exception {
|
|
String value = "beforeQuote\\\"afterQuote";
|
|
String actual = gson.fromJson("\"" + value + "\"", String.class);
|
|
String expected = "beforeQuote\"afterQuote";
|
|
assertEquals(expected, actual);
|
|
}
|
|
|
|
public void testStringValueAsSingleElementArraySerialization() throws Exception {
|
|
String[] target = {"abc"};
|
|
assertEquals("[\"abc\"]", gson.toJson(target));
|
|
assertEquals("[\"abc\"]", gson.toJson(target, String[].class));
|
|
}
|
|
|
|
public void testStringValueAsSingleElementArrayDeserialization() throws Exception {
|
|
String value = "someRandomStringValue";
|
|
String actual = gson.fromJson("[\"" + value + "\"]", String.class);
|
|
assertEquals(value, actual);
|
|
}
|
|
|
|
public void testReallyLongStringsDeserialization() throws Exception {
|
|
StringBuilder sb = new StringBuilder(8096);
|
|
sb.append("Once upon a time there was a really long string that caused a StackOverFlowError\n");
|
|
sb.append("and now it is fixed and instead throws a JsonParserException.....Yippie!!!\n");
|
|
sb.append("Wow....that is a really long string that is meant to be an exception stack trace, ");
|
|
sb.append("but is not :( \n\n\n\n\n\n.");
|
|
sb.append("lalalalala \n\n\n.");
|
|
sb.append("C'est la vie!!! \n\n\n\n\n");
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
sb.append(sb.toString());
|
|
}
|
|
|
|
while (true) {
|
|
try {
|
|
String stackTrace = sb.toString();
|
|
sb.append(stackTrace);
|
|
String json = "{\"message\":\"Error message.\","
|
|
+ "\"stackTrace\":\"" + stackTrace + "\"}";
|
|
parseLongJson(json);
|
|
} catch (JsonParseException expected) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void parseLongJson(String json) throws JsonParseException {
|
|
ExceptionHolder target = gson.fromJson(json, ExceptionHolder.class);
|
|
assertTrue(target.message.contains("Error"));
|
|
assertTrue(target.stackTrace.contains("Yippie"));
|
|
}
|
|
|
|
private static class ExceptionHolder {
|
|
public final String message;
|
|
public final String stackTrace;
|
|
public ExceptionHolder() {
|
|
this("", "");
|
|
}
|
|
public ExceptionHolder(String message, String stackTrace) {
|
|
this.message = message;
|
|
this.stackTrace = stackTrace;
|
|
}
|
|
}
|
|
}
|