Here is how I implemented such attribute:
{
adaptorValueConversionClassName =
"er.extensions.foundation.ERXPropertyListSerialization";
adaptorValueConversionMethodName = jsonStringFromPropertyList;
allowsNull = Y;
columnName = "dictionary";
externalType = VARCHAR2;
factoryMethodArgumentType = EOFactoryMethodArgumentIsNSString;
name = dictionary;
valueClassName = Object;
valueFactoryClassName =
"er.extensions.foundation.ERXPropertyListSerialization";
valueFactoryMethodName = dictionaryForJSONString;
valueType = S;
width = 4000;
},
Once the entity generated to a class, you have to change the _TheEntity.java
file and change the type of the attribute from Object to NSDictionary *1*.
Since there is a problem with mutable object as attribute (maybe there is
another way to fix it?), you have to consider the attribute as immutable and
use cover method to a mutable one.
So when you need to deal with an immutable version, do it with
mutableDictionary().
If you just need to ask contents, simply use dictionary();
Use the following code in TheEntity.java:
LeanMutableDictionary _mutableDictionary = null;
public LeanMutableDictionary mutableDictionary() {
if (_mutableDictionary == null) {
_mutableDictionary = new LeanMutableDictionary();
}
return _mutableDictionary;
}
/**
* Cover class to ease KVC. This « lean » version remove key when value
is null or Boolean.false.
*/
public class LeanMutableDictionary implements NSKeyValueCoding {
@Override
public Object valueForKey(final String key) {
final NSDictionary dict = userInfoDict();
if (dict != null) {
return dict.valueForKey(key);
}
return null;
}
@Override
@SuppressWarnings("unused")
public void takeValueForKey(final Object val, final String key)
{
NSDictionary dict = userInfoDict();
if (dict == null) {
dict = NSDictionary.EmptyDictionary;
}
dict = dict.mutableClone();
if (val == null || val.equals(Boolean.FALSE)) {
// remove that key from the dictionary
dict.remove(key);
} else {
dict.takeValueForKey(val, key);
}
setUserInfoDict(dict.immutableClone());
}
}
[1] This is because the type (valueClassName) will be used to find the right
method (with the type argument) and the method is defined in
ERXPropertyListSerialization with Object, not NSDictionary. So you have to
keep it Object.
-
jfv
Le 2014-03-24 à 17:56, Oscar González <[email protected]> a écrit :
> Hi all,
> I need to save a NSDictionary to the DB and then recovery it back.
> Is there some formatter to do this?
>
> Saludos,
>
> Oscar González Pérez
> TECH in AGRO S.A.
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list ([email protected])
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/webobjects-dev/jean_francois_veillette%40yahoo.ca
>
> This email sent to [email protected]
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com
This email sent to [email protected]