-
Notifications
You must be signed in to change notification settings - Fork 495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
序列化 深拷贝 #668
Labels
enhancement
New feature or request
Milestone
Comments
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.12-SNAPSHOT/ 新的JSON#copy方法使用方法如下: import com.alibaba.fastjson2_vo.Int2;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CopyTest {
@Test
public void test() {
Object[] values = new Object[]{JSONObject.of(), JSONArray.of(1, 2, 3), 1, 1.0, BigDecimal.ONE, null, 101L, "abc", Collections.emptyList()};
for (Object value : values) {
Object copy = JSON.copy(value);
assertEquals(value, copy);
}
}
@Test
public void test0() {
Int2 value = new Int2();
value.setV0000(1001);
value.setV0001(1002);
Int2 copy = JSON.copy(value);
assertEquals(value.getV0000(), copy.getV0000());
assertEquals(value.getV0001(), copy.getV0001());
}
@Test
public void test1() {
Bean1 bean = new Bean1();
bean.v0 = 1001;
bean.v1 = 1002;
Bean1 copy = JSON.copy(bean, JSONWriter.Feature.FieldBased);
assertEquals(bean.v0, copy.v0);
assertEquals(bean.v1, copy.v1);
Bean1 copy2 = JSON.copy(bean, JSONWriter.Feature.FieldBased, JSONWriter.Feature.BeanToArray);
assertEquals(bean.v0, copy2.v0);
assertEquals(bean.v1, copy2.v1);
}
@Test
public void test2() {
Bean1 bean = new Bean1();
bean.v0 = 1001;
bean.v1 = 1002;
JSONArray array = JSONArray.of(bean);
JSONArray copy = JSON.copy(array, JSONWriter.Feature.FieldBased);
Bean1 bean1 = (Bean1) copy.get(0);
assertEquals(bean.v0, bean1.v0);
assertEquals(bean.v1, bean1.v1);
JSONArray copy2 = JSON.copy(array, JSONWriter.Feature.FieldBased, JSONWriter.Feature.BeanToArray);
Bean1 bean2 = (Bean1) copy2.get(0);
assertEquals(bean.v0, bean2.v0);
assertEquals(bean.v1, bean2.v1);
}
public static class Bean1 {
private int v0;
private int v1;
}
} |
import com.alibaba.fastjson2.*;
import java.io.*;
import java.math.*;
import java.util.*;
public class SimpleTest
{
public static void main( String[] args )
{
TestBean testBean = new TestBean();
testBean.settCode( "zsdqjdbdd" );
testBean.setNum( 1 );
Set<String> set = new HashSet<>();
set.add( "111" );
set.add( "222" );
set.add( "333" );
testBean.setStringSet( set );
testBean.setBigDecimal( new BigDecimal( 1 ) );
TestBean copy = JSON.copy( testBean );
boolean equal = testBean == copy;
System.err.println( equal );
}
}
class TestBean implements Serializable
{
private static final long serialVersionUID = - 3928832861296252415L;
private String tCode;
private int num;
private Set<String> stringSet;
private BigDecimal bigDecimal;
public String gettCode()
{
return tCode;
}
public void settCode( String tCode )
{
this.tCode = tCode;
}
public int getNum()
{
return num;
}
public void setNum( int num )
{
this.num = num;
}
public Set<String> getStringSet()
{
return stringSet;
}
public void setStringSet( Set<String> stringSet )
{
this.stringSet = stringSet;
}
public BigDecimal getBigDecimal()
{
return bigDecimal;
}
public void setBigDecimal( BigDecimal bigDecimal )
{
this.bigDecimal = bigDecimal;
}
} |
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.12-SNAPSHOT/ |
目前没啥问题了 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
可否增加一个 api ,使用 fastjson 序列化 进行深拷贝
The text was updated successfully, but these errors were encountered: