Skip to content

Commit

Permalink
fix support FieldBased feature on field for issue #2350
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 committed Mar 21, 2024
1 parent 021facc commit 652cb53
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ public static void getFieldInfo(FieldInfo fieldInfo, JSONField jsonField, boolea

for (JSONReader.Feature feature : jsonField.deserializeFeatures()) {
fieldInfo.features |= feature.mask;
if (fieldInfo.ignore && feature == JSONReader.Feature.FieldBased) {
fieldInfo.ignore = false;
}
}

int ordinal = jsonField.ordinal();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,9 @@ private void getFieldInfo(FieldInfo fieldInfo, JSONField jsonField) {

for (JSONReader.Feature feature : jsonField.deserializeFeatures()) {
fieldInfo.features |= feature.mask;
if (fieldInfo.ignore && feature == JSONReader.Feature.FieldBased) {
fieldInfo.ignore = false;
}
}

int ordinal = jsonField.ordinal();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.alibaba.fastjson2.issues_2300;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

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

public class Issue2350 {

@Test
public void test() {
String json = "{\n" +
"\t\"testField\": \"My Test\",\n" +
"\t\"testField2\": \"My Test2\"\n" +
"}";
TestClass testClass = JSON.parseObject(json, TestClass.class);
assertEquals("My Test", testClass.getTestField());
assertNull(testClass.getTestField2());
}

public static class TestClass {
@JSONField(deserializeFeatures = { JSONReader.Feature.FieldBased })
private String testField;
private String testField2;

public String getTestField() {
return testField;
}
public String getTestField2() {
return testField2;
}
}
}

0 comments on commit 652cb53

Please sign in to comment.