JSR-353 How to add null values using javax.json.JsonObjectBuilder

后端 未结 2 732
夕颜
夕颜 2021-01-04 01:29

AS the javax.json docs suggest the way to create a JsonObject is using the provided builders like:

JsonBuilderFactory factory = Json.createBuilderFactory(con         


        
相关标签:
2条回答
  • 2021-01-04 01:59

    Another possible solution is to use simple and lean helper to wrap a value into JsonValue object or return JsonValue.NULL if value is null.

    Here is an example:

    public final class SafeJson {
        private static final String KEY = "1";
    
        //private ctor with exception throwing
    
        public static JsonValue nvl(final String ref) {
            if (ref == null) {
                return JsonValue.NULL;
            }
            return Json.createObjectBuilder().add(KEY, ref).build().get(KEY);
        }
    
        public static JsonValue nvl(final Integer ref) {
            if (ref == null) {
                return JsonValue.NULL;
            }
            return Json.createObjectBuilder().add(KEY, ref).build().get(KEY);
        }
    
        public static JsonValue nvl(final java.sql.Date ref) {
            if (ref == null) {
                return JsonValue.NULL;
            }
            return Json.createObjectBuilder().add(KEY, ref.getTime()).build().get(KEY);
        }
    }
    

    Usage is simple:

    Json.createObjectBuilder().add("id", this.someId)
        .add("zipCode", SafeJson.nvl(this.someNullableInt))
        .add("phone", SafeJson.nvl(this.someNullableString))
        .add("date", SafeJson.nvl(this.someNullableDate))
        .build(); 
    

    Constructs like Json.createObjectBuilder().add(KEY, ref).build().get(KEY); looks a little bit weird but it is the only portable way to wrap value into JsonValue I've found so far.

    In fact in JavaEE8 you can replace it with:

    Json.createValue()
    

    or underlying:

    JsonProvider.provider().createValue()
    

    call.

    0 讨论(0)
  • 2021-01-04 02:03

    It might be difficult to get your JsonObjectBuilder implementation from the factory but you can make it simpler.

    Create a JsonObjectBuilder decorator class that checks for null:

    public class NullAwareJsonObjectBuilder implements JsonObjectBuilder {
        // Use the Factory Pattern to create an instance.
        public static JsonObjectBuilder wrap(JsonObjectBuilder builder) {
          if (builder == null) {
            throw new IllegalArgumentException("Can't wrap nothing.");
          }
          return new NullAwareJsonObjectBuilder(builder);
        }
    
        // Decorated object per Decorator Pattern.
        private final JsonObjectBuilder builder;
    
        private NullAwareJsonObjectBuilder(JsonObjectBuilder builder) {
          this.builder = builder;
        }
    
        public JsonObjectBuilder add(String name, JsonValue value) {
          builder.add(name, (value == null) ? JsonValue.NULL : value);
        }
    
        // Implement all other JsonObjectBuilder methods.
        ..
    }
    

    And how you to use it:

    JsonObjectBuilder builder = NullAwareJsonObjectBuilder.wrap(
            factory.createObjectBuilder());
    builder.add("firstname", customer.getFirstame())
        .add(...
    
    0 讨论(0)
提交回复
热议问题