Moshi: Support Java record classes when they are stable

Created on 2 Dec 2020  路  3Comments  路  Source: square/moshi

As of the latest release 2.11.0 (and probably past releases too) Moshi is not able to deserialize Java Records, it simply fails with an AssertionError when calling Adapter#fromJson with an appropriate JSON object.
See the attached very simple sample project for more details: test_project.zip
Stacktrace:

Exception in thread "main" java.lang.AssertionError
    at com.squareup.moshi.ClassJsonAdapter.fromJson(ClassJsonAdapter.java:208)
    at com.squareup.moshi.internal.NullSafeJsonAdapter.fromJson(NullSafeJsonAdapter.java:41)
    at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:51)
    at Main.main(Main.java:8)
enhancement

Most helpful comment

It's been stable for two days, give us some time :)

All 3 comments

Moshi may support records in the future once records are stable, but for now you can use this implementation: https://github.com/ZacSweers/MoshiX/tree/main/moshi-records-reflect

Java 16 was released just recently and brought with it full support for records. Since they are now deemed stable enough to no longer be a preview feature, have there been any updates for this issue regarding serializing and deserializing them? It would be much more convenient to use records rather than writing out all the boilerplate (ie. getters, constructors) usually needed for a POJO. Apologies if this topic has already been discussed elsewhere, I don't keep up with Moshi's development very much. Thanks in advance 馃槃

It's been stable for two days, give us some time :)

Was this page helpful?
0 / 5 - 0 ratings