2008-10-10 04:29:06 +02:00
|
|
|
package com.google.gson.functional;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2008-10-10 23:52:02 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2008-10-10 04:29:06 +02:00
|
|
|
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);
|
|
|
|
}
|
2008-10-30 00:31:13 +01:00
|
|
|
|
|
|
|
public void testStringWithEscapedSlashDeserialization() {
|
|
|
|
String value = "/";
|
|
|
|
String json = "'\\/'";
|
|
|
|
String actual = gson.fromJson(json, String.class);
|
|
|
|
assertEquals(value, actual);
|
|
|
|
}
|
2008-10-10 04:29:06 +02:00
|
|
|
}
|