001/*
002 * Copyright (c) 2010-2024 Mark Allen, Norbert Bartels.
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.restfb.util;
023
024/**
025 * Helper class to encapsulate simple checks used in the DefaultJsonMapper.
026 */
027public class StringJsonUtils {
028
029  private StringJsonUtils() {
030    throw new IllegalStateException("StringJsonUtils must not be instantiated");
031  }
032
033  public static final String EMPTY_OBJECT = "{}";
034
035  /**
036   * Is the given JSON equivalent to the empty list (<code>[]</code>)?
037   *
038   * @param jsonString
039   *          The JSON to check.
040   * @return {@code true} if the JSON is equivalent to the empty list, {@code false} otherwise.
041   */
042  public static boolean isEmptyList(String jsonString) {
043    return "[]".equals(jsonString);
044  }
045
046  /**
047   * Checks if the given String start with a <code>[</code> character, so it may be a JsonArray
048   *
049   * @param jsonString
050   *          the JSON to check.
051   * @return {@code true} if the String may be a JSON Array
052   */
053  public static boolean isList(String jsonString) {
054    return jsonString != null && jsonString.startsWith("[");
055  }
056
057  /**
058   * Checks if the given String is equals to the String with the content {@code "null"}
059   * 
060   * @param jsonString
061   *          the JSON to check.
062   * @return {@code true} if the String is {@code "null"}
063   */
064  public static boolean isNull(String jsonString) {
065    return "null".equals(jsonString);
066  }
067
068  /**
069   * Checks if the given String is equals to the String with the content {@code "false"}
070   * 
071   * @param jsonString
072   *          the JSON to check.
073   * @return {@code true} if the String is {@code "false"}
074   */
075  public static boolean isFalse(String jsonString) {
076    return "false".equals(jsonString);
077  }
078
079  /**
080   * Checks if the given String start with a <code>{</code> character, so it may be a JsonObject
081   *
082   * @param jsonString
083   *          the JSON to check.
084   * @return {@code true} if the String may be a JSON object
085   */
086  public static boolean isObject(String jsonString) {
087    return jsonString != null && jsonString.startsWith("{");
088  }
089
090  /**
091   * Is the given JSON equivalent to the empty object (<code>{}</code>)?
092   *
093   * @param jsonString
094   *          The JSON to check.
095   * @return {@code true} if the JSON is equivalent to the empty object, {@code false} otherwise.
096   */
097  public static boolean isEmptyObject(String jsonString) {
098    return EMPTY_OBJECT.equals(jsonString);
099  }
100}