/src/test/java/com/alibaba/json/bvt/issue_3600/Issue3693.java

https://github.com/alibaba/fastjson · Java · 109 lines · 84 code · 25 blank · 0 comment · 0 complexity · c769d5df704802749a62b53376bc3156 MD5 · raw file

  1. package com.alibaba.json.bvt.issue_3600;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.TypeReference;
  4. import com.alibaba.fastjson.annotation.JSONField;
  5. import com.alibaba.fastjson.parser.DefaultJSONParser;
  6. import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
  7. import com.alibaba.fastjson.serializer.JSONSerializer;
  8. import com.alibaba.fastjson.serializer.ObjectSerializer;
  9. import junit.framework.TestCase;
  10. import java.lang.reflect.Type;
  11. public class Issue3693 extends TestCase {
  12. public void test_for_issue() throws Exception {
  13. Model<ModelProperties> model = new Model<ModelProperties>("hello 世界", new ModelProperties("红色", 66));
  14. String json = JSON.toJSONString(model);
  15. assertEquals("{\"name\":\"hello 世界\",\"properties\":\"{\\\"color\\\":\\\"红色\\\",\\\"size\\\":66}\"}", json);
  16. Model<ModelProperties> deserializedModel = JSON.parseObject(json, new TypeReference<Model<ModelProperties>>() {
  17. });
  18. assertNotNull(deserializedModel);
  19. assertEquals("hello 世界", deserializedModel.getName());
  20. assertNotNull(deserializedModel.getProperties());
  21. assertEquals("红色", deserializedModel.getProperties().getColor());
  22. assertEquals(66, deserializedModel.getProperties().getSize());
  23. }
  24. static class Model<T> {
  25. private String name;
  26. @JSONField(serializeUsing = MyCodec.class, deserializeUsing = MyCodec.class)
  27. private T properties;
  28. Model() {
  29. }
  30. Model(String name, T properties) {
  31. this.name = name;
  32. this.properties = properties;
  33. }
  34. public String getName() {
  35. return this.name;
  36. }
  37. public void setName(String name) {
  38. this.name = name;
  39. }
  40. public T getProperties() {
  41. return this.properties;
  42. }
  43. public void setProperties(T properties) {
  44. this.properties = properties;
  45. }
  46. }
  47. static class ModelProperties {
  48. private String color;
  49. private int size;
  50. ModelProperties() {
  51. }
  52. ModelProperties(String color, int size) {
  53. this.color = color;
  54. this.size = size;
  55. }
  56. public String getColor() {
  57. return this.color;
  58. }
  59. public void setColor(String color) {
  60. this.color = color;
  61. }
  62. public int getSize() {
  63. return this.size;
  64. }
  65. public void setSize(int size) {
  66. this.size = size;
  67. }
  68. }
  69. public static class MyCodec implements ObjectSerializer, ObjectDeserializer {
  70. @Override
  71. public int getFastMatchToken() {
  72. return 0;
  73. }
  74. @Override
  75. public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) {
  76. serializer.write(JSON.toJSONString(object));
  77. }
  78. @Override
  79. public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
  80. String json = parser.parseObject(String.class);
  81. return JSON.parseObject(json, type);
  82. }
  83. }
  84. }