1//
2//  ========================================================================
3//  Copyright (c) 1995-2014 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
19package org.eclipse.jetty.util.ajax;
20
21import java.lang.reflect.Method;
22import java.lang.reflect.Modifier;
23import java.util.Arrays;
24import java.util.HashSet;
25import java.util.Locale;
26import java.util.Map;
27import java.util.Set;
28
29import org.eclipse.jetty.util.ajax.JSON.Output;
30
31/* ------------------------------------------------------------ */
32/**
33 * Convert an Object to JSON using reflection on getters methods.
34 *
35 *
36 *
37 */
38public class JSONObjectConvertor implements JSON.Convertor
39{
40    private boolean _fromJSON;
41    private Set _excluded=null;
42
43    public JSONObjectConvertor()
44    {
45        _fromJSON=false;
46    }
47
48    public JSONObjectConvertor(boolean fromJSON)
49    {
50        _fromJSON=fromJSON;
51    }
52
53    /* ------------------------------------------------------------ */
54    /**
55     * @param fromJSON
56     * @param excluded An array of field names to exclude from the conversion
57     */
58    public JSONObjectConvertor(boolean fromJSON,String[] excluded)
59    {
60        _fromJSON=fromJSON;
61        if (excluded!=null)
62            _excluded=new HashSet(Arrays.asList(excluded));
63    }
64
65    public Object fromJSON(Map map)
66    {
67        if (_fromJSON)
68            throw new UnsupportedOperationException();
69        return map;
70    }
71
72    public void toJSON(Object obj, Output out)
73    {
74        try
75        {
76            Class c=obj.getClass();
77
78            if (_fromJSON)
79                out.addClass(obj.getClass());
80
81            Method[] methods = obj.getClass().getMethods();
82
83            for (int i=0;i<methods.length;i++)
84            {
85                Method m=methods[i];
86                if (!Modifier.isStatic(m.getModifiers()) &&
87                        m.getParameterTypes().length==0 &&
88                        m.getReturnType()!=null &&
89                        m.getDeclaringClass()!=Object.class)
90                {
91                    String name=m.getName();
92                    if (name.startsWith("is"))
93                        name=name.substring(2,3).toLowerCase(Locale.ENGLISH)+name.substring(3);
94                    else if (name.startsWith("get"))
95                        name=name.substring(3,4).toLowerCase(Locale.ENGLISH)+name.substring(4);
96                    else
97                        continue;
98
99                    if (includeField(name,obj,m))
100                        out.add(name, m.invoke(obj,(Object[])null));
101                }
102            }
103        }
104        catch (Throwable e)
105        {
106            throw new IllegalArgumentException(e);
107        }
108    }
109
110    protected boolean includeField(String name, Object o, Method m)
111    {
112        return _excluded==null || !_excluded.contains(name);
113    }
114
115}
116