44995

Deserialize JSON into a generic map, forcing all JSON floats into Decimal or String, in Jackson

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?

Answer1:

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>());

Recommend

  • C++ Tree class : constructor / copy / memory leak?
  • Using Test Doubles with DbEntityEntry and DbPropertyEntry
  • Incorrect VBA Overflow using Doubles (Excel)
  • Sending a sub-segment of an ArrayBuffer over a WebSocket without copying
  • Accelerate Framework FFT vDSPztoc split real form to split real vector
  • JTable fails to sort Integers and Double values properly
  • Is there a better way for reduce operation on RDD[Array[Double]]
  • Huffman Tree Encoding
  • .NET math calculation performances
  • How to restrict template functor return and parameter types
  • Get all root sites listed in share point Admin center using graph api beta
  • Excel comparison with multiple IFs
  • Converting Negative Decimal To String Loses the -
  • Matlab FFT and FFTW
  • Why do the numeric format strings in C# round the number when not using decimals (F0)?
  • Converting a list of strings to ints (or doubles) in Python
  • About multiple inheritance and ambiguity
  • float vs double comparison [duplicate]
  • PHP: Convert single-quoted string into double-quoted
  • Python functions: Pass global variables if only accessing them?
  • Sending keystrokes from a C# application to a Java application - strange behaviour?
  • How to get a list of all blobs in a repository in Git
  • Use double quote then curly brace in powershell -Command
  • How to calculate total Fridays between two dates in excel on weekly, biweekly and monthly basis?
  • WebApp in AppServices vs CloudService
  • How do i find all references to a user control
  • Excel distinct count with conditions
  • Action Pack components in Rails
  • several dataProvider per one Test in TestNG
  • How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Atlas images wrong size on iPad iOS 9
  • Splitting given String into two variables - php
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Deserializing XML into class C#
  • How to model a transition system with SPIN
  • Build own AppleScript numerical error handling
  • VBA Convert delimiter text file to Excel
  • Buffer size for converting unsigned long to string