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 Long 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, Long propertyValue) { 065 if (propertyValue != null) { 066 jsonObject.add(propertyName, propertyValue); 067 } 068 } 069 070 /** 071 * Adds Enum 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, Enum propertyValue) { 078 if (propertyValue != null) { 079 jsonObject.add(propertyName, propertyValue.name().toLowerCase()); 080 } 081 } 082 083 /** 084 * Adds Date property to the json object if it's not null. 085 * 086 * @param jsonObject json object that the key/value will be added to. 087 * @param propertyName name of the property in json (key). 088 * @param propertyValue value of the property. 089 */ 090 public static void addIfNotNull(JsonObject jsonObject, String propertyName, Date propertyValue) { 091 if (propertyValue != null) { 092 jsonObject.add(propertyName, BoxDateFormat.format(propertyValue)); 093 } 094 } 095 096 /** 097 * Add JsonObject property to json object if it's not null. 098 * 099 * @param jsonObject json object that the key/value will be added to. 100 * @param propertyName name of the property in json (key). 101 * @param propertyValue value of the property. 102 */ 103 public static void addIfNotNull(JsonObject jsonObject, String propertyName, JsonObject propertyValue) { 104 if (propertyValue != null) { 105 jsonObject.add(propertyName, propertyValue); 106 } 107 } 108 109 /** 110 * Add double property to json object if it's not null. 111 */ 112 public static void addIfNotNull(JsonObject jsonObject, String propertyName, Double propertyValue) { 113 if (propertyValue != null) { 114 jsonObject.add(propertyName, propertyValue); 115 } 116 } 117}