Class JsonBuilder

java.lang.Object
dev.demeng.pluginbase.gson.JsonBuilder

public final class JsonBuilder extends Object
Builder utilities for creating GSON Objects/Arrays.
  • Method Details

    • object

      public static JsonBuilder.JsonObjectBuilder object(com.google.gson.JsonObject object, boolean copy)
      Creates a new object builder

      If copy is not true, the passed object will be mutated by the builder methods.

      Parameters:
      object - the object to base the new builder upon
      copy - if the object should be deep copied, or just referenced.
      Returns:
      a new builder
    • object

      public static JsonBuilder.JsonObjectBuilder object(com.google.gson.JsonObject object)
      Creates a new object builder, without copying the passed object.

      Equivalent to calling object(JsonObject, boolean) with copy = false.

      Parameters:
      object - the object to base the new builder upon
      Returns:
      a new builder
    • object

      public static JsonBuilder.JsonObjectBuilder object()
      Creates a new object builder, with no initial values
      Returns:
      a new builder
    • array

      public static JsonBuilder.JsonArrayBuilder array(com.google.gson.JsonArray array, boolean copy)
      Creates a new array builder

      If copy is not true, the passed array will be mutated by the builder methods.

      Parameters:
      array - the array to base the new builder upon
      copy - if the array should be deep copied, or just referenced.
      Returns:
      a new builder
    • array

      public static JsonBuilder.JsonArrayBuilder array(com.google.gson.JsonArray array)
      Creates a new array builder, without copying the passed array.

      Equivalent to calling array(JsonArray, boolean) with copy = false.

      Parameters:
      array - the array to base the new builder upon
      Returns:
      a new builder
    • array

      public static JsonBuilder.JsonArrayBuilder array()
      Creates a new array builder, with no initial values
      Returns:
      a new builder
    • primitive

      public static com.google.gson.JsonElement primitive(@Nullable String value)
      Creates a JsonPrimitive from the given value.

      If the value is null, nullValue() is returned.

      Parameters:
      value - the value
      Returns:
      a json primitive for the value
    • primitive

      public static com.google.gson.JsonElement primitive(@Nullable Number value)
      Creates a JsonPrimitive from the given value.

      If the value is null, nullValue() is returned.

      Parameters:
      value - the value
      Returns:
      a json primitive for the value
    • primitive

      public static com.google.gson.JsonElement primitive(@Nullable Boolean value)
      Creates a JsonPrimitive from the given value.

      If the value is null, nullValue() is returned.

      Parameters:
      value - the value
      Returns:
      a json primitive for the value
    • primitive

      public static com.google.gson.JsonElement primitive(@Nullable Character value)
      Creates a JsonPrimitive from the given value.

      If the value is null, nullValue() is returned.

      Parameters:
      value - the value
      Returns:
      a json primitive for the value
    • nullValue

      public static com.google.gson.JsonNull nullValue()
      Returns an instance of JsonNull.
      Returns:
      a json null instance
    • primitiveNonNull

      public static com.google.gson.JsonPrimitive primitiveNonNull(String value)
      Creates a JsonPrimitive from the given value.

      If the value is null, a NullPointerException will be thrown.

      Parameters:
      value - the value
      Returns:
      a json primitive for the value
      Throws:
      NullPointerException - if value is null
    • primitiveNonNull

      public static com.google.gson.JsonPrimitive primitiveNonNull(Number value)
      Creates a JsonPrimitive from the given value.

      If the value is null, a NullPointerException will be thrown.

      Parameters:
      value - the value
      Returns:
      a json primitive for the value
      Throws:
      NullPointerException - if value is null
    • primitiveNonNull

      public static com.google.gson.JsonPrimitive primitiveNonNull(Boolean value)
      Creates a JsonPrimitive from the given value.

      If the value is null, a NullPointerException will be thrown.

      Parameters:
      value - the value
      Returns:
      a json primitive for the value
      Throws:
      NullPointerException - if value is null
    • primitiveNonNull

      public static com.google.gson.JsonPrimitive primitiveNonNull(Character value)
      Creates a JsonPrimitive from the given value.

      If the value is null, a NullPointerException will be thrown.

      Parameters:
      value - the value
      Returns:
      a json primitive for the value
      Throws:
      NullPointerException - if value is null
    • collectToObject

      public static <T> Collector<T,JsonBuilder.JsonObjectBuilder,com.google.gson.JsonObject> collectToObject(Function<? super T,String> keyMapper, Function<? super T,com.google.gson.JsonElement> valueMapper)
      Returns a collector which forms a JsonObject using the key and value mappers
      Type Parameters:
      T - the type
      Parameters:
      keyMapper - the function to map from T to String
      valueMapper - the function to map from T to JsonElement
      Returns:
      a new collector
    • collectToArray

      public static <T> Collector<T,JsonBuilder.JsonArrayBuilder,com.google.gson.JsonArray> collectToArray(Function<? super T,com.google.gson.JsonElement> valueMapper)
      Returns a collector which forms a JsonArray using the value mapper
      Type Parameters:
      T - the type
      Parameters:
      valueMapper - the function to map from T to JsonElement
      Returns:
      a new collector
    • collectToArray

      public static Collector<com.google.gson.JsonElement,JsonBuilder.JsonArrayBuilder,com.google.gson.JsonArray> collectToArray()
      Returns a collector which forms a JsonArray from JsonElements
      Returns:
      a new collector