Is there a simple way to ignore a field when using moshi to serialize to a json string? I can only think about is a custom adapter - but I have the feeling that there is a better way
Asked
Active
Viewed 9,945 times
1 Answers
16
Use transient
on the field declaration.
private transient String your_variable_name;
Originally I found Exclude fields from serialization and deserialization
Hope It's help you.

pRaNaY
- 24,642
- 24
- 96
- 146
-
Thanks - you get the bounty but I have to wait 22h – ligi Mar 28 '16 at 14:59
-
Glade to know it helped you. :) – pRaNaY Mar 28 '16 at 15:05
-
5How to do it in kotlin? – Greg Ennis Jan 01 '18 at 15:51
-
5@GregEnnis add the `@Transient` annotation – RobCo Feb 03 '18 at 23:56
-
Sorry, my question is actually: How to do it in Kotlin on a lazy property? Moshi chokes on lazy properties, and Transient cannot be applied to a property. So I'm stuck @RobCo – Greg Ennis Mar 02 '18 at 12:53
-
2`@IgnoredOnParcel @delegate:Transient val firstName by lazy { computeFirstName() }` – Evin1_ Jul 23 '18 at 13:07
-
transient is a terrible use, because it's also picked up and interpreted by other systems. You need a way to tell Moshi to ignore a field without making it transient. – Brill Pappin Feb 12 '21 at 15:38