Skip to content

Commit

Permalink
fix 1.x compatible api JSON#toJSON, for issue #2447
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 16, 2024
1 parent 70e14f7 commit cf69590
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.alibaba.fastjson2.issues_2400;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.Date;

public class Issue2447 {
@Test
public void test() {
Bean bean = new Bean();
bean.setNum(10L);
bean.setTime(new Date());
bean.setDecimal(new BigDecimal("0.02"));
JSONObject jsonObject = (JSONObject) JSON.toJSON(bean);
assert Long.class == jsonObject.get("num").getClass();
assert Date.class == jsonObject.get("time").getClass();
assert BigDecimal.class == jsonObject.get("decimal").getClass();
}

static class Bean {
private Long num;
private Date time;
private BigDecimal decimal;

public Long getNum() {
return num;
}

public void setNum(Long num) {
this.num = num;
}

public Date getTime() {
return time;
}

public void setTime(Date time) {
this.time = time;
}

public BigDecimal getDecimal() {
return decimal;
}

public void setDecimal(BigDecimal decimal) {
this.decimal = decimal;
}
}
}
20 changes: 15 additions & 5 deletions fastjson1-compatible/src/main/java/com/alibaba/fastjson/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -2135,12 +2135,22 @@ public static Object toJSON(Object javaObject) {
return javaObject;
}

String str = JSON.toJSONString(javaObject);
Object object = JSON.parse(str);
if (object instanceof List) {
return new JSONArray((List) object);
Object json;
try {
json = com.alibaba.fastjson2.JSON.toJSON(javaObject);
} catch (com.alibaba.fastjson2.JSONException e) {
throw new JSONException(e.getMessage(), e);
}
return object;

if (json instanceof com.alibaba.fastjson2.JSONObject) {
return new JSONObject((com.alibaba.fastjson2.JSONObject) json);
}

if (json instanceof com.alibaba.fastjson2.JSONArray) {
return new JSONArray((com.alibaba.fastjson2.JSONArray) json);
}

return json;
}

public static Object toJSON(Object javaObject, SerializeConfig config) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.alibaba.fastjson.v2issues;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.Date;

public class Issue2447 {
@Test
public void test() {
Bean bean = new Bean();
bean.setNum(10L);
bean.setTime(new Date());
bean.setDecimal(new BigDecimal("0.02"));
JSONObject jsonObject = (JSONObject) JSON.toJSON(bean);
assert Long.class == jsonObject.get("num").getClass();
assert Date.class == jsonObject.get("time").getClass();
assert BigDecimal.class == jsonObject.get("decimal").getClass();
}

static class Bean {
private Long num;
private Date time;
private BigDecimal decimal;

public Long getNum() {
return num;
}

public void setNum(Long num) {
this.num = num;
}

public Date getTime() {
return time;
}

public void setTime(Date time) {
this.time = time;
}

public BigDecimal getDecimal() {
return decimal;
}

public void setDecimal(BigDecimal decimal) {
this.decimal = decimal;
}
}
}

0 comments on commit cf69590

Please sign in to comment.