EnumTypeAdapterFactory .java

net.minecraft.util.EnumTypeAdapterFactory (class): members: create. imports: TypeAdapterFactory, TypeAdapter, JsonReader, JsonWriter, TypeToken, JsonToken.

net.minecraft.utilclass68 linescommon2.0 KB

Implements

API Surface (3)

<T> TypeAdapter<T> create(Gson p_create_1_, TypeToken<T> p_create_2_)
void write(JsonWriter p_write_1_, T p_write_2_)
T read(JsonReader p_read_1_)

Code Graph: Dependencies (1)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

EnumTypeAdapterFactory.java68 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.util;
2
3
import com.google.common.collect.Maps;
4
import com.google.gson.Gson;
5
import com.google.gson.TypeAdapter;
6
import com.google.gson.TypeAdapterFactory;
7
import com.google.gson.reflect.TypeToken;
8
import com.google.gson.stream.JsonReader;
9
import com.google.gson.stream.JsonToken;
10
import com.google.gson.stream.JsonWriter;
11
import java.io.IOException;
12
import java.util.Locale;
13
import java.util.Map;
14
15
public class EnumTypeAdapterFactory implements TypeAdapterFactory
16
{
17
    public <T> TypeAdapter<T> create(Gson p_create_1_, TypeToken<T> p_create_2_)
18
    {
19
        Class<T> oclass = (Class<T>)p_create_2_.getRawType();
20
21
        if (!oclass.isEnum())
22
        {
23
            return null;
24
        }
25
        else
26
        {
27
            final Map<String, T> map = Maps.<String, T>newHashMap();
28
29
            for (T t : oclass.getEnumConstants())
30
            {
31
                map.put(this.func_151232_a(t), t);
32
            }
33
34
            return new TypeAdapter<T>()
35
            {
36
                public void write(JsonWriter p_write_1_, T p_write_2_) throws IOException
37
                {
38
                    if (p_write_2_ == null)
39
                    {
40
                        p_write_1_.nullValue();
41
                    }
42
                    else
43
                    {
44
                        p_write_1_.value(EnumTypeAdapterFactory.this.func_151232_a(p_write_2_));
45
                    }
46
                }
47
                public T read(JsonReader p_read_1_) throws IOException
48
                {
49
                    if (p_read_1_.peek() == JsonToken.NULL)
50
                    {
51
                        p_read_1_.nextNull();
52
                        return (T)null;
53
                    }
54
                    else
55
                    {
56
                        return (T)map.get(p_read_1_.nextString());
57
                    }
58
                }
59
            };
60
        }
61
    }
62
63
    private String func_151232_a(Object p_151232_1_)
64
    {
65
        return p_151232_1_ instanceof Enum ? ((Enum)p_151232_1_).name().toLowerCase(Locale.US) : p_151232_1_.toString().toLowerCase(Locale.US);
66
    }
67
}
68

Deobfuscated MCP 1.8.9 source of EnumTypeAdapterFactory from Marcelektro/MavenMCP-1.8.9 (net/minecraft/util/EnumTypeAdapterFactory.java), stored verbatim.