Does `call.receive<SomeDataClass>()` parse j...
# ktor
a
Does
call.receive<SomeDataClass>()
parse json string?
đź“ť 1
g
Yes if you have
ContentNegotiation
feature enabled with some Json mapping library (Gson and Jackson implementations are available as part of ktor but you should add them to dependencies)
a
Copy code
install(ContentNegotiation) {
        gson {
            setDateFormat(DateFormat.LONG)
            excludeFieldsWithoutExposeAnnotation()
        }
    }
Is this enough for that?
g
yes, should be enough
I have
Copy code
data class CreatePostRequest(
        val content: String?,
        val userId: Long,
        val with: List<Long>?,
        val checkedIn: Long?,
        val foodId: Long?,
        val menuCategoryId: Long?,
        val actType: String?,
        val photos:List<String> = emptyList()
)
which isn't being deserialized
g
This is different serialization feature, not related to Gson. It’s from kotlinx.serialization
And Gson can map even without nullable fields, but be careful, if you don’t have default constructor, Gson just skip constructor invocation so you will lose your default values and even nullability
isn’t being deserialized
What does it mean?
a
I am getting nulls in all fields
Copy code
CreatePostRequest(content=null, userId=0, with=null, checkedIn=null, foodId=null, menuCategoryId=null, actType=null, photos=null)
g
Don’t forget that request should have type
application/json
, if requests have something like
text/plain
ContentNegotiation doesn’t work for Gson
Are you sure that you request has this data?
a
it does have
application/json
g
Gson deserialize even empty string
try to read raw request and check that you have some data
a
Copy code
{
  "content": "ABCasdasdDFE",
  "userId": 1,
  "with": [
    2,
    3
  ],
  "checkedIn": 2,
  "menuCategoryId": 1,
  "actType": "eat"
}
g
But you don’t have
@Expose
annotation on your fields
Do you really need
excludeFieldsWithoutExposeAnnotation
?
Do you understand what this feature does?
a
oh mybad
it had to do with
Expose
yeah I understand gson ignores fields without
@Expose
as I configured it
g
also probably you should annotate not properties, but fields
a
you mean?
g
something like:
Copy code
@field:Expose
val content: String?
a
what is the difference?
g
Because
content
is property, so in bytecode it’s actually getter, setter and private field, so you should annotate field, because Gson know nothing about kotlin properties
I don’t see much gain from
excludeFieldsWithoutExposeAnnotation
for data classes
a
What should I do if I want to ignore some field then?
g
Do you want ignore data class fields during serialization?
you can use @Transient
anyway, it’s of course depends on your case
a
I read somewhere to not use @Transient as it has other side effects too
g
yes, for example it also excluded from from java serialization
a
Also, how do I fail the json serialization if some fields are missing. I get nulls even if that field is non-nullable. I guess it's because gson doesn't know about Kotlin's nullability
g
actually, looks like instead of
@field:Expose
you can use just
@Expose
, because this annotation only targets fields, so everything should work
a
oh thanks