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

项目: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    文件:ByteArrayBase64Serializer.java   
private Base64.Encoder getEncoder(String strategy) {
    switch (strategy) {
        case BinaryDataStrategy.BASE_64:
            return Base64.getEncoder();
        case BinaryDataStrategy.BASE_64_URL:
            return Base64.getUrlEncoder();
        default:
            throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR,
                    "Invalid strategy: " + strategy));
    }
}
项目:yasson    文件:ByteArrayBase64Deserializer.java   
private Base64.Decoder getDecoder(String strategy) {
    switch (strategy) {
        case BinaryDataStrategy.BASE_64:
            return Base64.getDecoder();
        case BinaryDataStrategy.BASE_64_URL:
            return Base64.getUrlDecoder();
        default:
            throw new JsonbException(Messages.getMessage(MessageKeys.INTERNAL_ERROR, "Invalid strategy: " + strategy));
    }
}
项目:yasson    文件:JsonbConfigProperties.java   
private String initBinaryDataStrategy() {
    final Optional<Boolean> iJson = jsonbConfig.getProperty(JsonbConfig.STRICT_IJSON).map((obj->(Boolean)obj));
    if (iJson.isPresent() && iJson.get()) {
        return BinaryDataStrategy.BASE_64_URL;
    }
    final Optional<String> strategy = jsonbConfig.getProperty(JsonbConfig.BINARY_DATA_STRATEGY).map((obj) -> (String) obj);
    return strategy.orElse(BinaryDataStrategy.BYTE);
}
项目:yasson    文件:DefaultSerializersTest.java   
@Test
public void testByteArrayWithBinaryStrategy() {
    byte[] array = {127, -128, 127};
    Jsonb jsonb = (new JsonBindingBuilder().withConfig(new JsonbConfig().withBinaryDataStrategy(BinaryDataStrategy.BYTE))).build();

    assertEquals("[127,-128,127]", jsonb.toJson(array));
    assertArrayEquals(array, jsonb.fromJson("[127,-128,127]", byte[].class));
}
项目:yasson    文件:DefaultSerializersTest.java   
@Test
public void testByteArrayWithStrictJsonAndBinaryStrategy() {
    byte[] array = {1, 2, 3};
    ByteArrayWrapper byteArrayWrapper = new ByteArrayWrapper();
    byteArrayWrapper.array = array;
    Jsonb jsonb = (new JsonBindingBuilder().withConfig(new JsonbConfig().withStrictIJSON(true).withBinaryDataStrategy(BinaryDataStrategy.BYTE))).build();
    final String base64UrlEncodedJson = "{\"array\":\"" + Base64.getUrlEncoder().encodeToString(array) + "\"}";
    assertEquals(base64UrlEncodedJson, jsonb.toJson(byteArrayWrapper));
    ByteArrayWrapper result = jsonb.fromJson(base64UrlEncodedJson, ByteArrayWrapper.class);
    assertArrayEquals(array, result.array);

    jsonb = (new JsonBindingBuilder().withConfig(new JsonbConfig().withStrictIJSON(true).withBinaryDataStrategy(BinaryDataStrategy.BASE_64))).build();
    assertEquals(base64UrlEncodedJson, jsonb.toJson(byteArrayWrapper));
    result = jsonb.fromJson(base64UrlEncodedJson, ByteArrayWrapper.class);
    assertArrayEquals(array, result.array);

    jsonb = (new JsonBindingBuilder().withConfig(new JsonbConfig().withStrictIJSON(true).withBinaryDataStrategy(BinaryDataStrategy.BASE_64_URL))).build();
    assertEquals(base64UrlEncodedJson, jsonb.toJson(byteArrayWrapper));
    result = jsonb.fromJson(base64UrlEncodedJson, ByteArrayWrapper.class);
    assertArrayEquals(array, result.array);

    jsonb = (new JsonBindingBuilder().withConfig(new JsonbConfig().withBinaryDataStrategy(BinaryDataStrategy.BYTE))).build();
    assertEquals("[1,2,3]", jsonb.toJson(array));
    result = jsonb.fromJson("{\"array\":[1,2,3]}", ByteArrayWrapper.class);
    assertArrayEquals(array, result.array);

    jsonb = (new JsonBindingBuilder().withConfig(new JsonbConfig().withBinaryDataStrategy(BinaryDataStrategy.BASE_64))).build();
    final String base64EncodedJson = "{\"array\":\"" + Base64.getEncoder().encodeToString(array) + "\"}";
    assertEquals(base64EncodedJson, jsonb.toJson(byteArrayWrapper));
    result = jsonb.fromJson(base64EncodedJson, ByteArrayWrapper.class);
    assertArrayEquals(array, result.array);

    jsonb = (new JsonBindingBuilder().withConfig(new JsonbConfig().withBinaryDataStrategy(BinaryDataStrategy.BASE_64_URL))).build();
    assertEquals(base64UrlEncodedJson, jsonb.toJson(byteArrayWrapper));
    result = jsonb.fromJson(base64UrlEncodedJson, ByteArrayWrapper.class);
    assertArrayEquals(array, result.array);
}
项目:yasson    文件:SerializerBuilder.java   
/**
 * Builds a {@link JsonbSerializer}.
 *
 * @return JsonbSerializer.
 */
public JsonbSerializer<?> build() {
    runtimeType = resolveRuntimeType();

    if (getModel() != null && (getModel().getCustomization() == null
            || getModel().getCustomization() instanceof ComponentBoundCustomization)) {
        ComponentBoundCustomization customization = (ComponentBoundCustomization) getModel().getCustomization();
        //First check if user deserializer is registered for such type
        final ComponentMatcher componentMatcher = jsonbContext.getComponentMatcher();
        Optional<SerializerBinding<?>> userSerializer = componentMatcher.getSerializerBinding(getRuntimeType(), customization);
        if (userSerializer.isPresent()  && !jsonbContext.containsProcessedType(userSerializer.get().getBindingType())) {
            return new UserSerializerSerializer<>(model, userSerializer.get().getJsonbSerializer());
        }

        //Second user components is registered.
        Optional<AdapterBinding> adapterInfoOptional = componentMatcher.getAdapterBinding(getRuntimeType(), customization);
        if (adapterInfoOptional.isPresent() && !jsonbContext.containsProcessedType(adapterInfoOptional.get().getBindingType())) {
            return new AdaptedObjectSerializer<>(getModel(), adapterInfoOptional.get());
        }
    }

    final Optional<AbstractValueTypeSerializer<?>> supportedTypeSerializer = getSupportedTypeSerializer(objectClass);
    if (supportedTypeSerializer.isPresent()) {
        return supportedTypeSerializer.get();
    }

    if (Collection.class.isAssignableFrom(objectClass)) {
        return new CollectionSerializer<>(this);
    } else if (Map.class.isAssignableFrom(objectClass)) {
        return new MapSerializer<>(this);
    } else if (isByteArray(objectClass)) {
        String strategy = jsonbContext.getConfigProperties().getBinaryDataStrategy();
        switch (strategy) {
            case BinaryDataStrategy.BYTE:
                return new ByteArraySerializer(this);
            default:
                return new ByteArrayBase64Serializer(byte[].class, getModel());
        }
    } else if (objectClass.isArray() || getRuntimeType() instanceof GenericArrayType) {
        return createArrayItem(objectClass.getComponentType());

    } else if (JsonValue.class.isAssignableFrom(objectClass)) {
        if(JsonObject.class.isAssignableFrom(objectClass)) {
            return new JsonObjectSerializer(this);
        } else {
            return new JsonArraySerializer(this);
        }
    } else if (Optional.class.isAssignableFrom(objectClass)) {
        return new OptionalObjectSerializer<>(this);
    } else {
        jsonbContext.getMappingContext().addSerializerProvider(objectClass, new ObjectSerializerProvider());
        return new ObjectSerializer<>(this);
    }

}