Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
playing with serialization
import ceylon.language.serialization {
serialization,
Deconstructor
}
import ceylon.language.meta.model {
ClassModel,
Type
}
import ceylon.language.meta.declaration {
ValueDeclaration,
TypeParameter
}
import ceylon.json {
Object,
nil
}
class Point(shared Float x, shared Float y) {}
variable Integer generator = 0;
void test() {
value context = serialization();
Point point = Point(0.0, 1.0);
value id = generator++;
value ref = context.reference(id, point);
value result = Object { "$"->id };
ref.serialize((classModel) => object satisfies Deconstructor {
shared actual void putElement<Instance>(Integer index, Instance referenced) {}
shared actual void putOuterInstance<Instance>(Instance outerInstance) {}
shared actual void putTypeArgument(TypeParameter typeParameter, Type<Anything> typeArgument) {}
shared actual void putValue<Instance>(ValueDeclaration attribute, Instance referenced) {
String attName = attribute.name;
switch (ref = referenced of Anything)
case (is String|Integer|Float) {
result.put(attName, ref);
}
case (null) {
result.put(attName, nil);
}
else {
if (is Identifiable ref) { //a reference to another object in stream
//TODO: how do I get the id of ref?
result.put(attName,
context.getId(ref));
}
else { //an "embedded" object
value obj = Object();
//TODO: recurse it's properties!
result.put(attName, obj);
}
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment