Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude null-value fields when using Flexjson?

I am serializing a class like this into JSON using Flexjson:

public class Item {
    private Long id;
    private String name;
    private String description;
    ...

    // Getters and setters
    ...
}

Many of the Item fields can be null (e.g., description). Consequently, when such an Item object is serialized using Flexjson, I get the following JSON:

 {"id":62,"name":"Item A","description":null,...}

Since, as I already mentioned, an Item object may contain many null-value fields, the outcoming JSON is longer than effectively needed. This is in so far a problem, because I would like to send the generated JSON from a web server to a mobile client over a wireless connection via WiFi, 3G, EDGE or GPRS (i.e., more bandwidth is required, which results in less speed).

Therefore, I wanted to ask how it is possible to (efficiently) exclude null-value attributes using Flexjson?

Thanks!

like image 243
Matthias Avatar asked Oct 25 '11 08:10

Matthias


People also ask

How do you ignore null values?

In order to ignore null fields at the class level, we use the @JsonInclude annotation with include. NON_NULL. Let's take an example to understand how we can use @JsonInclude annotation to ignore the null fields at the class level.

How do I ignore a field in null JSON?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.

How do I ignore null values in post request body in spring boot?

Just use this @JsonSerialize(include = Inclusion. NON_NULL) instead of @JsonInclude(Include. NON_NULL) and it works..!!

How do you tell Jackson to ignore a field during Deserialization if its value is null?

Jackson default include null fields 1.2 By default, Jackson will include the null fields. To ignore the null fields, put @JsonInclude on class level or field level.


2 Answers

You can use the following transformer :

import flexjson.transformer.AbstractTransformer;

public class ExcludeTransformer extends AbstractTransformer {

  @Override
  public Boolean isInline() {
      return true;
  }

  @Override
  public void transform(Object object) {
      // Do nothing, null objects are not serialized.
      return;
  }
}

with the following usage :

new JSONSerializer().transform(new ExcludeTransformer(), void.class).serialize(yourObject)

Note that all null fields will be excluded.

Adding the Transformer by Path (vs by Class) is not supported as FlexJSON forces TypeTransformer for null values :

JSONContext.java : line 95 :

private Transformer getPathTransformer(Object object) {
    if (null == object) return getTypeTransformer(object);
    return pathTransformerMap.get(path);
}
like image 160
benp Avatar answered Nov 14 '22 22:11

benp


I am a newbie,i had same problem and could not find any solution on source forge so i used regular expression to remove all the nulls from JSON String

/**
 * This Function removes all the key:value pairs from the Json String for which the value equals null
 * @param jsonStringWithNullKeys
 * @return jsonStringWithoutNullKeys
 */
public static String getJsonStringWithoutNullKeys(String jsonStringWithNullKeys)
{
    Pattern p = Pattern.compile( "([,]?\"[^\"]*\":null[,]?)+" );
    Matcher m = p.matcher( jsonStringWithNullKeys );
    StringBuffer newString = new StringBuffer( jsonStringWithNullKeys.length() );

    while( m.find() )
    {
        if( m.group().startsWith( "," ) & m.group().endsWith( "," ) ) m.appendReplacement( newString, "," );
        else
            m.appendReplacement( newString, "" );
    }
    m.appendTail( newString );
    return newString.toString();
}
like image 33
faizan Avatar answered Nov 14 '22 23:11

faizan