commoble.databuddy.codec

## Class MapCodecHelper

- java.lang.Object
- - commoble.databuddy.codec.MapCodecHelper

    public class MapCodecHelper
    extends java.lang.Object

Helper class for converting maps to NBT/json/etc and back using mojang's codec library. Creates a map codec by combining codecs for keys and values into a codec for a map of the keys and the values. Some examples of getting or creating codecs:

- Primitives and String are found in Codec:

      Codec.STRING

- Some vanilla objects that are frequently de/serialized have their own codecs:

      ResourceLocation.CODEC
      BlockState.CODEC

- See Drullkus's codec primer for information on building your own codecs

A very short summary on what codecs are and how they are used to de/serialize things:

- To serialize a THING to a DATA and back, you need a Codec<THING> and a DynamicOps<DATA>
- A Codec<THING> defines how to break down a complex THING down into primitive values and collections
- A DynamicOps<DATA> defines how to convert primitive values and collections to DATA and back
- Vanilla minecraft comes with two types of DynamicOps:
  - NBTDynamicOps.INSTANCE to serialize to INBT
  - JsonOps.INSTANCE to serialize to JsonElements
- An example of serializing a thing to NBT: `Optional<INBT> nbt = yourCodec.encodeStart(NBTDynamicOps.INSTANCE, yourThing).resultOrPartial(onError)`
  - This gives you an optional that either contains your data if the serialization was successful, or an empty if it wasn't, and allows you to log or crash if unsuccessful
- To deserialize a DATA back to a THING, use e.g. `Optional<THING> thing = yourCodec.parse(NBTDynamicOps.INSTANCE, someNBT).resultOrPartial(onError)`
- .result() can also be used instead of resultOrPartial if errors are ignorable

### Constructor Summary

Constructors
Constructor and Description
`MapCodecHelper()`

### Method Summary

All Methods Static Methods Concrete Methods
`protected static <KEY,VALUE> java.util.Map<KEY,VALUE>` `convertEntriesToMap(java.util.List<com.mojang.datafixers.util.Pair<KEY,VALUE>> list)`
`protected static <KEY,VALUE> java.util.List<com.mojang.datafixers.util.Pair<KEY,VALUE>>` `convertMapToEntries(java.util.Map<KEY,VALUE> map)`
`protected static <KEY,VALUE> com.mojang.serialization.Codec<com.mojang.datafixers.util.Pair<KEY,VALUE>>` `getSafePairCodec(com.mojang.serialization.Codec<KEY> keyCodec, com.mojang.serialization.Codec<VALUE> valueCodec)`
`static <KEY,VALUE> com.mojang.serialization.Codec<java.util.Map<KEY,VALUE>>` `makeEntryListCodec(com.mojang.serialization.Codec<KEY> keyCodec, com.mojang.serialization.Codec<VALUE> valueCodec)` Returns a codec for a map that represents the map as a list of key-value pairs.
`static <VALUE> com.mojang.serialization.Codec<java.util.Map<java.lang.String,VALUE>>` `makeStringKeyedCodec(com.mojang.serialization.Codec<VALUE> valueCodec)` Returns a codec for a map with string keys.
`static <KEY,VALUE> com.mojang.serialization.Codec<java.util.Map<KEY,VALUE>>` `makeStringKeyedCodec(java.util.function.Function<java.lang.String,KEY> fromString, java.util.function.Function<KEY,java.lang.String> toString, com.mojang.serialization.Codec<VALUE> valueCodec)` Returns a codec for a map with string-representable keys.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### MapCodecHelper

    public MapCodecHelper()

### Method Detail

#### makeStringKeyedCodec

    public static <VALUE> com.mojang.serialization.Codec<java.util.Map<java.lang.String,VALUE>> makeStringKeyedCodec(com.mojang.serialization.Codec<VALUE> valueCodec)

Returns a codec for a map with string keys. The default map codec seems to require that keys be serialized to strings and back. String-keyed maps have nicer representation in NBT and json, but aren't always viable. For keys that aren't strings and can't nicely be converted to strings, see makeEntryListCodec

Type Parameters:
  `VALUE` - The type of the values in the map's entries
Parameters:
  `valueCodec` - A codec for the type of the values
Returns:
  A codec for a map with keys of type String and values of the given type, serialized as a string-keyed map

#### makeStringKeyedCodec

    public static <KEY,VALUE> com.mojang.serialization.Codec<java.util.Map<KEY,VALUE>> makeStringKeyedCodec(java.util.function.Function<java.lang.String,KEY> fromString,
                                                                                                            java.util.function.Function<KEY,java.lang.String> toString,
                                                                                                            com.mojang.serialization.Codec<VALUE> valueCodec)

Returns a codec for a map with string-representable keys. The default map codec seems to require that keys be serialized to strings and back. String-keyed maps have nicer representation in NBT and json, but aren't always viable. For keys that aren't strings and can't nicely be converted to strings, see makeEntryListCodec.

If you already have a key codec that serializes an object to strings, use Codec.unboundedMap(keyCodec, valueCodec). Keep in mind that this will fail to serialize your map if your key codec does not serialize your keys to strings. ResourceLocation.CODEC is a good example of something that unboundedMap is safe to use for.

Type Parameters:
  `KEY` - type of the map's keys
  `VALUE` - type of the map's values
Parameters:
  `toString` - Function to convert keys to strings
  `fromString` - Function to convert strings back to keys
  `valueCodec` - A codec for the type of the values
Returns:
  A codec for a map whose keys and values are the given types, serialized as a string-keyed map

#### makeEntryListCodec

    public static <KEY,VALUE> com.mojang.serialization.Codec<java.util.Map<KEY,VALUE>> makeEntryListCodec(com.mojang.serialization.Codec<KEY> keyCodec,
                                                                                                          com.mojang.serialization.Codec<VALUE> valueCodec)

Returns a codec for a map that represents the map as a list of key-value pairs. Keys that cannot or should not be serialized as strings must be serialized in this manner instead.

Type Parameters:
  `KEY` - The type of the keys in the map
  `VALUE` - The type of the values in the map
Parameters:
  `keyCodec` - A codec for the type of the map's keys
  `valueCodec` - A codec for the type of the map's values
Returns:
  A codec for a map whose keys and values are the given types, serialized as a list of key-value pairs

#### getSafePairCodec

    protected static <KEY,VALUE> com.mojang.serialization.Codec<com.mojang.datafixers.util.Pair<KEY,VALUE>> getSafePairCodec(com.mojang.serialization.Codec<KEY> keyCodec,
                                                                                                                             com.mojang.serialization.Codec<VALUE> valueCodec)

#### convertEntriesToMap

    protected static <KEY,VALUE> java.util.Map<KEY,VALUE> convertEntriesToMap(java.util.List<com.mojang.datafixers.util.Pair<KEY,VALUE>> list)

#### convertMapToEntries

    protected static <KEY,VALUE> java.util.List<com.mojang.datafixers.util.Pair<KEY,VALUE>> convertMapToEntries(java.util.Map<KEY,VALUE> map)