/** * Managed JSON writer * * @param any * Any object to serialize. * @return The JSON string from an object. */ public static String toJSon(final Object any) { try { return MAPPER.writeValueAsString(any); } catch (final JsonProcessingException e) { throw new TechnicalException("Unable to build JSon data from bean " + any, e); } }
/** * Return a JSON string corresponding to the given object. * * @param object * the error object. * @return the JSON entity. */ protected Object toEntity(final Object object) { try { return jacksonJsonProvider.locateMapper(object.getClass(), MediaType.APPLICATION_JSON_TYPE).writeValueAsString(object); } catch (final JsonProcessingException e) { // Ignore this error at UI level but trace it throw new TechnicalException("Unable to build a JSON string from a server error", e); } } }
/** * Return End Of Input indicator. */ private boolean isEndOfInput() { if (isEndOfWord()) { // Real separator, close definitely the previous word parsing return true; } if (Character.isWhitespace(current)) { return false; } throw new TechnicalException("Invalid character '" + current + "', white space, EOL, EOF or separator was expected near entry " + values); } }
/** * Return the field from the given class. * * @param beanType * Class of bean to build. * @param property * the bean property to get. * @return the {@link Field} of this property. */ protected Field getField(final Class<?> beanType, final String property) { final Field field = FieldUtils.getField(beanType, property, true); if (field == null) { throw new TechnicalException("Unknown property " + property + " in class " + beanType.getName()); } return field; }
/** * Build parameter configuration from the string definition. * * @param content * The content JSON string configuration. May be <code>null</code>. * @param valueTypeRef * The type reference to fix the return type. * @param <T> * The return type. * @return The parameter configuration. */ public static <T> T toConfiguration(final String content, final TypeReference<T> valueTypeRef) { try { return MAPPER.readValue(ObjectUtils.defaultIfNull(content, "{}"), valueTypeRef); } catch (final IOException e) { throw new TechnicalException("Unable to build configuration from " + content, e); } }
/** * Return a bean read from the reader. * * @param values * the property values. * @return the bean built with values. */ protected T build(final List<String> values) { if (values.isEmpty()) { return null; } if (headers.length < values.size()) { throw new TechnicalException(String.format("Too much values for type %s. Expected : %d, got : %d : %s", clazz.getName(), headers.length, values.size(), values)); } // Build the instance try { final T bean = clazz.getDeclaredConstructor().newInstance(); // Fill the instance fillInstance(bean, values); // Bean is completed return bean; } catch (final Exception e) { throw new TechnicalException("Unable to build an object of type : " + clazz, e); } }
throw new TechnicalException("Duplicate map entry key='" + key + "' for map property " + property + " in class " + bean.getClass().getName());
/** * Write a line corresponding to the given object. */ private void writeLine(final String[] headers, final Object o) { boolean first = true; try { for (final String header : headers) { writeSeparator(first); first = false; writeField(o, header); } } catch (final Exception e) { throw new TechnicalException("Unable to describe given object : " + o, e); } }