Gson: How to exclude specific fields from Serialization without annotations

后端 未结 15 1638
后悔当初
后悔当初 2020-11-22 05:39

I\'m trying to learn Gson and I\'m struggling with field exclusion. Here are my classes

public class Student {    
  private Long                id;
  privat         


        
15条回答
  •  野性不改
    2020-11-22 06:15

    Kotlin's @Transientannotation also does the trick apparently.

    data class Json(
        @field:SerializedName("serialized_field_1") val field1: String,
        @field:SerializedName("serialized_field_2") val field2: String,
        @Transient val field3: String
    )
    

    Output:

    {"serialized_field_1":"VALUE1","serialized_field_2":"VALUE2"}
    

提交回复
热议问题