List of usage examples for java.math BigDecimal BigDecimal
public BigDecimal(long val)
From source file:org.mstc.zmq.json.Encoder.java
public static String encode(Object o) throws IOException { StringWriter writer = new StringWriter(); ObjectMapper mapper = new ObjectMapper(); mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); JsonFactory factory = mapper.getFactory(); try (JsonGenerator g = factory.createGenerator(writer)) { g.writeStartObject();/*ww w. j ava2s. c o m*/ for (Field f : o.getClass().getDeclaredFields()) { try { f.setAccessible(true); Object value = f.get(o); if (value != null) { if (f.getType().isAssignableFrom(List.class)) { String items = mapper.writeValueAsString(value); g.writeStringField(f.getName(), items); } else if (f.getType().isArray()) { if (f.getType().getComponentType().isAssignableFrom(byte.class)) { g.writeBinaryField(f.getName(), (byte[]) value); } else { int length = Array.getLength(value); g.writeFieldName(f.getName()); g.writeStartArray(); for (int i = 0; i < length; i++) { Object av = Array.get(value, i); if (av instanceof Double) { g.writeNumber(new BigDecimal((Double) av).toPlainString()); } else if (av instanceof Float) { g.writeNumber(new BigDecimal((Float) av).toPlainString()); } else if (av instanceof Integer) { g.writeNumber(new BigDecimal((Integer) av).toPlainString()); } else { g.writeObject(av); } /*if (av instanceof Double) g.writeNumber(new BigDecimal((Double) av)); else if (av instanceof Float) g.writeNumber(new BigDecimal((Float) av)); else if (av instanceof Integer) g.writeNumber((Integer) av);*/ } g.writeEndArray(); } } else { g.writeObjectField(f.getName(), value); } } } catch (IllegalAccessException e) { logger.warn("Could not get field: {}", f.getName(), e); } } g.writeEndObject(); } if (logger.isDebugEnabled()) logger.debug(writer.toString()); return writer.toString(); }