use of com.servoy.j2db.util.serialize.JSONConverter in project servoy-client by Servoy.
the class ClientState method authenticate.
public Object authenticate(String authenticator_solution, String method, Object[] credentials) throws RepositoryException {
String jscredentials;
JSONConverter jsonConverter;
try {
jsonConverter = new JSONConverter(foundSetManager);
jscredentials = jsonConverter.convertToJSON(credentials);
} catch (Exception e) {
// $NON-NLS-1$
Debug.error("Could not convert credentials object to json", e);
return null;
}
String jsReturn = authenticate(new Credentials(clientInfo.getClientId(), authenticator_solution, method, jscredentials));
try {
return jsonConverter.convertFromJSON(jsReturn);
} catch (Exception e) {
// $NON-NLS-1$
Debug.error("Could not convert authentication json result to object", e);
return null;
}
}
use of com.servoy.j2db.util.serialize.JSONConverter in project servoy-client by Servoy.
the class RhinoConversion method defaultToRhino.
/**
* Default conversion used to convert to Rhino property types that do not explicitly implement component <-> Rhino conversions. <BR/><BR/>
* Types that don't implement the sablo <-> rhino conversions are by default available and their value is accessible directly.
*/
public static Object defaultToRhino(final Object webComponentValue, final PropertyDescription pd, final IWebObjectContext webObjectContext, Scriptable startScriptable) {
if (webComponentValue == null) {
// here we don't know if it's supposed to be null or undefined because for example if value comes from browser to sablo value null (undefined in JSON) will be null but JSONObject.NULL will also be converted into null
return null;
}
if (webComponentValue == JSONObject.NULL) {
// will probably never happen as you don't usually have JSONObject.NULL as a sablo java value; see comment above
return null;
}
if (webComponentValue instanceof IDataSet) {
return new JSDataSet((IDataSet) webComponentValue);
}
if (webComponentValue instanceof List && !(webComponentValue instanceof NativeArray)) {
List<?> array = (List<?>) webComponentValue;
Context cx = Context.enter();
Scriptable newObject = null;
try {
final boolean[] initializing = new boolean[] { true };
newObject = new // see comment on this constant
NativeArray(// see comment on this constant
MAX_NATIVE_ARRAY_LENGTH) {
@Override
public void put(int index, Scriptable start, Object value) {
super.put(index, start, value);
if (!initializing[0]) {
value = defaultFromRhino(value, null);
if (index < ((List) webComponentValue).size()) {
if (!Utils.equalObjects(((List) webComponentValue).get(index), value)) {
((List) webComponentValue).set(index, value);
if (webObjectContext != null && pd != null)
webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
}
} else {
for (int i = ((List) webComponentValue).size(); i < index; i++) {
((List) webComponentValue).add(i, null);
}
((List) webComponentValue).add(index, value);
if (webObjectContext != null && pd != null)
webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
}
}
}
@Override
public void delete(int index) {
super.delete(index);
if (!initializing[0]) {
if (index < ((List) webComponentValue).size()) {
((List) webComponentValue).remove(index);
}
if (webObjectContext != null && pd != null)
webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
}
}
};
ScriptableObject.putProperty(newObject, "length", array.size());
ScriptRuntime.setBuiltinProtoAndParent((NativeArray) newObject, startScriptable, TopLevel.Builtins.Array);
for (int i = 0; i < array.size(); i++) {
Object value = null;
try {
value = array.get(i);
} catch (JSONException e) {
Debug.error(e);
}
if (value != null) {
value = defaultToRhino(value, pd, webObjectContext, startScriptable);
}
ScriptRuntime.setObjectIndex(newObject, i, value, cx);
}
initializing[0] = false;
return newObject;
} finally {
Context.exit();
}
}
if (webComponentValue instanceof Map && !(webComponentValue instanceof NativeObject)) {
Map<?, ?> map = (Map) webComponentValue;
Context cx = Context.enter();
Scriptable newObject = null;
try {
final boolean[] initializing = new boolean[] { true };
// code from Context.newObject(Scriptable)
newObject = new NativeObject() {
@Override
public void put(String name, Scriptable start, Object value) {
super.put(name, start, value);
if (!initializing[0]) {
value = defaultFromRhino(value, null);
if (!Utils.equalObjects(((Map) webComponentValue).get(name), value)) {
((Map) webComponentValue).put(name, value);
if (webObjectContext != null)
webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
}
}
}
@Override
public void delete(String name) {
super.delete(name);
if (!initializing[0]) {
((Map) webComponentValue).remove(name);
if (webObjectContext != null)
webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
}
}
};
ScriptRuntime.setBuiltinProtoAndParent((NativeObject) newObject, startScriptable, TopLevel.Builtins.Object);
for (Object key : ((Map) webComponentValue).keySet()) {
((NativeObject) newObject).defineProperty(key.toString(), defaultToRhino(((Map) webComponentValue).get(key), pd, webObjectContext, startScriptable), ScriptableObject.EMPTY);
}
initializing[0] = false;
return newObject;
} finally {
Context.exit();
}
}
if (webComponentValue instanceof JSONObject) {
JSONObject json = (JSONObject) webComponentValue;
Context cx = Context.enter();
Scriptable newObject = null;
try {
final boolean[] initializing = new boolean[] { true };
// code from Context.newObject(Scriptable)
newObject = new NativeObject() {
private final JSONConverter converter = new JSONConverter();
@Override
public void put(String name, Scriptable start, Object value) {
super.put(name, start, value);
if (!initializing[0]) {
try {
value = converter.convertToJSONValue(value);
} catch (Exception e) {
Debug.error(e);
}
if (!Utils.equalObjects(json.opt(name), value)) {
json.put(name, value);
if (webObjectContext != null)
webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
}
}
}
@Override
public void delete(String name) {
super.delete(name);
if (!initializing[0]) {
json.remove(name);
if (webObjectContext != null)
webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
}
}
};
ScriptRuntime.setBuiltinProtoAndParent((NativeObject) newObject, startScriptable, TopLevel.Builtins.Object);
Iterator<String> iterator = json.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = null;
try {
value = ServoyJSONObject.jsonNullToNull(json.get(key));
} catch (JSONException e) {
Debug.error(e);
}
if (value != null) {
value = defaultToRhino(value, pd, webObjectContext, startScriptable);
}
ScriptRuntime.setObjectElem(newObject, key, value, cx);
}
initializing[0] = false;
return newObject;
} finally {
Context.exit();
}
}
if (webComponentValue instanceof JSONArray) {
JSONArray array = (JSONArray) webComponentValue;
Context cx = Context.enter();
NativeArray newObject = null;
try {
final boolean[] initializing = new boolean[] { true };
newObject = new // see comment on this constant
NativeArray(// see comment on this constant
MAX_NATIVE_ARRAY_LENGTH) {
private final JSONConverter converter = new JSONConverter();
@Override
public void put(int index, Scriptable start, Object value) {
super.put(index, start, value);
if (!initializing[0]) {
try {
value = converter.convertToJSONValue(value);
} catch (Exception e) {
Debug.error(e);
}
if (!Utils.equalObjects(array.opt(index), value)) {
array.put(index, value);
if (webObjectContext != null)
webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
}
}
}
@Override
public void delete(int index) {
super.delete(index);
if (!initializing[0]) {
array.remove(index);
if (webObjectContext != null)
webObjectContext.getUnderlyingWebObject().markPropertyAsChangedByRef(pd.getName());
}
}
};
ScriptableObject.putProperty(newObject, "length", array.length());
ScriptRuntime.setBuiltinProtoAndParent(newObject, startScriptable, TopLevel.Builtins.Array);
for (int i = 0; i < array.length(); i++) {
Object value = null;
try {
value = array.get(i);
} catch (JSONException e) {
Debug.error(e);
}
if (value != null) {
value = defaultToRhino(value, pd, webObjectContext, startScriptable);
}
ScriptRuntime.setObjectIndex(newObject, i, value, cx);
}
initializing[0] = false;
return newObject;
} finally {
Context.exit();
}
}
return webComponentValue;
}
Aggregations