Skip to content

Commit

Permalink
fix enable write string value in JSONType for issue #2431
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 committed Apr 12, 2024
1 parent d702e91 commit 7c503fa
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,16 @@ public enum Feature {
}
}

public Feature[] getFeaturesByMask(long mask) {
List<Feature> list = new ArrayList<>();
for (Feature feature : values()) {
if ((feature.mask & mask) != 0) {
list.add(feature);
}
}
return list.toArray(new Feature[]{});
}

public static final class Path {
public static final Path ROOT = new Path(null, "$");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3791,6 +3791,14 @@ void genVariantsMethodBefore(boolean jsonb) {
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "getFeatures", "()J", false);
mw.visitVarInsn(Opcodes.LSTORE, var2(CONTEXT_FEATURES));

mw.visitVarInsn(Opcodes.ALOAD, 1);
mw.visitFieldInsn(Opcodes.GETFIELD, TYPE_JSON_WRITER, "context", "L" + JSONWriter.Context.class.getName().replace('.', '/') + ";");
mw.visitVarInsn(Opcodes.ALOAD, 1);
mw.visitVarInsn(Opcodes.ALOAD, 0);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameType, "getFeatures", "()J", false);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "getFeaturesByMask", "(J)[L" + JSONWriter.Feature.class.getName().replace('.', '/') + ";", false);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, JSONWriter.Context.class.getName().replace('.', '/'), "config", "([L" + JSONWriter.Feature.class.getName().replace('.', '/') + ";)V", false);

if (!jsonb) {
Label l1 = new Label(), l2 = new Label();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.alibaba.fastjson2.issues_2400;

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

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class Issue2431 {
@Test
public void test() {
Bean bean = new Bean();
bean.setB((byte)1);
bean.setBs(Arrays.asList((byte)1));
bean.setS((short)1);
bean.setSs(Arrays.asList((short)1));
bean.setI(1);
bean.setIs(Arrays.asList(1));
bean.setL(1L);
bean.setLs(Arrays.asList(1L));
bean.setF(1.0F);
bean.setFs(Arrays.asList(1.0F));
bean.setD(1.0);
bean.setDs(Arrays.asList(1.0));
assertEquals(
"{\"b\":\"1\",\"bs\":[\"1\"],\"d\":\"1.0\",\"ds\":[\"1.0\"],\"f\":\"1.0\",\"fs\":[\"1.0\"],\"i\":\"1\",\"is\":[\"1\"],\"l\":\"1\",\"ls\":[\"1\"],\"s\":\"1\",\"ss\":[\"1\"]}",
JSON.toJSONString(bean));
}

@Data
@JSONType(serializeFeatures = JSONWriter.Feature.WriteNonStringValueAsString)
public static class Bean {
private Byte b;
private List<Byte> bs;
private Short s;
private List<Short> ss;
private Integer i;
private List<Integer> is;
private Long l;
private List<Long> ls;
private Float f;
private List<Float> fs;
private Double d;
private List<Double> ds;
}
}

0 comments on commit 7c503fa

Please sign in to comment.