tombentley / alabama

A JSON-based Ceylon serialization library
Other
6 stars 2 forks source link

Deserialize defaulted null fields #5

Open ghost opened 6 years ago

ghost commented 6 years ago

Alabama 1.0.4 is not able to deserialize class object, if defaulted null fields, are not provided in JSON. For example

{
"someField":"abc",
"someNullableField":null 
}

Can be deserialized, but

{
"someField":"abc",
}

can't.

So the bellow code will throw

import ceylon.test {
    test
}
import com.github.tombentley.alabama {
    deserialize
}

shared serializable
class NullableField(shared String name, shared String? id = null) {}

String json = "{\"name\":\"abc\"}";

shared test
void shouldDeserializeNullableField() {
    deserialize<NullableField>(json);
}

ceylon run: lacking sufficient state for instance with id 0: value x.x.x.x::NullableField.id
ceylon.language.serialization.DeserializationException "lacking sufficient state for instance with id 0: value x.x.x.x::NullableField.id"
    at ceylon.language.serialization.PartialImpl.insufficiantState(PartialImpl.java:354)
    at ceylon.language.serialization.PartialImpl.initializeObject(PartialImpl.java:266)
    at ceylon.language.serialization.PartialImpl.initialize(PartialImpl.java:157)
    at ceylon.language.serialization.DeserializationContextImpl.reconstruct(DeserializationContextImpl.ceylon:190)
    at com.github.tombentley.alabama.Deserializer.deserialize(Deserializer.ceylon:250)
    at com.github.tombentley.alabama.deserialize_.deserialize(Deserializer.ceylon:652)
    at com.github.tombentley.alabama.deserialize_.deserialize(Deserializer.ceylon:649)
    at test.com.voitech.calcx.http.shouldDeserializeNullableField_.shouldDeserializeNullableField(TestSerialization.ceylon:18)
    at x.x.x.x.shouldDeserializeNullableField_.main(TestSerialization.ceylon)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.redhat.ceylon.compiler.java.runtime.tools.impl.JavaRunnerImpl.invokeMain(JavaRunnerImpl.java:93)
    at com.redhat.ceylon.compiler.java.runtime.tools.impl.JavaRunnerImpl.run(JavaRunnerImpl.java:61)
    at ceylon.modules.bootstrap.CeylonRunTool.startInFlatClasspath(CeylonRunTool.java:424)
    at ceylon.modules.bootstrap.CeylonRunTool.run(CeylonRunTool.java:289)
    at com.redhat.ceylon.common.tools.CeylonTool.run(CeylonTool.java:547)
    at com.redhat.ceylon.common.tools.CeylonTool.execute(CeylonTool.java:423)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.redhat.ceylon.launcher.Launcher.runInJava7Checked(Launcher.java:108)
    at com.redhat.ceylon.launcher.Launcher.run(Launcher.java:38)
    at com.redhat.ceylon.launcher.Launcher.run(Launcher.java:31)
    at com.redhat.ceylon.launcher.Launcher.main(Launcher.java:24)

Is it intentional ? If not, how to enable this feature.

tombentley commented 6 years ago

It's not unreasonable to want to be able to do this. Off the top of my head, I don't recall the details of how alabama handles this stuff, so I can't really say how easy this request would be to implement, and currently I don't have time to work on this myself. You're welcome to try and submit a PR, and if you do I will find the time to review it.