View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.util.ajax;
15  
16  import java.util.Map;
17  
18  import org.eclipse.jetty.util.Loader;
19  import org.eclipse.jetty.util.ajax.JSON.Convertor;
20  import org.eclipse.jetty.util.ajax.JSON.Output;
21  
22  public class JSONPojoConvertorFactory implements JSON.Convertor
23  {
24      private JSON _json=null;
25  
26      public JSONPojoConvertorFactory(JSON json)
27      {
28          if (json==null)
29          {
30              throw new IllegalArgumentException();
31          }
32          _json=json;
33      }
34      
35      public void toJSON(Object obj, Output out)
36      {
37          String clsName=obj.getClass().getName();
38          Convertor convertor=_json.getConvertorFor(clsName);
39          if (convertor==null)
40          {
41              try
42              {
43                  Class cls=Loader.loadClass(JSON.class,clsName);
44                  convertor=new JSONPojoConvertor(cls);
45                  _json.addConvertorFor(clsName, convertor);
46               }
47              catch (ClassNotFoundException e)
48              {
49                  e.printStackTrace();
50              }
51          }
52          if (convertor!=null)
53          {
54              convertor.toJSON(obj, out);
55          }
56      }
57  
58      public Object fromJSON(Map object)
59      {
60          Map map=object;
61          String clsName=(String)map.get("class");
62          if (clsName!=null)
63          {
64              Convertor convertor=_json.getConvertorFor(clsName);
65              if (convertor==null)
66              {
67                  try
68                  {
69                      Class cls=Loader.loadClass(JSON.class,clsName);
70                      convertor=new JSONPojoConvertor(cls);
71                      _json.addConvertorFor(clsName, convertor);
72                  }
73                  catch (ClassNotFoundException e)
74                  {
75                      e.printStackTrace();
76                  }
77              }
78              if (convertor!=null)
79              {
80                  return convertor.fromJSON(object);
81              }
82          }
83          return map;
84      }
85  }