Interface ISerializer<T>

Type Parameters:
T - The data type that is handled by the serializer.
All Known Implementing Classes:
SerializerAttributeModifier, SerializerBlockPos, SerializerBlockState, SerializerBoolean, SerializerByte, SerializerCodec, SerializerCompoundTag, SerializerDouble, SerializerEffectInstance, SerializerEnchantmentInstance, SerializerEnum, SerializerFloat, SerializerIngredient, SerializerInteger, SerializerItemStack, SerializerLong, SerializerRegistryEntry, SerializerResourceLocation, SerializerShort, SerializerString, SerializerTagKey, SerializerText, SerializerUUID, SerializerVector3f, SerializerVector4f, Sound.Serializer

public interface ISerializer<T>
Provides a structure for serializing common data types into the various data exchange formats used in the game. These currently support JSON, NBT, and byte buffers (networking).
  • Method Summary

    Modifier and Type
    Method
    Description
    fromByteBuf(net.minecraft.network.FriendlyByteBuf buffer)
    Reads a value from a byte buffer.
    default List<T>
    fromByteBufList(net.minecraft.network.FriendlyByteBuf buffer)
    Reads a list of values from a byte buffer.
    default T
    fromByteBufNullable(net.minecraft.network.FriendlyByteBuf buffer)
     
    default Optional<T>
    fromByteBufOptional(net.minecraft.network.FriendlyByteBuf buffer)
    Read an optional value from a byte buffer.
    default net.minecraft.util.random.WeightedEntry.Wrapper<T>
    fromByteBufWeighted(net.minecraft.network.FriendlyByteBuf buffer)
    Reads a weighted wrapped value from a byte buffer.
    default net.minecraft.util.random.SimpleWeightedRandomList<T>
    fromByteBufWeightedList(net.minecraft.network.FriendlyByteBuf buffer)
    Reads a list of weighted wrapped entries from a byte buffer.
    fromJSON(com.google.gson.JsonElement json)
    Reads the value from a specific JSON element.
    default T
    fromJSON(com.google.gson.JsonObject json, String memberName)
    Reads a value from a member on a parent JSON object.
    default T
    fromJSON(com.google.gson.JsonObject json, String memberName, Supplier<T> fallback)
    Reads a value from a member on a parent JSON object.
    default T
    fromJSON(com.google.gson.JsonObject json, String memberName, T fallback)
    Reads a value from a member on a parent JSON object.
    default List<T>
    fromJSONList(com.google.gson.JsonElement json)
    Reads a list of values from JSON.
    default List<T>
    fromJSONList(com.google.gson.JsonObject json, String memberName)
    Reads a list of values from a child element on a JSON object.
    default List<T>
    fromJSONList(com.google.gson.JsonObject json, String memberName, Supplier<List<T>> fallback)
    Reads a list of values from a child element on a JSON object.
    default List<T>
    fromJSONList(com.google.gson.JsonObject json, String memberName, List<T> fallback)
    Reads a list of values from a child element on a JSON object.
    default T
    fromJSONNullable(com.google.gson.JsonElement json)
     
    default T
    fromJSONNullable(com.google.gson.JsonObject json, String memberName)
     
    default Optional<T>
    fromJSONOptional(com.google.gson.JsonElement json)
    Reads an optional value from a JSON element.
    default Optional<T>
    fromJSONOptional(com.google.gson.JsonObject json, String memberName)
    Read an optional value from a child JSON element.
    default Set<T>
    fromJSONSet(com.google.gson.JsonElement json)
    Reads a set of values from a JSON element.
    default Set<T>
    fromJSONSet(com.google.gson.JsonObject json, String memberName)
    Reads a set of values from a child member from the parent JSON element.
    default Set<T>
    fromJSONSet(com.google.gson.JsonObject json, String memberName, Supplier<Set<T>> fallback)
    Reads a set of values from a child member from the parent JSON element.
    default Set<T>
    fromJSONSet(com.google.gson.JsonObject json, String memberName, Set<T> fallback)
    Reads a set of values from a child member from the parent JSON element.
    default T
    fromJSONString(String jsonString)
    Reads the value from a JSON string.
    default net.minecraft.util.random.WeightedEntry.Wrapper<T>
    fromJSONWeighted(com.google.gson.JsonElement element)
    Reads a wrapped weighted entry from a JSON element.
    default net.minecraft.util.random.SimpleWeightedRandomList<T>
    fromJSONWeightedList(com.google.gson.JsonElement json)
    Reads a list of weighted wrapped values from the json.
    default T
    fromNBT(net.minecraft.nbt.CompoundTag tag, String name)
     
    default T
    fromNBT(net.minecraft.nbt.CompoundTag tag, String name, T fallback)
     
    fromNBT(net.minecraft.nbt.Tag nbt)
    Reads a value from a named binary tag (NBT).
    default List<T>
    fromNBTList(net.minecraft.nbt.CompoundTag tag, String name)
     
    default List<T>
    fromNBTList(net.minecraft.nbt.Tag toRead)
     
    default Set<T>
    readByteBufSet(net.minecraft.network.FriendlyByteBuf buffer)
    Reads a set of values from a byte buffer.
    void
    toByteBuf(net.minecraft.network.FriendlyByteBuf buffer, T toWrite)
    Writes a value to a byte buffer.
    default void
    toByteBufList(net.minecraft.network.FriendlyByteBuf buffer, List<T> toWrite)
    Writes a list of values to a byte buffer.
    default void
    toByteBufNullable(net.minecraft.network.FriendlyByteBuf buffer, T value)
     
    default void
    toByteBufOptional(net.minecraft.network.FriendlyByteBuf buffer, Optional<T> optional)
    Writes an optional value to the byte buffer.
    default void
    toByteBufWeighted(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.WeightedEntry.Wrapper<T> toWrite)
    Writes a weighted wrapped value to a byte buffer.
    default void
    toByteBufWeightedList(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.SimpleWeightedRandomList<T> list)
    Writes a list of weighted wrapped entries to a byte buffer.
    default void
    toJSON(com.google.gson.JsonObject json, String name, T toWrite)
     
    com.google.gson.JsonElement
    toJSON(T toWrite)
    Writes a value to a JSON element.
    default void
    toJSONList(com.google.gson.JsonObject json, String memberName, List<T> toWrite)
    Writes a list of values to a JSON object, if the list is null or empty it will be ignored.
    default com.google.gson.JsonElement
    toJSONList(List<T> toWrite)
    Writes a list of values to a JSON element.
    default void
    toJSONNullable(com.google.gson.JsonObject json, String memberName, T value)
     
    default com.google.gson.JsonElement
     
    default void
    toJSONOptional(com.google.gson.JsonObject json, String memberName, Optional<T> value)
    Write an optional value to a JSON object.
    default com.google.gson.JsonElement
    Writes an optional value to a JSON element.
    default com.google.gson.JsonElement
    Writes an optional value to a JSON element.
    default com.google.gson.JsonElement
    toJSONSet(Set<T> toWrite)
    Writes a set of values to a JSON element.
    default String
    toJSONString(T toWrite)
    Writes the value to a string representation of a JSON element.
    default com.google.gson.JsonElement
    toJSONWeighted(net.minecraft.util.random.WeightedEntry.Wrapper<T> weightedEntry)
    Writes a weighted entry to a JSON element.
    default com.google.gson.JsonElement
    toJSONWeightedList(net.minecraft.util.random.SimpleWeightedRandomList<T> list)
    Writes a list of weighted wrapped entries to JSON.
    default void
    toNBT(net.minecraft.nbt.CompoundTag tag, String name, T toWrite)
     
    net.minecraft.nbt.Tag
    toNBT(T toWrite)
    Writes a value to a named binary tag (NBT).
    default net.minecraft.nbt.ListTag
    toNBTList(List<T> toWrite)
     
    default void
    toNBTList(net.minecraft.nbt.CompoundTag tag, String name, List<T> toWrite)
     
    default void
    writeByteBufSet(net.minecraft.network.FriendlyByteBuf buffer, Set<T> toWrite)
    Writes a set of values from a byte buffer.
  • Method Details

    • fromJSON

      T fromJSON(com.google.gson.JsonElement json)
      Reads the value from a specific JSON element.
      Parameters:
      json - The JSON element to read data from.
      Returns:
      The value that was read.
    • fromJSON

      default T fromJSON(com.google.gson.JsonObject json, String memberName)
      Reads a value from a member on a parent JSON object.
      Parameters:
      json - The parent JSON object to read from.
      memberName - The name of the child element to read from.
      Returns:
      The value that was read.
    • fromJSON

      default T fromJSON(com.google.gson.JsonObject json, String memberName, T fallback)
      Reads a value from a member on a parent JSON object.
      Parameters:
      json - The parent JSON object to read from.
      memberName - The name of the child element to read from.
      fallback - A fallback value used when the child element does not exist.
      Returns:
      The value that was read or the fallback value.
    • fromJSON

      default T fromJSON(com.google.gson.JsonObject json, String memberName, Supplier<T> fallback)
      Reads a value from a member on a parent JSON object.
      Parameters:
      json - The parent JSON object to read from.
      memberName - The name of the child element to read from.
      fallback - A fallback value used when the child element does not exist.
      Returns:
      The value that was read or the fallback value.
    • toJSON

      default void toJSON(com.google.gson.JsonObject json, String name, T toWrite)
    • toJSON

      com.google.gson.JsonElement toJSON(T toWrite)
      Writes a value to a JSON element.
      Parameters:
      toWrite - The object to serialize into JSON data.
      Returns:
      The serialized JSON element.
    • fromByteBuf

      T fromByteBuf(net.minecraft.network.FriendlyByteBuf buffer)
      Reads a value from a byte buffer. This is commonly used for networking.
      Parameters:
      buffer - The byte buffer to read from.
      Returns:
      The value that was read.
    • toByteBuf

      void toByteBuf(net.minecraft.network.FriendlyByteBuf buffer, T toWrite)
      Writes a value to a byte buffer. This is commonly used for networking.
      Parameters:
      buffer - The byte buffer to write to.
      toWrite - The value to write.
    • toNBT

      net.minecraft.nbt.Tag toNBT(T toWrite)
      Writes a value to a named binary tag (NBT).
      Parameters:
      toWrite - The value to write.
      Returns:
      The serialized NBT data.
    • fromNBT

      T fromNBT(net.minecraft.nbt.Tag nbt)
      Reads a value from a named binary tag (NBT).
      Parameters:
      nbt - The tag to read data from.
      Returns:
      The value that was read from the data.
    • fromNBT

      default T fromNBT(net.minecraft.nbt.CompoundTag tag, String name)
    • fromNBT

      default T fromNBT(@Nullable net.minecraft.nbt.CompoundTag tag, String name, T fallback)
    • toNBT

      default void toNBT(net.minecraft.nbt.CompoundTag tag, String name, T toWrite)
    • toNBTList

      default void toNBTList(@Nullable net.minecraft.nbt.CompoundTag tag, String name, @Nullable List<T> toWrite)
    • toNBTList

      default net.minecraft.nbt.ListTag toNBTList(List<T> toWrite)
    • fromNBTList

      default List<T> fromNBTList(net.minecraft.nbt.CompoundTag tag, String name)
    • fromNBTList

      default List<T> fromNBTList(net.minecraft.nbt.Tag toRead)
    • fromJSONList

      default List<T> fromJSONList(com.google.gson.JsonElement json)
      Reads a list of values from JSON. When given a JSON array each entry is added to the list. When given a JSON object it will be treated as a list of one element.
      Parameters:
      json - The JSON element to read from.
      Returns:
      A list of values read from the JSON.
    • fromJSONList

      default List<T> fromJSONList(com.google.gson.JsonObject json, String memberName)
      Reads a list of values from a child element on a JSON object. If the child element is an array each entry will be added to the list. If the child element is an object it will be treated as a list of one.
      Parameters:
      json - The parent JSON element.
      memberName - The member of the element to read from.
      Returns:
      The list of values read from the JSON.
    • fromJSONList

      default List<T> fromJSONList(com.google.gson.JsonObject json, String memberName, List<T> fallback)
      Reads a list of values from a child element on a JSON object. If the child element is an array each entry will be added to the list. If the child element is an object it will be treated as a list of one.
      Parameters:
      json - The parent JSON element.
      memberName - The member of the element to read from.
      fallback - A list of fallback values to use when the child member is not present.
      Returns:
      The list of values read from the JSON.
    • fromJSONList

      default List<T> fromJSONList(com.google.gson.JsonObject json, String memberName, Supplier<List<T>> fallback)
      Reads a list of values from a child element on a JSON object. If the child element is an array each entry will be added to the list. If the child element is an object it will be treated as a list of one.
      Parameters:
      json - The parent JSON element.
      memberName - The member of the element to read from.
      fallback - A list of fallback values to use when the child member is not present.
      Returns:
      The list of values read from the JSON.
    • toJSONList

      default com.google.gson.JsonElement toJSONList(List<T> toWrite)
      Writes a list of values to a JSON element.
      Parameters:
      toWrite - The values to write.
      Returns:
      The serialized JSON element.
    • toJSONList

      default void toJSONList(com.google.gson.JsonObject json, String memberName, @Nullable List<T> toWrite)
      Writes a list of values to a JSON object, if the list is null or empty it will be ignored.
      Parameters:
      json - The JSON object to add the list to.
      memberName - The name to assign the list member.
      toWrite - The list to write.
    • fromByteBufList

      default List<T> fromByteBufList(net.minecraft.network.FriendlyByteBuf buffer)
      Reads a list of values from a byte buffer.
      Parameters:
      buffer - The buffer to read data from.
      Returns:
      The list of values read from the buffer.
    • toByteBufList

      default void toByteBufList(net.minecraft.network.FriendlyByteBuf buffer, List<T> toWrite)
      Writes a list of values to a byte buffer.
      Parameters:
      buffer - The buffer to write to.
      toWrite - The value to write.
    • fromJSONSet

      default Set<T> fromJSONSet(com.google.gson.JsonElement json)
      Reads a set of values from a JSON element. A JSON array will have each element added to the set. A JSON object will be treated as a set of one element.
      Parameters:
      json - The JSON element to read from.
      Returns:
      The set of values that were read.
    • fromJSONSet

      default Set<T> fromJSONSet(com.google.gson.JsonObject json, String memberName)
      Reads a set of values from a child member from the parent JSON element.
      Parameters:
      json - The parent JSON element.
      memberName - The name of the child member.
      Returns:
      The set of values that were read.
    • fromJSONSet

      default Set<T> fromJSONSet(com.google.gson.JsonObject json, String memberName, Set<T> fallback)
      Reads a set of values from a child member from the parent JSON element. If the member does not exist the fallback set will be used.
      Parameters:
      json - The parent JSON element.
      memberName - The name of the child member.
      fallback - The fallback set to use when the member is not present.
      Returns:
      The set of values that were read.
    • fromJSONSet

      default Set<T> fromJSONSet(com.google.gson.JsonObject json, String memberName, Supplier<Set<T>> fallback)
      Reads a set of values from a child member from the parent JSON element. If the member does not exist the fallback set will be used.
      Parameters:
      json - The parent JSON element.
      memberName - The name of the child member.
      fallback - The fallback set to use when the member is not present.
      Returns:
      The set of values that were read.
    • toJSONSet

      default com.google.gson.JsonElement toJSONSet(Set<T> toWrite)
      Writes a set of values to a JSON element.
      Parameters:
      toWrite - The set of values to write.
      Returns:
      The JSON value that was written.
    • readByteBufSet

      default Set<T> readByteBufSet(net.minecraft.network.FriendlyByteBuf buffer)
      Reads a set of values from a byte buffer.
      Parameters:
      buffer - The byte buffer to read from.
      Returns:
      The set of values that were read from the buffer.
    • writeByteBufSet

      default void writeByteBufSet(net.minecraft.network.FriendlyByteBuf buffer, Set<T> toWrite)
      Writes a set of values from a byte buffer.
      Parameters:
      buffer - The byte buffer to write to.
      toWrite - The set of values to write.
    • fromJSONOptional

      default Optional<T> fromJSONOptional(com.google.gson.JsonObject json, String memberName)
      Read an optional value from a child JSON element. If the child JSON member does not exist an empty optional will be used.
      Parameters:
      json - The parent JSON element to read from.
      memberName - The name of the child member to read.
      Returns:
      An optional containing the value that was read.
    • fromJSONOptional

      default Optional<T> fromJSONOptional(@Nullable com.google.gson.JsonElement json)
      Reads an optional value from a JSON element. If the element is null an empty optional will be returned.
      Parameters:
      json - The JSON to read data from.
      Returns:
      An optional containing the value that was read.
    • toJSONOptional

      @Nullable default com.google.gson.JsonElement toJSONOptional(@Nullable T value)
      Writes an optional value to a JSON element. If the value is not present a null value will be returned.
      Parameters:
      value - The optional value to write.
      Returns:
      The written JSON element. If the optional value was not present this will be null.
    • toJSONOptional

      @Nullable default com.google.gson.JsonElement toJSONOptional(Optional<T> value)
      Writes an optional value to a JSON element. If the value is not present a null value will be returned.
      Parameters:
      value - The optional value to write.
      Returns:
      The written JSON element. If the optional value was not present this will be null.
    • toJSONOptional

      default void toJSONOptional(com.google.gson.JsonObject json, String memberName, Optional<T> value)
      Write an optional value to a JSON object.
      Parameters:
      json - The JSON object to write to.
      memberName - The name of the member to write the value to.
      value - The value to write.
    • fromByteBufOptional

      default Optional<T> fromByteBufOptional(net.minecraft.network.FriendlyByteBuf buffer)
      Read an optional value from a byte buffer.
      Parameters:
      buffer - The buffer to read from.
      Returns:
      An optional containing the value that was read.
    • toByteBufOptional

      default void toByteBufOptional(net.minecraft.network.FriendlyByteBuf buffer, Optional<T> optional)
      Writes an optional value to the byte buffer.
      Parameters:
      buffer - The buffer to write to.
      optional - The optional value to write.
    • toJSONString

      default String toJSONString(T toWrite)
      Writes the value to a string representation of a JSON element.
      Parameters:
      toWrite - The value to write.
      Returns:
      A string representation of the element when written to JSON.
    • fromJSONString

      default T fromJSONString(String jsonString)
      Reads the value from a JSON string.
      Parameters:
      jsonString - A string representation of the JSON element to read data from.
      Returns:
      The value read from the JSON data.
    • fromJSONWeighted

      default net.minecraft.util.random.WeightedEntry.Wrapper<T> fromJSONWeighted(com.google.gson.JsonElement element)
      Reads a wrapped weighted entry from a JSON element. The weight is taken from a JSON Object property named "weight". If the JSON is not an Object or the property does not exist a weight of 0 will be used.
      Parameters:
      element - The JSON element to read from.
      Returns:
      The weighted wrapper.
    • toJSONWeighted

      default com.google.gson.JsonElement toJSONWeighted(net.minecraft.util.random.WeightedEntry.Wrapper<T> weightedEntry)
      Writes a weighted entry to a JSON element. The value and weight are stored as separate properties.
      Parameters:
      weightedEntry - The weighted entry to write.
      Returns:
      The serialized JSON.
    • toByteBufWeighted

      default void toByteBufWeighted(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.WeightedEntry.Wrapper<T> toWrite)
      Writes a weighted wrapped value to a byte buffer.
      Parameters:
      buffer - The buffer to write to.
      toWrite - The data to write.
    • fromByteBufWeighted

      default net.minecraft.util.random.WeightedEntry.Wrapper<T> fromByteBufWeighted(net.minecraft.network.FriendlyByteBuf buffer)
      Reads a weighted wrapped value from a byte buffer.
      Parameters:
      buffer - The buffer to read from.
      Returns:
      The value that was read from the buffer.
    • fromJSONWeightedList

      default net.minecraft.util.random.SimpleWeightedRandomList<T> fromJSONWeightedList(com.google.gson.JsonElement json)
      Reads a list of weighted wrapped values from the json.
      Parameters:
      json - The JSON data to read from.
      Returns:
      A list of weighted wrapped values.
    • toJSONWeightedList

      default com.google.gson.JsonElement toJSONWeightedList(net.minecraft.util.random.SimpleWeightedRandomList<T> list)
      Writes a list of weighted wrapped entries to JSON.
      Parameters:
      list - The list to write.
      Returns:
      The JSON output.
    • fromByteBufWeightedList

      default net.minecraft.util.random.SimpleWeightedRandomList<T> fromByteBufWeightedList(net.minecraft.network.FriendlyByteBuf buffer)
      Reads a list of weighted wrapped entries from a byte buffer.
      Parameters:
      buffer - The buffer to read from.
      Returns:
      The list of weighted wrapped entries.
    • toByteBufWeightedList

      default void toByteBufWeightedList(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.SimpleWeightedRandomList<T> list)
      Writes a list of weighted wrapped entries to a byte buffer.
      Parameters:
      buffer - The buffer to write to.
      list - The list of entries to write.
    • fromJSONNullable

      @Nullable default T fromJSONNullable(com.google.gson.JsonObject json, String memberName)
    • fromJSONNullable

      @Nullable default T fromJSONNullable(@Nullable com.google.gson.JsonElement json)
    • toJSONNullable

      @Nullable default com.google.gson.JsonElement toJSONNullable(@Nullable T value)
    • toJSONNullable

      default void toJSONNullable(@Nullable com.google.gson.JsonObject json, String memberName, @Nullable T value)
    • fromByteBufNullable

      @Nullable default T fromByteBufNullable(net.minecraft.network.FriendlyByteBuf buffer)
    • toByteBufNullable

      default void toByteBufNullable(net.minecraft.network.FriendlyByteBuf buffer, @Nullable T value)