Skip to content
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

Closed
ioplmk opened this issue Aug 10, 2022 · 5 comments
Closed

序列化 深拷贝 #668

ioplmk opened this issue Aug 10, 2022 · 5 comments
Labels
enhancement New feature or request
Milestone

Comments

@ioplmk
Copy link

ioplmk commented Aug 10, 2022

可否增加一个 api ,使用 fastjson 序列化 进行深拷贝

@ioplmk ioplmk added the enhancement New feature or request label Aug 10, 2022
@wenshao wenshao added this to the 2.0.12 milestone Aug 17, 2022
@wenshao
Copy link
Member

wenshao commented Aug 17, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.12-SNAPSHOT/
已经支持,请帮忙用2.0.12-SNAPSHOT版本验证,2.0.12版本预计在8月21日前发布

新的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;
    }
}

@ioplmk
Copy link
Author

ioplmk commented Aug 18, 2022

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;
      }
    
  }

20220818092315
你好,我在测试的时候发现抛了异常,断点显示在 为 Set 集合赋值的时候出现了问题

wenshao added a commit that referenced this issue Aug 18, 2022
@wenshao
Copy link
Member

wenshao commented Aug 18, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.12-SNAPSHOT/
问题已修复,请更新到最新2.0.12-SNAPSHOT版本帮忙验证,2.0.12版本预计在8月21日前发布

@ioplmk
Copy link
Author

ioplmk commented Aug 19, 2022

目前没啥问题了

@wenshao
Copy link
Member

wenshao commented Aug 20, 2022

@wenshao wenshao closed this as completed Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants