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.util.Map;
22  
23  import org.eclipse.jetty.util.Loader;
24  import org.eclipse.jetty.util.ajax.JSON.Convertor;
25  import org.eclipse.jetty.util.ajax.JSON.Output;
26  
27  public class JSONPojoConvertorFactory implements JSON.Convertor
28  {
29      private final JSON _json;
30      private final boolean _fromJson;
31  
32      public JSONPojoConvertorFactory(JSON json)
33      {
34          if (json==null)
35          {
36              throw new IllegalArgumentException();
37          }
38          _json=json;
39          _fromJson=true;
40      }
41      
42      /* ------------------------------------------------------------ */
43      /**
44       * @param json The JSON instance to use
45       * @param fromJSON If true, the class name of the objects is included
46       * in the generated JSON and is used to instantiate the object when
47       * JSON is parsed (otherwise a Map is used).
48       */
49      public JSONPojoConvertorFactory(JSON json,boolean fromJSON)
50      {
51          if (json==null)
52          {
53              throw new IllegalArgumentException();
54          }
55          _json=json;
56          _fromJson=fromJSON;
57      }
58      
59      /* ------------------------------------------------------------ */
60      public void toJSON(Object obj, Output out)
61      {
62          String clsName=obj.getClass().getName();
63          Convertor convertor=_json.getConvertorFor(clsName);
64          if (convertor==null)
65          {
66              try
67              {
68                  Class cls=Loader.loadClass(JSON.class,clsName);
69                  convertor=new JSONPojoConvertor(cls,_fromJson);
70                  _json.addConvertorFor(clsName, convertor);
71               }
72              catch (ClassNotFoundException e)
73              {
74                  JSON.LOG.warn(e);
75              }
76          }
77          if (convertor!=null)
78          {
79              convertor.toJSON(obj, out);
80          }
81      }
82  
83      public Object fromJSON(Map object)
84      {
85          Map map=object;
86          String clsName=(String)map.get("class");
87          if (clsName!=null)
88          {
89              Convertor convertor=_json.getConvertorFor(clsName);
90              if (convertor==null)
91              {
92                  try
93                  {
94                      Class cls=Loader.loadClass(JSON.class,clsName);
95                      convertor=new JSONPojoConvertor(cls,_fromJson);
96                      _json.addConvertorFor(clsName, convertor);
97                  }
98                  catch (ClassNotFoundException e)
99                  {
100                     JSON.LOG.warn(e);
101                 }
102             }
103             if (convertor!=null)
104             {
105                 return convertor.fromJSON(object);
106             }
107         }
108         return map;
109     }
110 }