Skip to content

Commit

Permalink
fix serialize List<String> error, for issue #1826
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Sep 4, 2023
1 parent 15c4962 commit dfb3ff7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ public void writeString(List<String> list) {
String str = list.get(i);
if (str == null) {
writeNull();
continue;
}
int coder = STRING_CODER.applyAsInt(str);
if (coder != LATIN) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.alibaba.fastjson2.issues_1800;

import com.alibaba.fastjson2.JSONB;
import org.junit.jupiter.api.Test;

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

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

public class Issue1826 {
@Test
public void test() {
Bean bean = new Bean();
bean.values = new ArrayList<>();
bean.values.add("a");
bean.values.add(null);
bean.values.add("b");

byte[] bytes = JSONB.toBytes(bean);
Bean bean1 = JSONB.parseObject(bytes, Bean.class);
assertEquals(bean.values, bean1.values);
}

public static class Bean {
public List<String> values;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.fastjson2.fastcode;

import org.junit.jupiter.api.Test;
import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;

public class BigDecimalTest {
@Test
public void test() throws Throwable {
Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafeField.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafeField.get(null);
Method objectFieldOffset = Unsafe.class.getMethod("objectFieldOffset", Field.class);
long stringCacheOffset = (Long) objectFieldOffset.invoke(
unsafe,
BigDecimal.class.getDeclaredField("stringCache")
);

BigDecimal d = BigDecimal.valueOf(12345, 2);
unsafe.putObject(d, stringCacheOffset, null);

d.toString();
System.out.println(unsafe.getObject(d, stringCacheOffset));
unsafe.putObject(d, stringCacheOffset, null);
System.out.println(unsafe.getObject(d, stringCacheOffset));
}
}

0 comments on commit dfb3ff7

Please sign in to comment.