Skip to content

Instantly share code, notes, and snippets.

@gavinking
Last active August 29, 2015 14:10
Show Gist options
  • Save gavinking/ca2fba39c73d9dc376ee to your computer and use it in GitHub Desktop.
Save gavinking/ca2fba39c73d9dc376ee to your computer and use it in GitHub Desktop.
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