Skip to content

Commit

Permalink
fix support custom reader for enum, for issue #2688 and issue #2726 (#…
Browse files Browse the repository at this point in the history
…2727)

* fix support custom reader for enum, for issue #2688 and issue #2726
  • Loading branch information
yanxutao89 committed Jun 25, 2024
1 parent 5c2795e commit 54cfd78
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/com/alibaba/fastjson2/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1482,10 +1482,10 @@ public static <T> T cast(Object obj, Class<T> targetClass, ObjectReaderProvider
if (obj instanceof Integer) {
int intValue = (Integer) obj;
return (T) ((ObjectReaderImplEnum) objectReader).of(intValue);
} else {
JSONReader jsonReader = JSONReader.of(JSON.toJSONString(obj));
return (T) objectReader.readObject(jsonReader, null, null, 0);
}
} else {
JSONReader jsonReader = JSONReader.of(JSON.toJSONString(obj));
return (T) objectReader.readObject(jsonReader, targetClass, null, 0);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.alibaba.fastjson2.issues_2600;
package com.alibaba.fastjson2.issues_2700;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.alibaba.fastjson2.issues_2700;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.annotation.JSONType;
import com.alibaba.fastjson2.reader.ObjectReader;
import lombok.Getter;
import lombok.Setter;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Type;
import java.util.Objects;

import static com.alibaba.fastjson2.issues_2700.Issue2726.ElementType.DIV;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2726 {
@Test
public void test() {
String json = "[{\"elementType\":1}]";
JSONArray jsonArray = JSONArray.parseArray(json);
assertEquals(DIV, ((JSONObject) jsonArray.get(0)).to(Bean.class).getElementType());
assertEquals(DIV, jsonArray.toJavaList(Bean.class).get(0).getElementType());
}

@Getter
@Setter
static class Bean {
private ElementType elementType;
}

@JSONType(writeEnumAsJavaBean = true, deserializer = FastJsonEnumDeserializer.class)
@Getter
enum ElementType {
HTML("HTML"),
DIV("DIV");

final String name;

ElementType(String name) {
this.name = name;
}
}

@SuppressWarnings("rawtypes")
static class FastJsonEnumDeserializer
implements ObjectReader<Object> {
@Override
public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
if (jsonReader.nextIfNull()) {
return null;
}
Class fieldClass = (Class) fieldType;
Object value = jsonReader.readAny();
if (fieldClass.isEnum()) {
Enum[] enumConstants = (Enum[]) fieldClass.getEnumConstants();
if (value instanceof Integer) {
for (Enum enumConstant : enumConstants) {
if (Objects.equals(enumConstant.ordinal(), value)) {
return enumConstant;
}
}
} else {
for (Enum enumConstant : enumConstants) {
if (Objects.equals(enumConstant.name(), value)) {
return enumConstant;
}
}
}
}
return null;
}
}
}

0 comments on commit 54cfd78

Please sign in to comment.