View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
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  /**
31   * Convert an {@link Enum} to JSON.
32   * If fromJSON is true in the constructor, the JSON generated will
33   * be of the form {class="com.acme.TrafficLight",value="Green"}
34   * If fromJSON is false, then only the string value of the enum is generated.
35   *
36   *
37   */
38  public class JSONEnumConvertor implements JSON.Convertor
39  {
40      private static final Logger LOG = Log.getLogger(JSONEnumConvertor.class);
41      private boolean _fromJSON;
42      private Method _valueOf;
43      {
44          try
45          {
46              Class<?> e = Loader.loadClass(getClass(),"java.lang.Enum");
47              _valueOf=e.getMethod("valueOf",Class.class,String.class);
48          }
49          catch(Exception e)
50          {
51              throw new RuntimeException("!Enums",e);
52          }
53      }
54  
55      public JSONEnumConvertor()
56      {
57          this(false);
58      }
59  
60      public JSONEnumConvertor(boolean fromJSON)
61      {
62          _fromJSON=fromJSON;
63      }
64  
65      public Object fromJSON(Map map)
66      {
67          if (!_fromJSON)
68              throw new UnsupportedOperationException();
69          try
70          {
71              Class c=Loader.loadClass(getClass(),(String)map.get("class"));
72              return _valueOf.invoke(null,c,map.get("value"));
73          }
74          catch(Exception e)
75          {
76              LOG.warn(e);
77          }
78          return null;
79      }
80  
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  }