
com.dslplatform.json.runtime.EnumDescription Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsl-json-java8 Show documentation
Show all versions of dsl-json-java8 Show documentation
DSL Platform compatible Java JSON library (https://dsl-platform.com)
package com.dslplatform.json.runtime;
import com.dslplatform.json.JsonReader;
import com.dslplatform.json.JsonWriter;
import java.io.IOException;
import java.util.Map;
public final class EnumDescription> implements JsonWriter.WriteObject, JsonReader.ReadObject {
private final Class manifest;
private final ReadPropertyInfo[] readers;
public EnumDescription(
final Class manifest,
final Map values) {
if (manifest == null) throw new IllegalArgumentException("manifest can't be null");
if (values == null) throw new IllegalArgumentException("values can't be null");
this.manifest = manifest;
final ReadPropertyInfo[] tmp = new ReadPropertyInfo[values.size()];
int i = 0;
for (String name : values.keySet()) {
tmp[i++] = new ReadPropertyInfo<>(name, false, values.get(name));
}
this.readers = ReadPropertyInfo.prepareReaders(tmp);
}
@Override
public void write(final JsonWriter writer, final T value) {
if (value == null) writer.writeNull();
else writer.writeString(value.name());
}
@Override
public T read(final JsonReader reader) throws IOException {
if (reader.wasNull()) return null;
final int hash = reader.calcHash();
for (final ReadPropertyInfo ri : readers) {
if (hash == ri.hash) {
if (ri.exactName && !reader.wasLastName(ri.name)) continue;
return ri.value;
}
}
return Enum.valueOf(manifest, reader.getLastName());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy