This might not be practical, but I have it as a task. I have an opened ServerSocket in java. Now I want to read a file which contains html and javascript, and output the jav
public static String convertEnumToJson(Class extends Enum>> cls) throws ScriptException, NoSuchMethodException, IllegalAccessException { List> map = createMap(Colors.class); return createJson(map); } public static List> createMap(Class extends Enum>> cls) throws IllegalAccessException { List> res = new ArrayList<>(); for (Enum> item : cls.getEnumConstants()) { Map map = new LinkedHashMap<>(); for (Field field : item.getClass().getDeclaredFields()) { if (field.getType() != item.getClass() && !"$VALUES".equals(field.getName())) { field.setAccessible(true); map.put(field.getName(), field.get(item)); } } res.add(map); } return res; } // It's better to use Jackson or similar lib for read/write json public static String createJson(T obj) throws ScriptException, NoSuchMethodException { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("JavaScript"); engine.eval("function sum(a){ return a;}"); return ((Invocable)engine).invokeFunction("sum", obj).toString(); }