001package com.box.sdk.internal.utils;
002
003import com.box.sdk.BoxDateFormat;
004import com.eclipsesource.json.JsonObject;
005import java.util.Date;
006
007/** Utility class for helping with json related operations. */
008public class JsonUtils {
009
010  /** Only static members. */
011  protected JsonUtils() {}
012
013  /**
014   * Adds String property to the json object if it's not null.
015   *
016   * @param jsonObject json object that the key/value will be added to.
017   * @param propertyName name of the property in json (key).
018   * @param propertyValue value of the property.
019   */
020  public static void addIfNotNull(
021      JsonObject jsonObject, String propertyName, String propertyValue) {
022    if (propertyValue != null) {
023      jsonObject.add(propertyName, propertyValue);
024    }
025  }
026
027  /**
028   * Adds Boolean property to the json object if it's not null.
029   *
030   * @param jsonObject json object that the key/value will be added to.
031   * @param propertyName name of the property in json (key).
032   * @param propertyValue value of the property.
033   */
034  public static void addIfNotNull(
035      JsonObject jsonObject, String propertyName, Boolean propertyValue) {
036    if (propertyValue != null) {
037      jsonObject.add(propertyName, propertyValue);
038    }
039  }
040
041  /**
042   * Adds Integer property to the json object if it's not null.
043   *
044   * @param jsonObject json object that the key/value will be added to.
045   * @param propertyName name of the property in json (key).
046   * @param propertyValue value of the property.
047   */
048  public static void addIfNotNull(
049      JsonObject jsonObject, String propertyName, Integer propertyValue) {
050    if (propertyValue != null) {
051      jsonObject.add(propertyName, propertyValue);
052    }
053  }
054
055  /**
056   * Adds Long property to the json object if it's not null.
057   *
058   * @param jsonObject json object that the key/value will be added to.
059   * @param propertyName name of the property in json (key).
060   * @param propertyValue value of the property.
061   */
062  public static void addIfNotNull(JsonObject jsonObject, String propertyName, Long propertyValue) {
063    if (propertyValue != null) {
064      jsonObject.add(propertyName, propertyValue);
065    }
066  }
067
068  /**
069   * Adds Enum property to the json object if it's not null.
070   *
071   * @param jsonObject json object that the key/value will be added to.
072   * @param propertyName name of the property in json (key).
073   * @param propertyValue value of the property.
074   */
075  public static void addIfNotNull(JsonObject jsonObject, String propertyName, Enum propertyValue) {
076    if (propertyValue != null) {
077      jsonObject.add(propertyName, propertyValue.name().toLowerCase(java.util.Locale.ROOT));
078    }
079  }
080
081  /**
082   * Adds Date property to the json object if it's not null.
083   *
084   * @param jsonObject json object that the key/value will be added to.
085   * @param propertyName name of the property in json (key).
086   * @param propertyValue value of the property.
087   */
088  public static void addIfNotNull(JsonObject jsonObject, String propertyName, Date propertyValue) {
089    if (propertyValue != null) {
090      jsonObject.add(propertyName, BoxDateFormat.format(propertyValue));
091    }
092  }
093
094  /**
095   * Add JsonObject property to json object if it's not null.
096   *
097   * @param jsonObject json object that the key/value will be added to.
098   * @param propertyName name of the property in json (key).
099   * @param propertyValue value of the property.
100   */
101  public static void addIfNotNull(
102      JsonObject jsonObject, String propertyName, JsonObject propertyValue) {
103    if (propertyValue != null) {
104      jsonObject.add(propertyName, propertyValue);
105    }
106  }
107
108  /** Add double property to json object if it's not null. */
109  public static void addIfNotNull(
110      JsonObject jsonObject, String propertyName, Double propertyValue) {
111    if (propertyValue != null) {
112      jsonObject.add(propertyName, propertyValue);
113    }
114  }
115}