private Map<String, ?> readerConfig() { final Map<String, Object> map = new HashMap<>(); if (config == null) { return map; } config.getProperty(JsonParserFactoryImpl.BUFFER_LENGTH).ifPresent(b -> map.put(JsonParserFactoryImpl.BUFFER_LENGTH, b)); config.getProperty(JsonParserFactoryImpl.MAX_STRING_LENGTH).ifPresent(b -> map.put(JsonParserFactoryImpl.MAX_STRING_LENGTH, b)); config.getProperty(JsonParserFactoryImpl.SUPPORTS_COMMENTS).ifPresent(b -> map.put(JsonParserFactoryImpl.SUPPORTS_COMMENTS, b)); config.getProperty(AbstractJsonFactory.BUFFER_STRATEGY).ifPresent(b -> map.put(AbstractJsonFactory.BUFFER_STRATEGY, b)); return map; } }
private Map<String, ?> generatorConfig() { final Map<String, Object> map = new HashMap<>(); if (config == null) { return map; } config.getProperty(JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH).ifPresent(b -> map.put(JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH, b)); config.getProperty(AbstractJsonFactory.BUFFER_STRATEGY).ifPresent(b -> map.put(AbstractJsonFactory.BUFFER_STRATEGY, b)); config.getProperty(JsonbConfig.FORMATTING).ifPresent(b -> map.put(JsonGenerator.PRETTY_PRINTING, b)); return map; }
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); }
private Map<String, ?> readerConfig() { final Map<String, Object> map = new HashMap<>(); if (config == null) { return map; } config.getProperty(JsonParserFactoryImpl.BUFFER_LENGTH).ifPresent(b -> map.put(JsonParserFactoryImpl.BUFFER_LENGTH, b)); config.getProperty(JsonParserFactoryImpl.MAX_STRING_LENGTH).ifPresent(b -> map.put(JsonParserFactoryImpl.MAX_STRING_LENGTH, b)); config.getProperty(JsonParserFactoryImpl.SUPPORTS_COMMENTS).ifPresent(b -> map.put(JsonParserFactoryImpl.SUPPORTS_COMMENTS, b)); config.getProperty(AbstractJsonFactory.BUFFER_STRATEGY).ifPresent(b -> map.put(AbstractJsonFactory.BUFFER_STRATEGY, b)); return map; } }
private Map<String, ?> generatorConfig() { final Map<String, Object> map = new HashMap<>(); if (config == null) { return map; } config.getProperty(JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH).ifPresent(b -> map.put(JsonGeneratorFactoryImpl.GENERATOR_BUFFER_LENGTH, b)); config.getProperty(AbstractJsonFactory.BUFFER_STRATEGY).ifPresent(b -> map.put(AbstractJsonFactory.BUFFER_STRATEGY, b)); config.getProperty(JsonbConfig.FORMATTING).ifPresent(b -> map.put(JsonGenerator.PRETTY_PRINTING, b)); return map; }
@Override public JsonbBuilder withConfig(JsonbConfig config) { config.getProperty("jsonb.null-values") .ifPresent(o -> settings.skipDefaultValues(Boolean.FALSE.equals(o))); return this; }
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; }
@SuppressWarnings("unchecked") private <T> void mergeProperties(final String propertyKey, final T[] values, final Class<T> tClass) { final Optional<Object> property = getProperty(propertyKey); if (!property.isPresent()) { setProperty(propertyKey, values); return; } T[] storedValues = (T[]) property.get(); T[] newValues = (T[]) Array.newInstance(tClass, storedValues.length + values.length); System.arraycopy(storedValues, 0, newValues, 0, storedValues.length); System.arraycopy(values, 0, newValues, storedValues.length, values.length); setProperty(propertyKey, newValues); } }
@SuppressWarnings("unchecked") private Map<Class<?>,Class<?>> initUserTypeMapping() { Optional<Object> property = jsonbConfig.getProperty(YassonProperties.USER_TYPE_MAPPING); if (!property.isPresent()) { return Collections.emptyMap(); } Object result = property.get(); if (!(result instanceof Map)) { throw new JsonbException(Messages.getMessage(MessageKeys.JSONB_CONFIG_PROPERTY_INVALID_TYPE, YassonProperties.USER_TYPE_MAPPING, Map.class.getSimpleName())); } return (Map<Class<?>, Class<?>>) result; }
private boolean getBooleanConfigProperty(String propertyName, boolean defaultValue) { final Optional<Object> property = jsonbConfig.getProperty(propertyName); if (property.isPresent()) { final Object result = property.get(); if (!(result instanceof Boolean)) { throw new JsonbException(Messages.getMessage(MessageKeys.JSONB_CONFIG_PROPERTY_INVALID_TYPE, propertyName, Boolean.class.getSimpleName())); } return (boolean) result; } return defaultValue; }
private String getGlobalConfigJsonbDateFormat() { final Optional<Object> formatProperty = jsonbConfig.getProperty(JsonbConfig.DATE_FORMAT); return formatProperty.map(f -> { if (!(f instanceof String)) { throw new JsonbException(Messages.getMessage(MessageKeys.JSONB_CONFIG_PROPERTY_INVALID_TYPE, JsonbConfig.DATE_FORMAT, String.class.getSimpleName())); } return (String) f; }).orElse(JsonbDateFormat.DEFAULT_FORMAT); }
/** * Gets locale from {@link JsonbConfig}. * * @return Configured locale. */ private Locale initConfigLocale() { final Optional<Object> localeProperty = jsonbConfig.getProperty(JsonbConfig.LOCALE); return localeProperty.map(loc -> { if (!(loc instanceof Locale)) { throw new JsonbException(Messages.getMessage(MessageKeys.JSONB_CONFIG_PROPERTY_INVALID_TYPE, JsonbConfig.LOCALE, Locale.class.getSimpleName())); } return (Locale) loc; }).orElseGet(Locale::getDefault); }
private String toJson(String object, JsonbContext jsonbContext) { if ((boolean) jsonbContext.getConfig().getProperty(JsonbConfig.STRICT_IJSON).orElse(false)) { try { String newString = new String(object.getBytes("UTF-8"), "UTF-8"); if (!newString.equals(object)) { throw new JsonbException(Messages.getMessage(MessageKeys.UNPAIRED_SURROGATE)); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return object; }
@SuppressWarnings("unchecked") private <T> void mergeProperties(final String propertyKey, final T[] values, final Class<T> tClass) { final Optional<Object> property = getProperty(propertyKey); if (!property.isPresent()) { setProperty(propertyKey, values); return; } T[] storedValues = (T[]) property.get(); T[] newValues = (T[]) Array.newInstance(tClass, storedValues.length + values.length); System.arraycopy(storedValues, 0, newValues, 0, storedValues.length); System.arraycopy(values, 0, newValues, storedValues.length, values.length); setProperty(propertyKey, newValues); } }
private <T> JsonbConfig accumulate(final String key, final T[] values, final Class<T> componentType) { if (values == null || values.length == 0) { return this; } final Optional<Object> opt = getProperty(key); if (opt.isPresent()) { final T[] existing = (T[]) opt.get(); final T[] aggregated = (T[]) Array.newInstance(componentType, existing.length + values.length); System.arraycopy(existing, 0, aggregated, 0, existing.length); System.arraycopy(values, 0, aggregated, existing.length + 1, values.length); return setProperty(key, aggregated); } return setProperty(key, values); } }
private JohnzonAdapterFactory findFactory() { if (getBeanManager() == NO_BM || config.getProperty("johnzon.skip-cdi") .map(s -> "true".equalsIgnoreCase(String.valueOf(s))).orElse(false)) { return new SimpleJohnzonAdapterFactory(); } try { // don't trigger CDI is not there return new org.apache.johnzon.jsonb.factory.CdiJohnzonAdapterFactory(beanManager); } catch (final NoClassDefFoundError | Exception e) { return new SimpleJohnzonAdapterFactory(); } }
@Override protected String deserialize(String jsonValue, Unmarshaller unmarshaller, Type rtType) { if ((boolean) unmarshaller.getJsonbContext().getConfig().getProperty(JsonbConfig.STRICT_IJSON).orElse(false)) { try { String newString = new String(jsonValue.getBytes("UTF-8"), "UTF-8"); if (!newString.equals(jsonValue)) { throw new JsonbException(Messages.getMessage(MessageKeys.UNPAIRED_SURROGATE)); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return jsonValue; } }
private JohnzonAdapterFactory findFactory() { if (getBeanManager() == NO_BM || config.getProperty("johnzon.skip-cdi") .map(s -> "true".equalsIgnoreCase(String.valueOf(s))).orElse(false)) { return new SimpleJohnzonAdapterFactory(); } try { // don't trigger CDI is not there return new org.apache.johnzon.jsonb.factory.CdiJohnzonAdapterFactory(beanManager); } catch (final NoClassDefFoundError | Exception e) { return new SimpleJohnzonAdapterFactory(); } }
private JsonGenerator streamGenerator(OutputStream stream) { Map<String, ?> factoryProperties = createJsonpProperties(jsonbContext.getConfig()); final String encoding = (String) jsonbContext.getConfig().getProperty(JsonbConfig.ENCODING).orElse("UTF-8"); return jsonbContext.getJsonProvider().createGeneratorFactory(factoryProperties).createGenerator(stream, Charset.forName(encoding)); }
private JsonParser inputStreamParser(InputStream stream) { return new JsonbRiParser(jsonbContext.getJsonProvider().createParserFactory(createJsonpProperties(jsonbContext.getConfig())) .createParser(stream, Charset.forName((String) jsonbContext.getConfig().getProperty(JsonbConfig.ENCODING).orElse("UTF-8")))); }