View Javadoc

1   /*
2    * $Id: JsonLibHandler.java 651946 2008-04-27 13:41:38Z apetrelli $
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 java.io.*;
25  import java.util.Collection;
26  
27  import net.sf.json.JSONObject;
28  import net.sf.json.JsonConfig;
29  import net.sf.json.JSONArray;
30  
31  /***
32   * Handles JSON content using json-lib
33   */
34  public class JsonLibHandler implements ContentTypeHandler {
35  
36      public void toObject(Reader in, Object target) throws IOException {
37          StringBuilder sb = new StringBuilder();
38          char[] buffer = new char[1024];
39          int len = 0;
40          while ((len = in.read(buffer)) > 0) {
41              sb.append(buffer, 0, len);
42          }
43          if (target != null && sb.length() > 0 && sb.charAt(0) == '[') {
44              JSONArray jsonArray = JSONArray.fromObject(sb.toString());
45              if (target.getClass().isArray()) {
46                  JSONArray.toArray(jsonArray, target, new JsonConfig());
47              } else {
48                  JSONArray.toList(jsonArray, target, new JsonConfig());
49              }
50  
51          } else {
52              JSONObject jsonObject = JSONObject.fromObject(sb.toString());
53              JSONObject.toBean(jsonObject, target, new JsonConfig());
54          }
55      }
56  
57      public String fromObject(Object obj, String resultCode, Writer stream) throws IOException {
58          if (obj != null) {
59              if (isArray(obj)) {
60                  JSONArray jsonArray = JSONArray.fromObject(obj);
61                  stream.write(jsonArray.toString());
62              } else {
63                  JSONObject jsonObject = JSONObject.fromObject(obj);
64                  stream.write(jsonObject.toString());
65              }
66          }
67          return null;
68  
69  
70      }
71  
72      private boolean isArray(Object obj) {
73          return obj instanceof Collection || obj.getClass().isArray();
74      }
75  
76      public String getContentType() {
77          return "text/javascript";
78      }
79      
80      public String getExtension() {
81          return "json";
82      }
83  }