logo
down
shadow

Jackson deserialize map null values to empty string


Jackson deserialize map null values to empty string

By : Innocent Ramsis Band
Date : November 27 2020, 04:01 AM
I hope this helps you . I'm trying to find a global ObjectMapper configuration (or any other customization) for all json Map to get deserialized into a map without null values (there should be an empty String instead) , I ended up using DeserializerModifier technique as follows:
code :
static class MapCustomDeserializerModifier extends BeanDeserializerModifier {

  @Override
  public JsonDeserializer<?> modifyMapDeserializer(DeserializationConfig config, MapType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer {

    final JsonDeserializer<?> jsonDeserializer = super.modifyMapDeserializer(config, type, beanDesc, deserializer);
    if (type.getKeyType().isTypeOrSubTypeOf(String.class) && type.getContentType().isTypeOrSubTypeOf(String.class)) {
      return new MapCustomDeserializer();
    }

    return jsonDeserializer;
  }

}

static class MapCustomDeserializer extends JsonDeserializer<Map<String, String>> {

  @Override
  public Map<String, String> deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {

    final Map<String, String> map = jsonParser.readValueAs(Map.class);

    if (map != null) {
      map.replaceAll((key, value) -> Strings.nullToEmpty(value));
    }
    return map;
  }
}


Share : facebook icon twitter icon
Jackson to deserialize Map variable as empty Map instead of null

Jackson to deserialize Map variable as empty Map instead of null


By : chhalam saddam hossa
Date : March 29 2020, 07:55 AM
it helps some times You can setup you own module and override getNullValue()
see doc http://jackson.codehaus.org/1.9.9/javadoc/org/codehaus/jackson/map/JsonDeserializer.html
code :
    SimpleModule module = new SimpleModule("test",  new Version(1, 0, 0, null));

    module.addDeserializer(Map.class, new JsonDeserializer<Map>() {
        @Override
        public Map deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            return jp.readValueAs(HashMap.class);
        }

        @Override
        public Map getNullValue() {
            return new HashMap();
        }
    });

    mapper.registerModule(module);
String s = "{\"names\":{\"1\":2}, \"descriptions\":null, \"nicknames\":null}";
User{descriptions={}, names={1=2}, nicknames={}}
Jackson and Bean Validation: Deserialize empty String as null

Jackson and Bean Validation: Deserialize empty String as null


By : user3305904
Date : March 29 2020, 07:55 AM
around this issue If you want to accept the empty string, you could use the @Pattern constraint matching also the empty string: @Pattern(regexp = "^$|(.){2}") or @Pattern(regexp = "^$|...
The alternative is to write your own custom constraint.
Jackson : Filter null or blank values while converting json string response to pojo conversion using jackson

Jackson : Filter null or blank values while converting json string response to pojo conversion using jackson


By : procrastinator 100
Date : March 29 2020, 07:55 AM
may help you . I am using jackson to convert jason response to List of pojo. Below is the response which I am getting. , You probably want to annotate your bean class like this:
How to deserialize Jackson Json NULL String to Date with JsonFormat

How to deserialize Jackson Json NULL String to Date with JsonFormat


By : Sandip Mavani
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Your problem is not that a null value is passed in the JSON. The problem is that the JSON contains a string that has the value "NULL".
So, in order to fix this there are a number of available approaches. I think that the following two will work for this case.
jackson - How to deserialize empty as null

jackson - How to deserialize empty as null


By : gas1121
Date : March 29 2020, 07:55 AM
Hope this helps Configure your mapper to enable the ACCEPT_EMPTY_STRING_AS_NULL_OBJECT feature.
Related Posts Related Posts :
  • How do I make this image larger?
  • Run icCube with JDK 10
  • Expression with Math.random() always returns the same value
  • Getting the Set with the most elements nested in a HashMap using Java Streams
  • XmlSlurper to parse XML and get value of inside elements using Groovy
  • Extracting data from HTML and formatting the output
  • SOLR documentCache JMX metrics clarification
  • Limiting Wildfly 14 Two-Way SSL to specific clients
  • How do I get Min and max values to only print when "year" is entered?
  • Hashmap can't loop - getKey() method not found - Using Java 8
  • Android Studio - Create an EditText with a click of a button
  • Mockito Test not invoking verify() method
  • Wrap method implementations of Java interfaces
  • Remediating dynamic SQL into prepared statements
  • Where do X and Y start at in swing windows
  • java code with files work from eclipse but dont work from cmd
  • Return page object from JPA query
  • I can't figure out why this code in my APCS multiple choice book returns 19
  • How to save data between methods
  • I'm trying to install Apache Gobblin. How can I install it using Gradle?
  • Spring Data Sorting Array or Set into Pageable
  • Question about the Java documentation and its implementation
  • How to make a JButton that when pressed it does a new action
  • Java hibernate No validator could be found for boolean
  • Making a POJO Thread Safe
  • Save the data of a text file in a arraylist
  • Sort a List<String[]> by indices using Comparator
  • Overloading in Java for user input?
  • Unable to format timestamp as YYYY-MM-DD HH:mm:ss in java
  • Access SQLite Helper From Adapter
  • How to stream a csv file with header to a HashMap<String, Double> in Java?
  • can't get go daddy ssl certificate to work with spring boot
  • ResourceBundle can't find BaseName gradle project java
  • Java; Jackson; Parsing the array of array json string
  • Java - map key lookup ignoring case
  • Anyone knows why setCount() is not working in twitter4j?
  • Object Visibility in a Multi-threaded Program in Java
  • Can't store and load an arraylist in an object file
  • convert a string number starts with `00` to `+` in java
  • Java - avoiding NonSuchElementException using ConcurrentLinkedDeque
  • Converting Immutable to mutable list Java
  • Getting nosuchmethod exception
  • How to get MQTT subscriptions
  • Android Google Sign in Exceptions
  • JavaFX - method that waits for user input
  • Replacing values for a particular key in treemap changes values for every key
  • This method call passes a null value for a nonnull method parameter. Either the parameter is annotated as a parameter th
  • Kafka: consume all messages on demand
  • Notify what text was changed in textview
  • Tinkerpop/Gremlin: select vertices together with outgoing edge count
  • transform a list of objects into a list of integers that pass a check
  • Why this java code is showing strange behavior?
  • Maven Project classes not compiling
  • Edit image to make text more clear opencv
  • Android Spinner nullpointer
  • Add result to int array every time you finish counting the occurrence
  • Android import java library
  • How to use LDAP Authentication in a corporate environment
  • adding item during iteration in java special usecase
  • How can I sort a map with string key? like (1 foo , 2 foo)
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org