Skip to content

Commit

Permalink
serialize int support JSONField(format='xxx')
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jun 29, 2023
1 parent 9251364 commit 93eee1d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
22 changes: 22 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,28 @@ public void writeInt16(short[] value) {

public abstract void writeInt32(int value);

public final void writeInt32(int value, DecimalFormat format) {
if (format == null || jsonb) {
writeInt32(value);
return;
}

writeString(
format.format(value)
);
}

public final void writeInt32(int value, String format) {
if (format == null || jsonb) {
writeInt32(value);
return;
}

writeString(
String.format(format, value)
);
}

public abstract void writeInt64(long i);

public void writeMillis(long i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public final void writeInt32(JSONWriter jsonWriter, int value) {
return;
}
writeFieldName(jsonWriter);
jsonWriter.writeInt32(value);
if (format != null) {
jsonWriter.writeInt32(value, format);
} else {
jsonWriter.writeInt32(value);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3018,14 +3018,20 @@ private void gwFieldValueInt32V(
mw.visitLabel(notDefaultValue_);

gwFieldName(mwc, i);

mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
mw.visitVarInsn(Opcodes.ILOAD, FIELD_VALUE);
if ("string".equals(format)) {
mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
mw.visitVarInsn(Opcodes.ILOAD, FIELD_VALUE);
mw.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "toString", "(I)Ljava/lang/String;", false);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "writeString", "(Ljava/lang/String;)V", false);
} else if (format != null) {
mw.visitLdcInsn(format);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "writeInt32", "(ILjava/lang/String;)V", false);
} else if (format != null) {
mw.visitFieldInsn(Opcodes.GETFIELD, mwc.classNameType, fieldWriter(i), DESC_FIELD_WRITER);
mw.visitFieldInsn(Opcodes.GETFIELD, TYPE_FIELD_WRITER, "decimalFormat", "Ljava/text/DecimalFormat;");
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "writeInt32", "(ILjava/text/DecimalFormat;)V", false);
} else {
mw.visitVarInsn(Opcodes.ALOAD, JSON_WRITER);
mw.visitVarInsn(Opcodes.ILOAD, FIELD_VALUE);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_WRITER, "writeInt32", "(I)V", false);
}

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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.annotation.JSONField;
import com.alibaba.fastjson2.writer.ObjectWriter;
import com.alibaba.fastjson2.writer.ObjectWriterCreator;
import org.junit.jupiter.api.Test;

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

public class Issue1599 {
@Test
public void test() {
Bean bean = new Bean();
bean.backgroundColor = 0xFEF0FE;
bean.borderColor = 0xEE00CC;

String str = JSON.toJSONString(bean);
assertEquals("{\"background-color\":\"#00FEF0FE\",\"border-color\":\"#00EE00CC\"}", str);
}

public class Bean {
@JSONField(name = "background-color", format = "#%08X")
public int backgroundColor;
@JSONField(name = "border-color", format = "#%08X")
public int borderColor;
}

@Test
public void test1() {
Bean bean = new Bean();
bean.backgroundColor = 0xFEF0FE;
bean.borderColor = 0xEE00CC;

ObjectWriter objectWriter = ObjectWriterCreator.INSTANCE.createObjectWriter(Bean.class);

JSONWriter jsonWriter = JSONWriter.of();
objectWriter.write(jsonWriter, bean);
String str = jsonWriter.toString();
assertEquals("{\"background-color\":\"#00FEF0FE\",\"border-color\":\"#00EE00CC\"}", str);
}
}

0 comments on commit 93eee1d

Please sign in to comment.