Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2702 from Omega-Ariston/fix-issue1780
Browse files Browse the repository at this point in the history
issue #1780 (支持org.json.JSONObject )的两种解决方案
  • Loading branch information
wenshao authored Sep 4, 2019
2 parents bacef0f + 22abbf7 commit 90998d5
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,13 @@
<version>4.8.42</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/alibaba/fastjson/serializer/JSONObjectCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.fastjson.serializer;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;

public class JSONObjectCodec implements ObjectSerializer {
public final static JSONObjectCodec instance = new JSONObjectCodec();

@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
throws IOException {
SerializeWriter out = serializer.out;
MapSerializer mapSerializer = MapSerializer.instance;

try {
Field mapField = object.getClass().getDeclaredField("map");
if (Modifier.isPrivate(mapField.getModifiers())) {
mapField.setAccessible(true);
}

Object map = mapField.get(object);
mapSerializer.write(serializer, map, fieldName, fieldType, features);

} catch (Exception e) {
out.writeNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public class SerializeConfig {
private static boolean springfoxError = false;
private static boolean guavaError = false;
private static boolean jsonnullError = false;

private static boolean jsonobjectError = false;

private static boolean jodaError = false;

private boolean asm = !ASMUtils.IS_ANDROID;
Expand Down Expand Up @@ -695,6 +696,16 @@ private ObjectSerializer getObjectWriter(Class<?> clazz, boolean create) {
jsonnullError = true;
}
}

if (!jsonobjectError && className.equals("org.json.JSONObject")) {
try {
put(Class.forName("org.json.JSONObject"), writer = JSONObjectCodec.instance);
return writer;
} catch (ClassNotFoundException e) {
// skip
jsonobjectError = true;
}
}

if ((!jodaError) && className.startsWith("org.joda.")) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.alibaba.json.bvt.issue_1700;

import org.junit.Assert;
import com.alibaba.fastjson.JSON;
import junit.framework.TestCase;

public class Issue1780_JSONObject extends TestCase {

public void test_for_issue() {
org.json.JSONObject req = new org.json.JSONObject();
req.put("id", 1111);
req.put("name", "name11");
Assert.assertEquals("{\"name\":\"name11\",\"id\":1111}", JSON.toJSONString(req));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.alibaba.json.bvt.issue_1700;

import java.io.IOException;
import java.lang.reflect.Type;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.spi.Module;

import junit.framework.TestCase;

public class Issue1780_Module extends TestCase {

public void test_for_issue() {
org.json.JSONObject req = new org.json.JSONObject();

SerializeConfig config = new SerializeConfig();
config.register(new myModule());
req.put("id", 1111);
req.put("name", "name11");
Assert.assertEquals("{\"name\":\"name11\",\"id\":1111}", JSON.toJSONString(req, config));
}

public class myModule implements Module {

@SuppressWarnings("rawtypes")
@Override
public ObjectDeserializer createDeserializer(ParserConfig config, Class type) {
return null;
}

@SuppressWarnings("rawtypes")
@Override
public ObjectSerializer createSerializer(SerializeConfig config, Class type) {
return new ObjectSerializer() {

@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
int features) throws IOException {
System.out.println("-------------myModule.createSerializer-------------------");
org.json.JSONObject req = (org.json.JSONObject) object;
serializer.out.write(req.toString());
}
};
}

}
}

0 comments on commit 90998d5

Please sign in to comment.