Skip to content

Commit

Permalink
JSONObject#to support Void.class, for issue #2879
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 1, 2025
1 parent 838fd03 commit 4d492bc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,10 @@ public <T> T to(Class<T> clazz, JSONReader.Feature... features) {
return (T) this;
}

if (clazz == Void.class || clazz == void.class) {
return null;
}

ObjectReaderProvider provider = JSONFactory.getDefaultObjectReaderProvider();
ObjectReader<T> objectReader = provider.getObjectReader(clazz, fieldBased);
return objectReader.createInstance(this, featuresValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.alibaba.fastjson2.issues_2800;

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

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

public class Issue2879 {
@Test
public void test() {
JSONObject object = JSON.parseObject("{}");
assertNull(object.to(Void.class));
assertNull(object.to(void.class));
}
}

0 comments on commit 4d492bc

Please sign in to comment.