Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -2568,9 +2567,7 @@ public <T> FieldReader createFieldReaderMethod(
}
}

boolean list = fieldClass == List.class
|| fieldClass == ArrayList.class
|| fieldClass == LinkedList.class
boolean list = List.class.isAssignableFrom(fieldClass)
|| "cn.hutool.json.JSONArray".equals(fieldClass.getName());
if (list) {
if (fieldTypeResolved instanceof ParameterizedType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alibaba.fastjson2.issues_3600.issue3601;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.modules.ObjectReaderModule;
import com.alibaba.fastjson2.reader.ObjectReader;
import com.alibaba.fastjson2.reader.ObjectReaderImplList;
import lombok.var;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class FastJson2Reader {
public static class JsonReaderModule
implements ObjectReaderModule {
@Override
public ObjectReader getObjectReader(Type type) {
if (type instanceof ParameterizedType) {
var ptype = (ParameterizedType) type;
if (ptype.getRawType() == MyArrayList.class) {
return ObjectReaderImplList.of(type, null, 0);
}
}
return ObjectReaderModule.super.getObjectReader(type);
}
}

public static void init() {
JSON.register(new JsonReaderModule());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.alibaba.fastjson2.issues_3600.issue3601;

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

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class Issue3601 {
@Test
public void test() {
FastJson2Reader.init();

var game = new Game();
game.players.addAll(Arrays.asList(new Player(1, 100), new Player(2, 200)));

var gameJson = JSON.toJSONString(game);
var deserializedGame1 = JSON.parseObject(gameJson, Game.class);
assertTrue(deserializedGame1.players.get(0).getClass() == Player.class);
}

public static class Player {
public int level;
public int exp;
public Player() {
}
public Player(int level, int exp) {
this.level = level;
this.exp = exp;
}
}

public static class Game {
private MyArrayList<Player> players = new MyArrayList<>();
public MyArrayList<Player> getPlayers() {
return players;
}
public void setPlayers(MyArrayList<Player> players) {
this.players = players;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package com.alibaba.fastjson2.issues_3600.issue3601;

import lombok.var;

import java.util.*;

public class MyArrayList<T>
implements List<T> {
private transient ArrayList<T> impl = new ArrayList<>();

public MyArrayList() {
}

public MyArrayList(Collection<T> coll) {
this.addAll(coll);
}

public MyArrayList(T... coll) {
for (T v : coll) {
this.add(v);
}
}

@Override
public int size() {
return impl.size();
}

@Override
public boolean isEmpty() {
return impl.isEmpty();
}

@Override
public boolean contains(Object o) {
return impl.contains(o);
}

@Override
public Iterator<T> iterator() {
return impl.iterator();
}

@Override
public Object[] toArray() {
return impl.toArray();
}

@Override
public <V> V[] toArray(V[] a) {
return impl.toArray(a);
}

@Override
public boolean add(T v) {
boolean r = impl.add(v);
return r;
}

@Override
public boolean remove(Object o) {
int index = impl.indexOf(o);
var r = index >= 0;
return r;
}

@Override
public boolean containsAll(Collection<?> c) {
return impl.containsAll(c);
}

@Override
public boolean addAll(Collection<? extends T> c) {
impl.ensureCapacity(impl.size() + c.size());
for (var v : c) {
this.add(v);
}
return true;
}

@Override
public boolean addAll(int index, Collection<? extends T> c) {
impl.ensureCapacity(impl.size() + c.size());
for (var v : c) {
this.add(index++, v);
}
return true;
}

@Override
public boolean removeAll(Collection<?> c) {
for (var v : c) {
this.remove(v);
}
return true;
}

@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException("retainAll");
}

@Override
public void clear() {
if (isEmpty()) {
return;
}
impl.clear();
}

@Override
public T get(int index) {
return impl.get(index);
}

@Override
public T set(int index, T element) {
var r = impl.set(index, element);
return r;
}

@Override
public void add(int index, T element) {
impl.add(index, element);
}

@Override
public T remove(int index) {
T prev = impl.remove(index);
return prev;
}

@Override
public int indexOf(Object o) {
return impl.indexOf(o);
}

@Override
public int lastIndexOf(Object o) {
return impl.lastIndexOf(o);
}

@Override
public ListIterator<T> listIterator() {
return impl.listIterator();
}

@Override
public ListIterator<T> listIterator(int index) {
return impl.listIterator(index);
}

@Override
public List<T> subList(int fromIndex, int toIndex) {
return Collections.unmodifiableList(impl.subList(fromIndex, toIndex));
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MyArrayList<?> dbList = (MyArrayList<?>) o;
return impl.equals(dbList.impl);
}

@Override
public int hashCode() {
return Objects.hash(impl);
}

@Override
public String toString() {
return "MyArrayList{" +
"impl=" + impl +
'}';
}
}