Skip to content

Commit

Permalink
Fix#1254#1305 bug on JSON serialization and deserialization (#1306)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Feb 28, 2023
1 parent 027a09a commit d8b2415
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static Object serialize(Object bean, boolean addType) throws NullPointerE
}
return array;
} else if (bean instanceof Map) {
Map map = (Map) bean;
Map map = new LinkedHashMap<>((Map) bean);
Iterator itr = map.entrySet().iterator();
Map.Entry entry = null;
while (itr.hasNext()) {
Expand Down Expand Up @@ -132,7 +132,7 @@ public static Object serialize(Object bean, boolean addType) throws NullPointerE
}
}
if (addType) {
String typeName = beanClass.getCanonicalName();
String typeName = beanClass.getName();
if (!typeName.startsWith("java.")
&& !typeName.startsWith("javax.")
&& !typeName.startsWith("sun.")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
*
*
Expand All @@ -38,4 +41,24 @@ public void test() {
Assert.assertTrue(str.contains(JSON.CLASS_KEY));
}

@Test
public void testBeanWithMapSerialization() {
TestJsonBean bean = new TestJsonBean();
bean.setName("xxxx");
Map<String, Object> map = new HashMap<String, Object>();
map.put("key", new Object());
bean.setMap(map);
String jsonString = JSON.toJSONString(bean, true);
bean.getMap().values().forEach(value -> Assert.assertEquals(value.getClass(), Object.class));
}

@Test
public void testBeanWithInnerClassDeserialization() {
TestJsonBean bean = new TestJsonBean();
bean.setName("xxxx");
String jsonString = JSON.toJSONString(bean, true);
Assert.assertEquals(JSON.parseObject(jsonString, TestJsonBean.class).getInnerBean().getClass(),
TestJsonBean.InnerBean.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.rpc.common.json;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -48,6 +49,17 @@ public class TestJsonBean {
@JSONIgnore
private String ignoreString;

private Map<String, Object> map;

private final InnerBean innerBean;

public static class InnerBean {
}

public TestJsonBean() {
this.innerBean = new InnerBean();
}

public String getName() {
return name;
}
Expand Down Expand Up @@ -129,6 +141,18 @@ public void setStatus(Status status) {
this.status = status;
}

public Map<String, Object> getMap() {
return map;
}

public void setMap(Map<String, Object> map) {
this.map = map;
}

public InnerBean getInnerBean() {
return innerBean;
}

public enum Status {
START(0, "启动"),
STOP(1, "停止");
Expand Down

0 comments on commit d8b2415

Please # to comment.