JSONPath.extract support NonZeroNumberCastToBooleanAsTrue

This commit is contained in:
shaojin.wensj 2023-02-14 21:02:44 +08:00
parent 609166e2f3
commit 2ed4551633
4 changed files with 50 additions and 3 deletions

View File

@ -501,6 +501,11 @@ public final class JSONFactory {
return new JSONReader.Context(provider);
}
public static JSONReader.Context createReadContext(long features) {
ObjectReaderProvider provider = JSONFactory.getDefaultObjectReaderProvider();
return new JSONReader.Context(provider, features);
}
public static JSONReader.Context createReadContext(JSONReader.Feature... features) {
JSONReader.Context context = new JSONReader.Context(
JSONFactory.getDefaultObjectReaderProvider()

View File

@ -78,7 +78,7 @@ class JSONPathTypedMulti
}
protected JSONReader.Context createContext() {
JSONReader.Context context = JSONFactory.createReadContext();
JSONReader.Context context = JSONFactory.createReadContext(features);
if (zoneId != null && zoneId != DEFAULT_ZONE_ID) {
context.zoneId = zoneId;
}

View File

@ -3335,6 +3335,14 @@ public abstract class JSONReader
this.symbolTable = null;
}
public Context(ObjectReaderProvider provider, long features) {
this.features = features;
this.provider = provider;
this.objectSupplier = JSONFactory.defaultObjectSupplier;
this.arraySupplier = JSONFactory.defaultArraySupplier;
this.symbolTable = null;
}
public Context(Feature... features) {
this.features = defaultReaderFeatures;
this.provider = JSONFactory.getDefaultObjectReaderProvider();

View File

@ -1,11 +1,14 @@
package com.alibaba.fastjson2.read;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONPath;
import com.alibaba.fastjson2.util.DateUtils;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Type;
import static com.alibaba.fastjson2.JSONReader.Feature.NonZeroNumberCastToBooleanAsTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
public class BooleanTest {
@Test
@ -26,6 +29,37 @@ public class BooleanTest {
assertTrue(JSON.parseObject("{\"value\":true}").toJavaObject(Bean.class, NonZeroNumberCastToBooleanAsTrue).value);
}
@Test
public void testPath() {
JSONPath path = JSONPath.of(
new String[]{"$.id", "$.value"},
new Type[]{int.class, boolean.class},
new String[2],
new long[]{0, 0},
DateUtils.SHANGHAI_ZONE_ID,
NonZeroNumberCastToBooleanAsTrue
);
Object[] values = (Object[]) path.extract("{\"id\":123,\"value\":3}");
assertEquals(Boolean.TRUE, values[1]);
}
@Test
public void testPath1() {
JSONPath path = JSONPath.of(
new String[]{"$.id", "$.value"},
new Type[]{int.class, boolean.class},
new String[2],
new long[]{0, 0},
DateUtils.SHANGHAI_ZONE_ID
);
assertEquals(Boolean.TRUE, ((Object[]) path.extract("{\"id\":123,\"value\":\"true\"}"))[1]);
assertEquals(Boolean.TRUE, ((Object[]) path.extract("{\"id\":123,\"value\":\"TRUE\"}"))[1]);
assertEquals(Boolean.TRUE, ((Object[]) path.extract("{\"id\":123,\"value\":\"1\"}"))[1]);
assertEquals(Boolean.FALSE, ((Object[]) path.extract("{\"id\":123,\"value\":\"false\"}"))[1]);
assertEquals(Boolean.FALSE, ((Object[]) path.extract("{\"id\":123,\"value\":\"FALSE\"}"))[1]);
assertEquals(Boolean.FALSE, ((Object[]) path.extract("{\"id\":123,\"value\":\"0\"}"))[1]);
}
public static class Bean {
public boolean value;
}