https://kotlinlang.org logo
Title
s

Stylianos Gakis

05/24/2022, 4:16 PM
A follow-up on the original workaround to using the LocalDate in a testBuilder here When I do
JavaLocalDateAdapter.toJsonString(LocalDate.of(2021, 4, 11))
I get back the string
""2021-04-11""
. Now if I do a test case like this:
val localDate = LocalDate.of(2021, 4, 11)
val jsonRepresentation = JavaLocalDateAdapter.toJsonString(localDate)
val buffer = Buffer().write(jsonRepresentation.encodeUtf8())
val jsonReader = BufferedSourceJsonReader(buffer)
val parsedAgain = JavaLocalDateAdapter.fromJson(jsonReader, CustomScalarAdapters.Empty)
assertThat(parsedAgain).isEqualTo(localDate)
It passes fine as expected. However when I am actually using it on the builder, it crashes at the adapter, since it tries to then parse
""2021-04-11""
directly, and LocalDate fails to do so. In this line in particular, the
nextString!!
returns the value with those double quotes and it fails. In the code snippet I put above, the encodeUtf8() part seems to turn it into a string with a single quote which makes this work, and maybe this isn’t happening in the test builders code? I am not quite sure what piece of the puzzle is going wrong, so I am posting this here in case you may have a better idea. Edit: For people who might read this at some future time and might be confused. I had simply misread that it was giving me back a String with double quoted strings, but it was just that in the debugger view in IntelliJ, to indicate a string it wraps it with quotations in the UI. The String I got back was in fact
"2021-04-11"
as expected and the workaround for the test builders was to strip the ‘“’ from the start and the end of the resulting string. This resulted in the String
2021-04-11
which was correctly parsed from the LocalDate.parse
👀 1
m

mbonnin

05/24/2022, 4:25 PM
Aaaarg looks like
toJsonString
puts extra quotes.. Which might be what we want actually
This screams for being able to input kotlin instances directly...
You could manually remove the leading and trailing
"
but that's far from ideal
s

Stylianos Gakis

05/24/2022, 5:54 PM
Right, of course. We want the toJsonString to behave that way, and we don't want to need to specify strings with double quotes on every single thing in the builders, so it's a mismatch between the two. That's completely fine though, just stripping the "" is an easy enough solution. Maybe make a local extension function with a name like .toJsonStringForBuilders or something 😂 Thank you again! 🤗🤗