Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix support custom reader for enum, for issue #2688 and issue #2726 #2727

Merged
merged 3 commits into from
Jun 25, 2024
Merged
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
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;
}
}
}
Loading