This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2702 from Omega-Ariston/fix-issue1780
issue #1780 (支持org.json.JSONObject )的两种解决方案
- Loading branch information
Showing
5 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/com/alibaba/fastjson/serializer/JSONObjectCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/test/java/com/alibaba/json/bvt/issue_1700/Issue1780_JSONObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/com/alibaba/json/bvt/issue_1700/Issue1780_Module.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
}; | ||
} | ||
|
||
} | ||
} |