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