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