Hi all. Can anybody point me to a decent example f...
# multiplatform
d
Hi all. Can anybody point me to a decent example for how to automatically marshall and unmarshall a class to and from JSON? Something akin to how we do it in Java (using Jackson and annotations) would be great.
d
Do I need the JVM plugin? Hopefully not since my targets are Android and iOS for this project.
c
β€’ Complete multiplatform support: JVM, JS and Native.
d
About to test out using this. Wish me luck, and thanks!
It worked! Now to figure out why Ktor's not sending my content and I'll be good. πŸ˜„
c
ktor has its own plugin using this. https://ktor.io/docs/server-serialization.html
d
I have a dependency on ktor-client-content-negotiation already: is that not for doing the client-side negotiation?
c
yes, thats fine on client side.
πŸ‘ 1
and you set it up using kotlinx.serialization ?
d
I have my HttpClient setup like this:
Copy code
private val client: HttpClient = HttpClient {
        install(ContentNegotiation) {
            json(Json { isLenient = true; ignoreUnknownKeys = true })
        }

        install(Logging) {
            logger = HttpClientLogger
            level = LogLevel.ALL
        }
    }
πŸ‘πŸΌ 1
But when I just tried to send that object that was marshalled to JSON (did an evaluate expression to verify that the object was properly marshalled) I see this in Logcat:
Copy code
2025-05-24 12:04:40.192 31658-31686 NoteFeedApi             app.nogeo.mobile.android             D  REQUEST: <http://192.168.86.104:8080/api/v1/location/notes/save>
                                                                                                    METHOD: POST
                                                                                                    COMMON HEADERS
                                                                                                    -> Accept: application/json
                                                                                                    -> Accept-Charset: UTF-8
                                                                                                    CONTENT HEADERS
                                                                                                    -> Content-Length: 0
                                                                                                    BODY Content-Type: null
                                                                                                    BODY START
                                                                                                    
                                                                                                    BODY END
Not sure exactly what I'm doing wrong πŸ˜•
My method that's making the actual call with that HttpClient is:
Copy code
client.post("http://${hostname}/api/v1/location/notes/save", {
        Json.encodeToString($THEARGS))
    }).body()
I'm sure I'm doing something wrong: just trying to learn as I go.
c
first start reading documentation πŸ˜‰
d
That's much nicer. Trying it now.
Will that also be able to unmarshall the response to an object?
c
man, READ THE DOCS πŸ˜… it the next point in the link.
πŸ˜„ 1
d
Yeah, sorry, sometimes it gets comfortable asking for the next fish rather than casting the line. πŸ˜„ You got me unstuck (my requests are now going through) so thank you, bud!
πŸ‘πŸΌ 1
😡 1