Skip to content

Commit

Permalink
fix WriteNonStringValueAsString not work on Boolean type, for issue #…
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 11, 2024
1 parent 7f22132 commit a3ea6c9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
9 changes: 2 additions & 7 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -3131,14 +3131,9 @@ public final void write(List array) {

@Override
public final void writeString(boolean value) {
boolean writeAsString = (context.features & WriteNonStringValueAsString.mask) == 0;
if (writeAsString) {
writeQuote();
}
chars[off++] = quote;
writeBool(value);
if (writeAsString) {
writeQuote();
}
chars[off++] = quote;
}

@Override
Expand Down
10 changes: 3 additions & 7 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,10 @@ public final void writeString(List<String> list) {

@Override
public final void writeString(boolean value) {
boolean writeAsString = (context.features & WriteNonStringValueAsString.mask) == 0;
if (writeAsString) {
writeQuote();
}
byte quote = (byte) this.quote;
bytes[off++] = quote;
writeBool(value);
if (writeAsString) {
writeQuote();
}
bytes[off++] = quote;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type f
}

boolean value = (Boolean) object;
if ((features & WriteNonStringValueAsString.mask) != 0) {
if ((jsonWriter.getFeatures(features) & WriteNonStringValueAsString.mask) != 0) {
jsonWriter.writeString(value);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public void testSingle() {
assertEquals("3.14", JSON.toJSONString(new BigDecimal("3.14")));

// as string
assertEquals("true", JSON.toJSONString(true, JSONWriter.Feature.WriteNonStringValueAsString));
assertEquals("\"true\"", JSON.toJSONString(true, JSONWriter.Feature.WriteNonStringValueAsString));
assertEquals("\"false\"", JSON.toJSONString(false, JSONWriter.Feature.WriteNonStringValueAsString));
assertEquals("\"true\"", new String(JSON.toJSONBytes(true, JSONWriter.Feature.WriteNonStringValueAsString)));
assertEquals("\"false\"", new String(JSON.toJSONBytes(false, JSONWriter.Feature.WriteNonStringValueAsString)));
assertEquals("\"123\"", JSON.toJSONString(123, JSONWriter.Feature.WriteNonStringValueAsString));
assertEquals("\"123\"", JSON.toJSONString(Integer.valueOf("123"), JSONWriter.Feature.WriteNonStringValueAsString));
assertEquals("\"3.14\"", JSON.toJSONString(3.14, JSONWriter.Feature.WriteNonStringValueAsString));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.alibaba.fastjson2.issues_2500;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2560 {
@Test
public void mutatedTest() {
List<TestJson> list = new ArrayList<>();
list.add(new TestJson(true, Boolean.TRUE));
list.add(new TestJson(false, Boolean.FALSE));
String json = JSON.toJSONString(list, JSONWriter.Feature.WriteNonStringValueAsString);
List<TestJson> list2 = JSON.parseArray(json, TestJson.class);
assertEquals(list, list2);
String expected = "[{\"b\":\"true\",\"b2\":\"true\"},{\"b\":\"false\",\"b2\":\"false\"}]";
assertEquals(expected, json);
assertEquals(expected, new String(JSON.toJSONBytes(list, JSONWriter.Feature.WriteNonStringValueAsString)));
}

@Data
@AllArgsConstructor
public static class TestJson {
private boolean b;
private Boolean b2;
}

@Test
public void mutatedTest1() {
List<TestJson1> list = new ArrayList<>();
list.add(new TestJson1(1, 1));
list.add(new TestJson1(2, 2));
String json = JSON.toJSONString(list, JSONWriter.Feature.WriteNonStringValueAsString);
System.out.println(json);
List<TestJson1> list2 = JSON.parseArray(json, TestJson1.class);
assertEquals(list, list2);
assertEquals("[{\"a\":\"1\",\"a2\":\"1\"},{\"a\":\"2\",\"a2\":\"2\"}]", json);
}

@Data
@AllArgsConstructor
public static class TestJson1 {
private int a;
private Integer a2;
}
}

0 comments on commit a3ea6c9

Please sign in to comment.