-1

The Class that I'm using with toJson

class LoadUserDTO: Serializable {
    @SerializedName("userGuid")
    var userGuid: String? = null

    @SerializedName("userToken")
    var userToken: String? = null

    constructor(userGuid: String, userToken: String) {
        this.userGuid = userGuid
        this.userToken = userToken
    }
}

And the rest of the use:

val payload = LoadUserDTO(userGuid = user.guid!!, userToken = user.token!!)

val jsonPayload = Gson().toJson(payload)

this.socketService.socket.emit(EndpointsEnum.AppLoadUser, jsonPayload)

The jsonPayload should be a JSON Object. Instead, the entire thing is a String of what should be a JSON Object:

"{"userGuid":"...","userToken":"..."}"

When this is received by the server, it's not receiving a JSON Object as the Web UI or Swift App sends, but is instead just a String, not converted to a JSON Object.

3
  • can u log what payload contains? Commented Sep 6, 2021 at 15:39
  • What do you mean by a JSON object? what is your expected output compared to whats currently happening? How do you know it is sending a string? Commented Sep 6, 2021 at 15:55
  • @DevWithZachary I can set a breakpoint on the API endpoint. The data is a string, when it should be an object. When I do the same thing via the Web App its an object, also when I do it from the iOS App, it's an object. In this case it's sending across as a string. Commented Sep 6, 2021 at 17:35

3 Answers 3

0

try this,

var jsonString = """{"id":1,"description":"Test"}""";
var testModel = gson.fromJson(jsonString, TestModel::class.java)
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not looking to create a Java Object. I want an actual JSON Object sent over the wire instead of a string of the JSON Object.
0

Based on your requirement instead using Gson you may use String.format to achieve this by one line of code -

   // val payload = LoadUserDTO(userGuid = user.guid!!, userToken = user.token!!)
   // val jsonPayload = Gson().toJson(payload)
     val jsonPayload = String.format("\"{\"userGuid\": \"%s\"," + "\"userToken\": \"%s\"" + "}\"",user.guid,user.token)
   // Log.d("ZI",jsonPayload)

Output- example

"{"userGuid": "1234","userToken": "token000"}"

You may remove space after : while formatting string .

       val jsonPayload = String.format("\"{\"userGuid\":\"%s\"," + "\"userToken\":\"%s\"" + "}\"",user.guid,user.token)

Output-

"{"userGuid":"1234","userToken":"token000"}"

1 Comment

I have a lot of DTOs to setup as Objects. I definitely don't want to have to deal with string interpolation.
0

I don't fully understand why yet, but this SO Question gave me the answer for it to work:

Send JSON data from in socket.io from android

val jsonPayload = Gson().toJson(payload)

val jsonObj = JSONObject(jsonPayload)

this.socketService.socket.emit(EndpointsEnum.AppLoadUser, jsonObj)

After making the jsonObj the server now has a properly formed JSON Object that it can work with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.