Java 类javax.json.bind.config.PropertyVisibilityStrategy 实例源码

项目:yasson    文件:JsonbPropertyVisibilityStrategyTest.java   
/**
 * Tests applying for both public and nonpublic fields.
 */
@Test
public void testFieldVisibilityStrategy() {
    JsonbConfig customizedConfig = new JsonbConfig();
    customizedConfig.setProperty(JsonbConfig.PROPERTY_VISIBILITY_STRATEGY, new PropertyVisibilityStrategy() {
        @Override
        public boolean isVisible(Field field) {
            final String fieldName = field.getName();
            return fieldName.equals("afield") || fieldName.equals("dfield");
        }

        @Override
        public boolean isVisible(Method method) {
            throw new IllegalStateException("Not supported");
        }
    });

    FieldPojo fieldPojo = new FieldPojo("avalue", "bvalue", "cvalue", "dvalue");

    Jsonb jsonb = JsonbBuilder.create(customizedConfig);
    assertEquals("{\"afield\":\"avalue\",\"dfield\":\"dvalue\"}", jsonb.toJson(fieldPojo));
}
项目:yasson    文件:JsonbPropertyVisibilityStrategyTest.java   
/**
 * Tests applying for both public and nonpublic getters.
 */
@Test
public void testMethodVisibilityStrategy() {
    JsonbConfig customizedConfig = new JsonbConfig();
    customizedConfig.setProperty(JsonbConfig.PROPERTY_VISIBILITY_STRATEGY, new PropertyVisibilityStrategy() {
        @Override
        public boolean isVisible(Field field) {
            throw new IllegalStateException("Not supported");
        }

        @Override
        public boolean isVisible(Method method) {
            final String methodName = method.getName();
            return methodName.equals("getAgetter") || methodName.equals("getDgetter");
        }
    });

    GetterPojo getterPojo = new GetterPojo();

    Jsonb jsonb = JsonbBuilder.create(customizedConfig);
    assertEquals("{\"agetter\":\"avalue\",\"dgetter\":\"dvalue\"}", jsonb.toJson(getterPojo));
}
项目:yasson    文件:SecurityManagerTest.java   
@Test
public void testWithSecurityManager() {
    Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyVisibilityStrategy(new PropertyVisibilityStrategy() {
        @Override
        public boolean isVisible(Field field) {
            return Modifier.isPublic(field.getModifiers()) || field.getName().equals("privateProperty");
        }

        @Override
        public boolean isVisible(Method method) {
            return Modifier.isPublic(method.getModifiers());
        }
    }));

    Pojo pojo = new Pojo();
    pojo.setStrProperty("string propery");
    Crate crate = new Crate();
    crate.crateBigDec = BigDecimal.TEN;
    crate.crateStr = "crate string";
    pojo.setCrate(crate);

    String result = jsonb.toJson(pojo);
}
项目:yasson    文件:CdiInjectionTest.java   
@Test
public void testNonCdiEnvironment() {
    JsonbConfig config = new JsonbConfig();
    //allow only field with components that doesn't has cdi dependencies.
    config.withPropertyVisibilityStrategy(new PropertyVisibilityStrategy() {
        @Override
        public boolean isVisible(Field field) {
            return "adaptedValue3".equals(field.getName());
        }

        @Override
        public boolean isVisible(Method method) {
            return false;
        }
    });
    Jsonb jsonb = JsonbBuilder.create(config);
    final String result = jsonb.toJson(new AdaptedPojo());
    assertEquals("{\"adaptedValue3\":1010}", result);
}
项目:JSON-framework-comparison    文件:RuntimeSampler.java   
public static JsonbConfig jsonbConfig() {
    return new JsonbConfig()

            // Property visibility
            .withNullValues(true)
            .withPropertyVisibilityStrategy(new PropertyVisibilityStrategy() {
                @Override
                public boolean isVisible(Field field) {
                    return false;
                }

                @Override
                public boolean isVisible(Method method) {
                    return false;
                }
            })

            // Property naming and order
            .withPropertyNamingStrategy(PropertyNamingStrategy.CASE_INSENSITIVE)
            .withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE)

            // Customised de/serializers
            .withAdapters(new ClassAdapter())
            .withDeserializers(new CustomDeserializer())
            .withSerializers(new CustomSerializer())

            // Formats, locals, encoding, binary data
            .withBinaryDataStrategy(BinaryDataStrategy.BASE_64_URL)
            .withDateFormat("MM/dd/yyyy", Locale.ENGLISH)
            .withLocale(Locale.CANADA)
            .withEncoding("UTF-8")
            .withStrictIJSON(true)
            .withFormatting(true);
}
项目:Java-EE-8-Sampler    文件:JsonBindingExample.java   
public String allCustomizedMapping() {

        PropertyVisibilityStrategy vis = new PropertyVisibilityStrategy() {
            @Override
            public boolean isVisible(Field field) {
                return false;
            }

            @Override
            public boolean isVisible(Method method) {
                return false;
            }
        };

        JsonbConfig jsonbConfig = new JsonbConfig()
                .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES)
                .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL)
                .withPropertyVisibilityStrategy(vis)
                .withStrictIJSON(true)
                .withFormatting(true)
                .withNullValues(true)
                .withBinaryDataStrategy(BinaryDataStrategy.BASE_64_URL)
                .withDateFormat("MM/dd/yyyy", Locale.ENGLISH);

        Jsonb jsonb = JsonbBuilder.create(jsonbConfig);

        return jsonb.toJson(book1);
    }
项目:yasson    文件:PropertyValuePropagation.java   
/**
 * Look up class and package level @JsonbVisibility, or global config PropertyVisibilityStrategy.
 * If any is found it is used for resolving visibility by calling provided visibilityCheckFunction.
 *
 * @param visibilityCheckFunction function declaring visibility check
 * @param declaringClass class to lookup annotation onto
 * @param ctx jsonb context
 * @return Optional with result of visibility check, or empty optional if no strategy is found
 */
private Boolean isVisible(Function<PropertyVisibilityStrategy, Boolean> visibilityCheckFunction, Class<?> declaringClass, Field field, Method method, JsonbContext ctx) {
    final Optional<PropertyVisibilityStrategy> classLevelStrategy =
            ctx.getAnnotationIntrospector().getPropertyVisibilityStrategy(declaringClass);
    Optional<PropertyVisibilityStrategy> strategy =
            Optional.ofNullable(classLevelStrategy.orElseGet(()->ctx.getConfigProperties().getPropertyVisibilityStrategy()));

    PropertyVisibilityStrategy str = strategy.orElse(new DefaultVisibilityStrategy(field, method));
    return visibilityCheckFunction.apply(str);
}
项目:yasson    文件:AnnotationIntrospector.java   
/**
 * Get a @JsonbVisibility annotation from a class or its package.
 * @param clazz Class to lookup annotation
 * @return Instantiated PropertyVisibilityStrategy if annotation is present
 */
public Optional<PropertyVisibilityStrategy> getPropertyVisibilityStrategy(Class<?> clazz) {
    JsonbVisibility visibilityAnnotation = findAnnotation(clazz.getDeclaredAnnotations(), JsonbVisibility.class);
    if ((visibilityAnnotation == null) && (clazz.getPackage() != null)) {
        visibilityAnnotation = findAnnotation(clazz.getPackage().getDeclaredAnnotations(), JsonbVisibility.class);
    }
    final Optional<JsonbVisibility> visibilityOptional = Optional.ofNullable(visibilityAnnotation);
    return visibilityOptional.map(jsonbVisibility -> ReflectionUtils.createNoArgConstructorInstance(jsonbVisibility.value()));
}
项目:yasson    文件:JsonbConfigProperties.java   
private PropertyVisibilityStrategy initPropertyVisibilityStrategy() {
    final Optional<Object> property = jsonbConfig.getProperty(JsonbConfig.PROPERTY_VISIBILITY_STRATEGY);
    if (!property.isPresent()) {
        return null;
    }
    final Object propertyVisibilityStrategy = property.get();
    if (!(propertyVisibilityStrategy instanceof PropertyVisibilityStrategy)) {
        throw new JsonbException("JsonbConfig.PROPERTY_VISIBILITY_STRATEGY must be instance of " + PropertyVisibilityStrategy.class);
    }
    return (PropertyVisibilityStrategy) propertyVisibilityStrategy;
}
项目:yasson    文件:DatesTest.java   
private Jsonb getJsonbWithMillisIgnored() {
    final JsonbConfig config = new JsonbConfig();
    config.withPropertyVisibilityStrategy(new PropertyVisibilityStrategy() {
        @Override
        public boolean isVisible(Field field) {
            return !field.getName().startsWith("millis");
        }

        @Override
        public boolean isVisible(Method method) {
            return false;
        }
    });
    return JsonbBuilder.create(config);
}
项目:johnzon    文件:JsonbAccessMode.java   
public JsonbAccessMode(final PropertyNamingStrategy propertyNamingStrategy, final String orderValue,
                       final PropertyVisibilityStrategy visibilityStrategy, final boolean caseSensitive,
                       final Map<AdapterKey, Adapter<?, ?>> defaultConverters, final JohnzonAdapterFactory factory,
                       final Supplier<JsonParserFactory> parserFactory, final AccessMode delegate) {
    this.naming = propertyNamingStrategy;
    this.order = orderValue;
    this.visibility = visibilityStrategy;
    this.caseSensitive = caseSensitive;
    this.delegate = delegate;
    this.defaultConverters = defaultConverters;
    this.factory = factory;
    this.parserFactory = parserFactory;
}
项目:johnzon    文件:JsonbAccessMode.java   
private boolean isTransient(final DecoratedType dt, final PropertyVisibilityStrategy visibility) {
    if (!FieldAndMethodAccessMode.CompositeDecoratedType.class.isInstance(dt)) {
        return isTransient(dt) || shouldSkip(visibility, dt);
    }
    final FieldAndMethodAccessMode.CompositeDecoratedType cdt = FieldAndMethodAccessMode.CompositeDecoratedType.class.cast(dt);
    return isTransient(cdt.getType1()) || isTransient(cdt.getType2()) ||
            (shouldSkip(visibility, cdt.getType1()) && shouldSkip(visibility, cdt.getType2()));
}
项目:johnzon    文件:JsonbAccessMode.java   
private boolean shouldSkip(final PropertyVisibilityStrategy visibility, final DecoratedType t) {
    return isNotVisible(visibility, t);
}
项目:johnzon    文件:JsonbAccessMode.java   
private boolean isNotVisible(PropertyVisibilityStrategy visibility, DecoratedType t) {
    return !(FieldAccessMode.FieldDecoratedType.class.isInstance(t) ?
            visibility.isVisible(FieldAccessMode.FieldDecoratedType.class.cast(t).getField())
            : (MethodAccessMode.MethodDecoratedType.class.isInstance(t) &&
            visibility.isVisible(MethodAccessMode.MethodDecoratedType.class.cast(t).getMethod())));
}
项目:yasson    文件:JsonbConfigProperties.java   
/**
 * Gets property visibility strategy.
 *
 * @return Property visibility strategy.
 */
public PropertyVisibilityStrategy getPropertyVisibilityStrategy() {
    return propertyVisibilityStrategy;
}