Search in sources :

Example 1 with NSMutableData

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);
    }
}
Also used : NSData(com.webobjects.foundation.NSData) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) NSMutableData(com.webobjects.foundation.NSMutableData) UnmarshallException(org.jabsorb.serializer.UnmarshallException)

Example 2 with NSMutableData

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;
}
Also used : NSRange(com.webobjects.foundation.NSRange) NSData(com.webobjects.foundation.NSData) NSMutableData(com.webobjects.foundation.NSMutableData)

Aggregations

NSData (com.webobjects.foundation.NSData)2 NSMutableData (com.webobjects.foundation.NSMutableData)2 NSRange (com.webobjects.foundation.NSRange)1 UnmarshallException (org.jabsorb.serializer.UnmarshallException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1