001package com.box.sdk.internal.utils; 002 003import java.util.Date; 004 005import com.box.sdk.BoxDateFormat; 006import com.eclipsesource.json.JsonObject; 007 008/** 009 * Utility class for helping with json related operations. 010 */ 011public class JsonUtils { 012 013 /** 014 * Only static members. 015 */ 016 protected JsonUtils() { 017 } 018 019 /** 020 * Adds String property to the json object if it's not null. 021 * @param jsonObject json object that the key/value will be added to. 022 * @param propertyName name of the property in json (key). 023 * @param propertyValue value of the property. 024 */ 025 public static void addIfNotNull(JsonObject jsonObject, String propertyName, String propertyValue) { 026 if (propertyValue != null) { 027 jsonObject.add(propertyName, propertyValue); 028 } 029 } 030 031 /** 032 * Adds Boolean property to the json object if it's not null. 033 * @param jsonObject json object that the key/value will be added to. 034 * @param propertyName name of the property in json (key). 035 * @param propertyValue value of the property. 036 */ 037 public static void addIfNotNull(JsonObject jsonObject, String propertyName, Boolean propertyValue) { 038 if (propertyValue != null) { 039 jsonObject.add(propertyName, propertyValue); 040 } 041 } 042 043 /** 044 * Adds Integer property to the json object if it's not null. 045 * @param jsonObject json object that the key/value will be added to. 046 * @param propertyName name of the property in json (key). 047 * @param propertyValue value of the property. 048 */ 049 public static void addIfNotNull(JsonObject jsonObject, String propertyName, Integer propertyValue) { 050 if (propertyValue != null) { 051 jsonObject.add(propertyName, propertyValue); 052 } 053 } 054 055 /** 056 * Adds Enum property to the json object if it's not null. 057 * @param jsonObject json object that the key/value will be added to. 058 * @param propertyName name of the property in json (key). 059 * @param propertyValue value of the property. 060 */ 061 public static void addIfNotNull(JsonObject jsonObject, String propertyName, Enum propertyValue) { 062 if (propertyValue != null) { 063 jsonObject.add(propertyName, propertyValue.name().toLowerCase()); 064 } 065 } 066 067 /** 068 * Adds Date property to the json object if it's not null. 069 * @param jsonObject json object that the key/value will be added to. 070 * @param propertyName name of the property in json (key). 071 * @param propertyValue value of the property. 072 */ 073 public static void addIfNotNull(JsonObject jsonObject, String propertyName, Date propertyValue) { 074 if (propertyValue != null) { 075 jsonObject.add(propertyName, BoxDateFormat.format(propertyValue)); 076 } 077 } 078}