Skip to content

Commit

Permalink
improved fastjson 1.x compatible, for issues alibaba#1291
Browse files Browse the repository at this point in the history
  • Loading branch information
hcxxiaomo committed Mar 27, 2023
1 parent 0bd035c commit 5584205
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName
list.add(
jsonReader.readString());
}
} else if (ch == '"' || ch == '\'') {
} else if (ch == '"' || ch == '\'' || ch == '{') {
String str = jsonReader.readString();
list.add(str);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.alibaba.fastjson2.issues_1000;

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

import java.util.Date;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class Issue1291 {
@Test
public void test() {
String str = "{\"obj\":{\"unitCode\":\"755H\"}}";
Result<List> result = JSON.parseObject(str, new TypeReference<Result<List>>() {});
assertEquals(1, result.obj.size());
JSONObject object = (JSONObject) result.obj.get(0);
assertNotNull(object);
assertEquals("755H", object.get("unitCode"));
}

@Test
public void test_1() {
String info = "{\"obj\":{\"unitCode\":\"755H\"}}";
Result<List<String>> result = JSONObject.parseObject(info, new TypeReference<Result<List<String>>>() {});
assertEquals(1, result.obj.size());
String string = result.obj.get(0);
assertEquals("{\"unitCode\":\"755H\"}", string);
}

@Test
public void test_2() {
String info = "{\"obj\":{\"unitCode\":\"755H\"}}";
Result<List<Info>> result = JSONObject.parseObject(info, new TypeReference<Result<List<Info>>>() {});
assertEquals(1, result.obj.size());
Info object = result.obj.get(0);
assertNotNull(object);
assertEquals("755H", object.unitCode);
}

public static class Result<T> {
public Date date;
public String errorCode;
public String errorMessage;
public T obj;
public boolean success;
}

public static class Info{
public String unitCode;
}

}

0 comments on commit 5584205

Please sign in to comment.