Hey! I’m having issues with RudderStack Java SDK. ...
# support
f
Hey! I’m having issues with RudderStack Java SDK. The problem is that I can’t provide custom GSON serializer within builder.
Copy code
this.client = RudderAnalytics.builder(apiKey)
            .setDataPlaneUrl(dataPlaneUrl)
            .flushQueueSize(10)
            .log(RudderStackLog())
            .build()
Because of that I can’t provide custom TypeAdapter to correctly map “Instant” Java class. Following method throws an error:
Copy code
public int messageSizeInBytes(Message message) {
    Gson gson = getGsonInstance();
    String stringifiedMessage = gson.toJson(message); <<--- Can't serialize Instant

    int sizeInBytes = stringifiedMessage.getBytes(ENCODING).length;
    return sizeInBytes;
  }
Exception:
Copy code
com.google.gson.JsonIOException: Failed making field 'java.time.Instant#seconds' accessible; either change its visibility or write a custom TypeAdapter for its declaring type
Is there any way to workaround the problem? 🤔 BTW I’m using Java SDK from Kotlin code.
<!subteam^S04QT1G74Q3|@sdk-team>
k
@narrow-wolf-10290
n
@full-evening-54933 Can you share us the class where you are using Instant? How you making it a part of the Message Object?
f
Part of implementation @narrow-wolf-10290 :
Copy code
{
...        
val message = TrackMessage.builder("api_requested")
            .userId(userContext.userId)
            .properties(
                mapOf(
                    "serviceName" to serviceName,
                    "request" to mapOf(
                        "url" to requestContext.url,
                        "queryName" to requestContext.queryName,
                        "queryArguments" to requestContext.queryArgs,
                    ),
                    "error" to errorValue,
                ),
            )
  this.client.enqueue(message)
}
Where
requestContext.queryArgs
is
Map<String, Any>
. And one of the fields in the structure is
Instant
. Now I made a workaround to just parse
requestContext.queryArgs
POJO -> JSON -> POJO by custom ObjectMapper (Jackson) . It works because “unsupported” Instant class is parsed to String.
Copy code
...
"queryArguments" to cleanupFromUnsupportedTypes(requestContext.queryArgs)
...

private fun cleanupFromUnsupportedTypes(data: Map<String, Any>): Map<*, *> = objectMapper.readValue(objectMapper.writeValueAsString(data))
Of course it is not an ideal solution, so would be good if it would be possible to just provide own GSON client or “stringify” method implementation 🙂
n
We are not currently supporting custom serialization/deserialization, but sure we will add this to our backlog. 🙂