View Javadoc

1   /*
2    * $Id: JsonLibHandler.java 666756 2008-06-11 18:11:00Z hermanns $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.rest.handler;
23  
24  import net.sf.json.JSONArray;
25  import net.sf.json.JSONObject;
26  import net.sf.json.JsonConfig;
27  
28  import java.io.IOException;
29  import java.io.Reader;
30  import java.io.Writer;
31  import java.util.Collection;
32  
33  /***
34   * Handles JSON content using json-lib
35   */
36  public class JsonLibHandler implements ContentTypeHandler {
37  
38      public void toObject(Reader in, Object target) throws IOException {
39          StringBuilder sb = new StringBuilder();
40          char[] buffer = new char[1024];
41          int len = 0;
42          while ((len = in.read(buffer)) > 0) {
43              sb.append(buffer, 0, len);
44          }
45          if (target != null && sb.length() > 0 && sb.charAt(0) == '[') {
46              JSONArray jsonArray = JSONArray.fromObject(sb.toString());
47              if (target.getClass().isArray()) {
48                  JSONArray.toArray(jsonArray, target, new JsonConfig());
49              } else {
50                  JSONArray.toList(jsonArray, target, new JsonConfig());
51              }
52  
53          } else {
54              JSONObject jsonObject = JSONObject.fromObject(sb.toString());
55              JSONObject.toBean(jsonObject, target, new JsonConfig());
56          }
57      }
58  
59      public String fromObject(Object obj, String resultCode, Writer stream) throws IOException {
60          if (obj != null) {
61              if (isArray(obj)) {
62                  JSONArray jsonArray = JSONArray.fromObject(obj);
63                  stream.write(jsonArray.toString());
64              } else {
65                  JSONObject jsonObject = JSONObject.fromObject(obj);
66                  stream.write(jsonObject.toString());
67              }
68          }
69          return null;
70  
71  
72      }
73  
74      private boolean isArray(Object obj) {
75          return obj instanceof Collection || obj.getClass().isArray();
76      }
77  
78      public String getContentType() {
79          return "text/javascript";
80      }
81      
82      public String getExtension() {
83          return "json";
84      }
85  }