-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
5b1e892
commit d458ff9
Showing
2 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
core/src/test/java/com/alibaba/fastjson2/issues_2700/Issue2726.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.alibaba.fastjson2.issues_2700; | ||
|
||
import com.alibaba.fastjson2.annotation.JSONType; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.alibaba.fastjson2.JSONArray; | ||
import com.alibaba.fastjson2.JSONObject; | ||
import com.alibaba.fastjson2.JSONReader; | ||
import com.alibaba.fastjson2.reader.ObjectReader; | ||
|
||
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; | ||
} | ||
} | ||
} | ||
|