3

I am using Jackson in the jsonrpc4j to access a remote service. In my Java application there are no defined classes for the return values, so deserialization produces generic LinkedHashMaps. So I cannot put any annotations anywhere. jsonrpc4j can take in a Jackson ObjectMapper object. The remote service responds with structured json objects, some fields of which are very big decimal numbers, and Jackson treats them as Doubles. An example object can look like this

{"s1":"zxcvb","f1":20.00234,"a1":[{"f2":3883.99400943},{"f3":0.00093944432}]}

I would like it instead interpreting all the numbers as either strings or Decimals with configurable precision and parsing them correctly according to those types. Is it possible to do this using a modified ObjectMapper object? If not that, what would be the easiest way to achieve this?

Cray
  • 2,396
  • 19
  • 29
  • what code have you got so far? – Chris Apr 30 '13 at 22:15
  • It's basically just the example code from http://code.google.com/p/jsonrpc4j/ for the Jsonrpc Client, I don't know even where to start experimenting, since ObjectMapper class does not seem to have any configuration options or obvious override methods to achieve this easily. – Cray Apr 30 '13 at 22:18
  • Look at the mixin annotations. Will they work for you? http://wiki.fasterxml.com/JacksonMixInAnnotations?highlight=%28%28JacksonAnnotations%29%29 – Lee Meador Apr 30 '13 at 22:20
  • Could you please specify how to use them (like, on which class)? I don't have any actual classes to be serialized, the proxy functions are returning a LinkedHashMap with all the values it automatically parses from JSON. (I've tried LinkedHashMap, but some of JSON objects have sublevels, which produces errors.) – Cray Apr 30 '13 at 22:26

1 Answers1

6

I think I found it: On my first search I missed that Jackson serialization had both SerializationFeatures and DeserializationFeatures, and they are a little different. According to http://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/DeserializationFeature.html there is a feature called USE_BIG_DECIMAL_FOR_FLOATS

Feature that determines whether JSON floating point numbers are to be deserialized into BigDecimals if only generic type description (either Object or Number, or within untyped Map or Collection context) is available.

So in my case it was basically

ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);

JsonRpcHttpClient rpcHttpClient = new JsonRpcHttpClient(
    mapper,
    new URL("the url"),
    new HashMap<String, String>()); 
Cray
  • 2,396
  • 19
  • 29