Skip to content

Commit

Permalink
fix bug sofastack#1254 for JSON serialization and deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
HISSs committed Feb 18, 2023
1 parent af35a32 commit 7fcc3d9
Show file tree
Hide file tree
Showing 2 changed files with 42 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 @@ -131,8 +131,11 @@ public static Object serialize(Object bean, boolean addType) throws NullPointerE
+ "." + field.getName() + " error! ", e);
}
}
if (map.size() == 0) {
addType = true;
}
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
@@ -0,0 +1,37 @@
package com.alipay.sofa.rpc.test;

import com.alipay.sofa.rpc.common.json.JSON;

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

/**
* @author github.com/gofow
* @date 2023/02/18 16:55
**/
public class JSONTest {

public static void main(String[] args) {
BeanEntity bean = new JSONTest.BeanEntity();

Map<String, ? super ValueEntity> map = new HashMap<>();
map.put("1", new ValueEntity());
bean.map = map;

String jsonString = JSON.toJSONString(bean);
System.out.println(jsonString);

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

BeanEntity ans = JSON.parseObject(jsonString, BeanEntity.class);
System.out.println(ans.map.get("1").getClass().getSimpleName());
}

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

static class ValueEntity {}
}

0 comments on commit 7fcc3d9

Please sign in to comment.