Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed the parsing of the uppercase enums and added some extra tests.
- Loading branch information
Showing
4 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/net/bramp/ffmpeg/gson/LowercaseEnumTypeAdapterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package net.bramp.ffmpeg.gson; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.TypeAdapterFactory; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
/** | ||
* Taken from https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/TypeAdapterFactory.html | ||
*/ | ||
public class LowercaseEnumTypeAdapterFactory implements TypeAdapterFactory { | ||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
Class<T> rawType = (Class<T>) type.getRawType(); | ||
if (!rawType.isEnum()) { | ||
return null; | ||
} | ||
|
||
// Setup mapping of consts | ||
final Map<String, T> lowercaseToConstant = new HashMap<String, T>(); | ||
for (T constant : rawType.getEnumConstants()) { | ||
lowercaseToConstant.put(toLowercase(constant), constant); | ||
} | ||
|
||
return new TypeAdapter<T>() { | ||
public void write(JsonWriter out, T value) throws IOException { | ||
if (value == null) { | ||
out.nullValue(); | ||
} else { | ||
out.value(toLowercase(value)); | ||
} | ||
} | ||
|
||
public T read(JsonReader reader) throws IOException { | ||
if (reader.peek() == JsonToken.NULL) { | ||
reader.nextNull(); | ||
return null; | ||
} | ||
return lowercaseToConstant.get(reader.nextString()); | ||
} | ||
}; | ||
} | ||
|
||
private String toLowercase(Object o) { | ||
return o.toString().toLowerCase(Locale.UK); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters