blob: 9b5eaf3c46cde0625eea6ada6b02f713abc2b1bd [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.lib.json;
2
3import java.io.*;
4import java.lang.reflect.*;
5import java.util.*;
6
7public class EnumHandler extends Handler {
8 @SuppressWarnings("rawtypes")
Stuart McCulloch2286f232012-06-15 13:27:53 +00009 final Class type;
Stuart McCullochbb014372012-06-07 21:57:32 +000010
Stuart McCulloch2286f232012-06-15 13:27:53 +000011 public EnumHandler(Class< ? > type) {
Stuart McCullochbb014372012-06-07 21:57:32 +000012 this.type = type;
13 }
14
Stuart McCulloch2286f232012-06-15 13:27:53 +000015 @Override
16 void encode(Encoder app, Object object, Map<Object,Type> visited) throws IOException, Exception {
Stuart McCullochbb014372012-06-07 21:57:32 +000017 StringHandler.string(app, object.toString());
18 }
19
Stuart McCullocha21b9e82012-08-02 13:26:25 +000020 @Override
Stuart McCulloch2286f232012-06-15 13:27:53 +000021 @SuppressWarnings("unchecked")
Stuart McCullocha21b9e82012-08-02 13:26:25 +000022 Object decode(Decoder dec, String s) throws Exception {
Stuart McCullochbb014372012-06-07 21:57:32 +000023 return Enum.valueOf(type, s);
24 }
25
26}