org.openqa.selenium.remote.BeanToJsonConverter Maven / Gradle / Ivy
/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Copyright 2008 Google Inc. All Rights Reserved.
package org.openqa.selenium.remote;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.WebDriverException;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Utility class for converting between JSON and Java Objects.
*/
public class BeanToJsonConverter {
private static final int MAX_DEPTH = 5;
/**
* Convert an object that may or may not be a JSONArray or JSONObject into
* its JSON string representation, handling the case where it is neither in a
* graceful way.
*
* @param object which needs conversion
* @return the JSON string representation of object
*/
public String convert(Object object) {
if (object == null)
return null;
try {
Object converted = convertObject(object, MAX_DEPTH);
if (converted instanceof JSONObject || converted instanceof JSONArray) {
return converted.toString();
}
return String.valueOf(object);
} catch (JSONException e) {
throw new WebDriverException("Unable to convert: " + object, e);
}
}
/**
* Convert a JSON[Array|Object] into the equivalent Java Collection type
* (that is, List|Map) returning other objects untouched. This method is used
* for preparing values for use by the HttpCommandExecutor
*
* @param o Object to convert
* @return a Map, List or the unconverted Object.
*/
private Object convertUnknownObjectFromJson(Object o) {
if (o instanceof JSONArray) {
return convertJsonArray((JSONArray) o);
}
if (o instanceof JSONObject) {
return convertJsonObject((JSONObject) o);
}
return o;
}
private Map convertJsonObject(JSONObject jsonObject) {
Map toReturn = new HashMap();
Iterator allKeys = jsonObject.keys();
while (allKeys.hasNext()) {
String key = (String) allKeys.next();
try {
toReturn.put(key, convertUnknownObjectFromJson(jsonObject.get(key)));
} catch (JSONException e) {
throw new IllegalStateException("Unable to access key: " + key, e);
}
}
return toReturn;
}
private List