| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package org.eclipse.jetty.util.ajax; |
| 20 |
|
| 21 |
import java.lang.reflect.Method; |
| 22 |
import java.util.Map; |
| 23 |
|
| 24 |
import org.eclipse.jetty.util.Loader; |
| 25 |
import org.eclipse.jetty.util.ajax.JSON.Output; |
| 26 |
import org.eclipse.jetty.util.log.Log; |
| 27 |
import org.eclipse.jetty.util.log.Logger; |
| 28 |
|
| 29 |
/** |
| 30 |
* Convert an {@link Enum} to JSON. |
| 31 |
* If fromJSON is true in the constructor, the JSON generated will |
| 32 |
* be of the form {class="com.acme.TrafficLight",value="Green"} |
| 33 |
* If fromJSON is false, then only the string value of the enum is generated. |
| 34 |
*/ |
| 35 |
public class JSONEnumConvertor implements JSON.Convertor |
| 36 |
{ |
| 37 |
private static final Logger LOG = Log.getLogger(JSONEnumConvertor.class); |
| 38 |
private boolean _fromJSON; |
| 39 |
private Method _valueOf; |
| 40 |
|
| 41 |
{ |
| 42 |
try |
| 43 |
{ |
| 44 |
Class<?> e = Loader.loadClass("java.lang.Enum"); |
| 45 |
_valueOf = e.getMethod("valueOf", Class.class, String.class); |
| 46 |
} |
| 47 |
catch (Exception e) |
| 48 |
{ |
| 49 |
throw new RuntimeException("!Enums", e); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public JSONEnumConvertor() |
| 54 |
{ |
| 55 |
this(false); |
| 56 |
} |
| 57 |
|
| 58 |
public JSONEnumConvertor(boolean fromJSON) |
| 59 |
{ |
| 60 |
_fromJSON = fromJSON; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public Object fromJSON(Map map) |
| 65 |
{ |
| 66 |
if (!_fromJSON) |
| 67 |
throw new UnsupportedOperationException(); |
| 68 |
try |
| 69 |
{ |
| 70 |
Class c = Loader.loadClass((String)map.get("class")); |
| 71 |
return _valueOf.invoke(null, c, map.get("value")); |
| 72 |
} |
| 73 |
catch (Exception e) |
| 74 |
{ |
| 75 |
LOG.warn(e); |
| 76 |
} |
| 77 |
return null; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void toJSON(Object obj, Output out) |
| 82 |
{ |
| 83 |
if (_fromJSON) |
| 84 |
{ |
| 85 |
out.addClass(obj.getClass()); |
| 86 |
out.add("value", ((Enum)obj).name()); |
| 87 |
} |
| 88 |
else |
| 89 |
{ |
| 90 |
out.add(((Enum)obj).name()); |
| 91 |
} |
| 92 |
} |
| 93 |
} |