Man... after rough source inspection i finally used DeserializationContext<Element>.element(...) method. This is soo intuitive and sooooo undocumented. For further reference DeserializationContext<Element>.element(...) method can be used for List<Element> reconstruction. But some prerequisites must be fulfilled.
Prerequisites:
1) The attribute being declared as List<Element> must be registered with DeserializationContext<Element>.instance() method.
Parameters:
a) InstanceId- instance id which will be used for DeserializationContext<Element>.attribute() method for that specific field being declared as List<Element>;
b) `Array<Element>`
2) The attribute "size" of the Arrray<Element> for field being declared as List<Element> must be registered with DeserializationContexext<Element>.attribute() method.
Parameters:
a) InstanceId- instance id which will be used for DeserializationContext<Element>.instance() method for that specific field being declared as List<Element> (the same as step before);
b) `Array<Element>.size`.declaration
c) attributeValueId- id of attribute which will be used for attaching value of size for Array<Element>.size attribute.
3) The attribute "size" of the Array<Element> must be set to value coresponding with DeserializationContexext<Element>.element() calls by calling DeserializationContexext<Element>.instanceValue() .
Parameters:
a) instanceId - attributeValueId from last step
b) size of List<Element> / Array<Element> which attribute being reconstructed.
4) All content must be recreated with two methods call:
- DeserializationContexext<Element>.instanceValue()
Parameters:
a) InstanceId - unique id of content value (Element) - (referenced further as "unique")
b) instanceValue - value of content (Element)
- DeserializationContexext<Element>.element()
Parameters:
a) InstanceId- the same id as in point - 1,2
b) index of element in Array<Element>/List<Element>
c) the "unique" identifier used before
The whole test looks like this:
shared serializable class TestDataList(shared List<String> objs){}
shared test void listAutomaticRecreation(){
DeserializationContext<String> context = deserialization<String>();
context.instance(`TestDataList`.declaration.name, `TestDataList`);
context.attribute(`TestDataList`.declaration.name, `TestDataList.objs`.declaration, `TestDataList.objs`.declaration.name);
String[] sequence=["abc","fed"];
context.instance(`TestDataList.objs`.declaration.name, `Array<String>`);
context.attribute(`TestDataList.objs`.declaration.name, `Array<String>.size`.declaration, `Array<String>.size`.declaration.name);
context.instanceValue(`Array<String>.size`.declaration.name, sequence.size);
for(Integer->String a in sequence.indexed){
context.instanceValue(a.key.string, a.item);
context.element(`TestDataList.objs`.declaration.name, a.key, a.key.string);
}
TestDataList reconstruct = context.reconstruct<TestDataList>(`TestDataList`.declaration.name);
assert(sequence.every((String element) => reconstruct.objs.contains(element)));
}
Cheers
Wojciech.
On Monday, 12 September 2016 13:00:38 UTC+2, Wojciech Potiopa wrote:
I was able to recreate each type of collection by manually providing instanceValue with constructed Sequence/Stream/List
example:
import ceylon.test {
test
}
import ceylon.language.serialization {
deserialization,
DeserializationContext
}
shared serializable class TestDataSequence(shared String[] objs){}
shared test void sequenceManualRecreation(){
DeserializationContext<String> context = deserialization<String>();
context.instance(`TestDataSequence`.declaration.name, `TestDataSequence`);
context.attribute(`TestDataSequence`.declaration.name, `TestDataSequence.objs`.declaration, `TestDataSequence.objs`.declaration.name);
String[] objs=["abc","fed"];
context.instanceValue(`TestDataSequence.objs`.declaration.name, objs);
TestDataSequence reconstruct = context.reconstruct<TestDataSequence>(`TestDataSequence`.declaration.name);
assert(reconstruct.objs==objs);
}
is it possible that DeserializationContex would take care with recreation of Sequence/Stream/List for me ? There is DeserializationContext.element method but I'm not able to correctly use it.
shared test void sequenceAutomaticRecreation(){
DeserializationContext<String> context = deserialization<String>();
context.instance(`TestDataSequence`.declaration.name, `TestDataSequence`);
context.attribute(`TestDataSequence`.declaration.name, `TestDataSequence.objs`.declaration, `TestDataSequence.objs`.declaration.name);
String[] objs=["abc","fed"];
for(Integer->String obj in objs.indexed){
context.element(`TestDataSequence.objs`.declaration.name, obj.key, `TestDataSequence.objs`.declaration.name+obj.key.string);
context.instanceValue(`TestDataSequence.objs`.declaration.name+obj.key.string, obj.item);
}
TestDataSequence reconstruct = context.reconstruct<TestDataSequence>(`TestDataSequence`.declaration.name);
assert(reconstruct.objs==objs);
}
Test throws:
ceylon.language.serialization.DeserializationException " no class specified for instance with id objs"
Looking at DeserializationContext.element method it seems as it works like DeserializationContext.attribute so it should be ok ;) How to make it work, the best would be if it would also allow deserialization of collections and streams ? Should i always manually Sequence/Stream/List like in first test ? If so what for is DeserializationContext.element method ?
Cheers
WojciechTo view this discussion on the web visit https://groups.google.com/d/msgid/ceylon-users/81701cfe-58b4-4d54-9bad-70f9282e92d0%40googlegroups.com.--
You received this message because you are subscribed to the Google Groups "ceylon-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-users+unsubscribe@googlegroups.com.
To post to this group, send email to ceylon...@googlegroups.com.
Visit this group at https://groups.google.com/group/ceylon-users.
Hi Tom.
I'm not sure right now if i fully understand application of DeserializationContext methods - so I wouldn't provide you proper documentation update. I can post my doubts and struggles here or via e-mail if you wish - the discussion here may be better option for further generations :). So if you do not mind i will post them here in meantime.
Thank You,
Wojciech
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-users...@googlegroups.com.
To post to this group, send email to ceylon...@googlegroups.com.
Visit this group at https://groups.google.com/group/ceylon-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceylon-users/81701cfe-58b4-4d54-9bad-70f9282e92d0%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "ceylon-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-users+unsubscribe@googlegroups.com.
To post to this group, send email to ceylon...@googlegroups.com.
Visit this group at https://groups.google.com/group/ceylon-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceylon-users/b1d0ee3a-7468-4249-8836-cbedf7b898e7%40googlegroups.com.