-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #308 from json-iterator/289-fixed-escape-unicode-p…
…ublic-api-implementation Fixed public api 'public static String serialize(boolean escapeUnicod…
- Loading branch information
Showing
4 changed files
with
59 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,40 @@ | ||
package com.jsoniter.output; | ||
|
||
import com.jsoniter.spi.Config; | ||
import com.jsoniter.spi.Config.Builder; | ||
import com.jsoniter.spi.JsoniterSpi; | ||
import java.io.ByteArrayOutputStream; | ||
import junit.framework.TestCase; | ||
|
||
public class TestString extends TestCase { | ||
|
||
public static final String UTF8_GREETING = "Привет čau 你好 ~"; | ||
|
||
public void test_unicode() { | ||
String output = JsonStream.serialize(new Config.Builder().escapeUnicode(false).build(), "中文"); | ||
assertEquals("\"中文\"", output); | ||
} | ||
public void test_unicode_tilde() { | ||
String output = JsonStream.serialize(new Config.Builder().escapeUnicode(false).build(), "~"); | ||
assertEquals("\"~\"", output); | ||
final String tilde = "~"; | ||
String output = JsonStream.serialize(new Config.Builder().escapeUnicode(false).build(), tilde); | ||
assertEquals("\""+tilde+"\"", output); | ||
} | ||
public void test_escape_unicode() { | ||
final Config config = new Builder().escapeUnicode(false).build(); | ||
|
||
assertEquals("\""+UTF8_GREETING+"\"", JsonStream.serialize(config, UTF8_GREETING)); | ||
assertEquals("\""+UTF8_GREETING+"\"", JsonStream.serialize(config.escapeUnicode(), UTF8_GREETING.getClass(), UTF8_GREETING)); | ||
} | ||
public void test_escape_control_character() { | ||
String output = JsonStream.serialize(new String(new byte[]{0})); | ||
assertEquals("\"\\u0000\"", output); | ||
} | ||
public void test_serialize_into_output_stream() { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
boolean escapeUnicode = JsoniterSpi.getCurrentConfig().escapeUnicode(); | ||
JsoniterSpi.setCurrentConfig(JsoniterSpi.getCurrentConfig().copyBuilder().escapeUnicode(false).build()); | ||
JsonStream.serialize(String.class, UTF8_GREETING, baos); | ||
JsoniterSpi.setCurrentConfig(JsoniterSpi.getCurrentConfig().copyBuilder().escapeUnicode(escapeUnicode).build()); | ||
assertEquals("\"" + UTF8_GREETING + "\"", baos.toString()); | ||
} | ||
} |