Skip to content

Commit d837d23

Browse files
committed
fix #126, surrogate failed to serialize in gson mode
1 parent 5a4b389 commit d837d23

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/main/java/com/jsoniter/extra/GsonCompatibilityMode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ public void encode(Object obj, JsonStream stream) throws IOException {
287287
if (i >= value.length()) { // unless we hit the end?
288288
break;
289289
}
290+
i++;
291+
c = value.charAt(i);
290292
int firstPart = _surrogate;
291293
_surrogate = 0;
292294
// Ok, then, is the second part valid?

src/main/java/com/jsoniter/output/ReflectionObjectEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public ReflectionObjectEncoder(ClassInfo classInfo) {
3535
public void encode(Object obj, JsonStream stream) throws IOException {
3636
try {
3737
enocde_(obj, stream);
38-
} catch (RuntimeException e) {
38+
} catch (JsonException e) {
3939
throw e;
4040
} catch (Exception e) {
4141
throw new JsonException(e);

src/test/java/com/jsoniter/output/TestGson.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,22 @@ public boolean shouldSkipClass(Class<?> clazz) {
293293
output = JsonStream.serialize(config, obj);
294294
assertEquals("{\"field3\":\"field3\"}", output);
295295
}
296+
297+
298+
private static class TestObject {
299+
private String test;
300+
}
301+
302+
public void test_surrogate() {
303+
GsonCompatibilityMode gsonConfig =
304+
new GsonCompatibilityMode.Builder()
305+
.disableHtmlEscaping()
306+
.build();
307+
308+
String input = "{\"test\":\"lorem-\uD83D\uDC44\uD83D\uDC40\"}";
309+
TestObject testObject = new Gson().fromJson(input, TestObject.class);
310+
311+
System.out.println("Gson: " + new Gson().toJson(testObject));
312+
System.out.println("jsoniter: " + JsonStream.serialize(gsonConfig, testObject));
313+
}
296314
}

0 commit comments

Comments
 (0)