use of com.webobjects.foundation.NSMutableData in project wonder-slim by undur.
the class NSDataSerializer method unmarshall.
public Object unmarshall(SerializerState state, Class clazz, Object o) throws UnmarshallException {
try {
JSONObject jso = (JSONObject) o;
String java_class = jso.getString("javaClass");
if (java_class == null) {
throw new UnmarshallException("no type hint");
}
String string = jso.getString("bytes");
NSData data = (NSData) NSPropertyListSerialization.propertyListFromString(string);
if (NSMutableData.class.equals(clazz)) {
NSMutableData mutableData = new NSMutableData(data);
state.setSerialized(o, mutableData);
return mutableData;
} else if (NSData.class.equals(clazz)) {
state.setSerialized(o, data);
return data;
}
throw new UnmarshallException("invalid class " + clazz);
} catch (JSONException e) {
throw new UnmarshallException("Failed to unmarshall NSData.", e);
}
}
use of com.webobjects.foundation.NSMutableData in project wonder-slim by undur.
the class ERXValueUtilities method dataValueWithDefault.
/**
* Basic utility method for reading <code>NSData</code> values which also
* works with serialized NSData. The default value is used if the object is
* null.
*
* @param obj
* object to be evaluated
* @param def
* default value if object is null
* @return NSData evaluation of the given object
*/
public static NSData dataValueWithDefault(Object obj, NSData def) {
NSData value = def;
if (!ERXValueUtilities.isNull(obj)) {
if (obj instanceof NSData) {
value = (NSData) obj;
} else if (obj instanceof byte[]) {
byte[] byteValue = (byte[]) obj;
value = new NSData(byteValue, new NSRange(0, byteValue.length), true);
} else if (obj instanceof String) {
String strValue = ((String) obj).trim();
if (strValue.length() > 0) {
// MS:
Object objValue = NSPropertyListSerialization.propertyListFromString(strValue);
// Encoding?
if (objValue == null || !(objValue instanceof NSData)) {
throw new IllegalArgumentException("Failed to parse data from the value '" + obj + "'.");
}
value = (NSData) objValue;
if (value instanceof NSMutableData) {
// AK: we need NSData if we want to use it for a PK, but
// we get NSMutableData
value = new NSData(value);
}
}
} else {
throw new IllegalArgumentException("Failed to parse data from the value '" + obj + "'.");
}
}
return value;
}
Aggregations