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

JSON.toJSONString() 序列化导致原Map的value转换为LinkedHashMap #1254

Closed
bzl132 opened this issue Oct 18, 2022 · 4 comments
Closed
Assignees
Labels
bug Something isn't working good first issue Good for newcomers later This will be worked on in later version

Comments

@bzl132
Copy link

bzl132 commented Oct 18, 2022

Describe the bug

JSON.toJSONString() 序列化导致原Map的value转换为LinkedHashMap。

Expected behavior

JSON.toJSONString() 序列化后不影响原实体

Actual behavior

JSON序列化自定义对象时,调用BeanSerializer.serialize()将对象转换成map
image
如果,自定义对象中属性是Map时,会将Value替换成递归调用的结果,
image
这时如果Value是对象,会转换成LinkedHashMap,返回后会替换原始实体
image

Steps to reproduce

运行Demo的main方法

Minimal yet complete reproducer code (or GitHub URL to code)

import com.alipay.sofa.rpc.common.json.JSON;
import java.util.HashMap;
import java.util.Map;

public class TestDemo {

public static void main(String[] args) {
    BeanEntity bean = new TestDemo.BeanEntity();
    Map<String, ? super ValueEntity> map = new HashMap<>();
    map.put("1", new ValueEntity());
    bean.map = map;

    String jsonString = JSON.toJSONString(bean);

    bean.map.values().forEach(value -> {
        System.out.println(value.getClass().getSimpleName());
    });
}

static class BeanEntity {
    Map<String, ? super ValueEntity> map;
}

static class ValueEntity {}

}

Environment

  • SOFARPC version:5.7.3
  • JVM version (e.g. java -version): 11.0.15
  • OS version (e.g. uname -a): Windows10
  • Maven version:3.6.1
  • IDE version:idea 2020.2.4
@EvenLjj
Copy link
Collaborator

EvenLjj commented Oct 29, 2022

image
@bzl132 Thank you for feedback. It's really a bug, there should be a new map. Are you interested to fix it?

@stale
Copy link

stale bot commented Dec 28, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Dec 28, 2022
@OrezzerO OrezzerO added bug Something isn't working good first issue Good for newcomers and removed wontfix This will not be worked on labels Dec 30, 2022
@EvenLjj EvenLjj added the later This will be worked on in later version label Jan 3, 2023
@gofow
Copy link
Contributor

gofow commented Feb 11, 2023

Hi there, I'd like to take a crack at fixing it. Would you mind assigning the issue to me? My thought is to create a fresh Map within the serialize() function when the "bean" is a Map instance, in order to prevent any unpredictable changes due to references. @OrezzerO @EvenLjj

@EvenLjj
Copy link
Collaborator

EvenLjj commented Feb 17, 2023

@gofow You are right, looking forward to your participation.

gofow pushed a commit to gofow/sofa-rpc that referenced this issue Feb 19, 2023
gofow pushed a commit to gofow/sofa-rpc that referenced this issue Feb 19, 2023
EvenLjj pushed a commit that referenced this issue Feb 28, 2023
* fix bug #1254 for JSON serialization and deserialization

* fix bug #1254 for JSON serialization and deserialization

* modify JSONTest

* add two cases to JSONTest

* pass JSONTest

* remove the modification of addType

* format code

* modify test method name

---------

Co-authored-by: HISSs <taiguhe@foxmail.com>
Co-authored-by: liujianjun.ljj <liujianjun.ljj@antgroup.com>
@OrezzerO OrezzerO closed this as completed Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers later This will be worked on in later version
Projects
None yet
Development

No branches or pull requests

4 participants