Can I use kotlin serialization in kotlin script?
# scripting
e
Can I use kotlin serialization in kotlin script?
e
Thank you!
m
Most of the times, I use
kotlinx-serialization-json
without the compiler plugin. It's not as type safe but for scripts, it's often "good enough"
πŸ‘Œ 2
e
I need xml
m
You can do stuff like
Copy code
"{}".parseToJsonElement()
I'm assuming XML would have the same primitives
Gives you some
XMLNode
item
e
Let me try
m
Then you can cast/get children as needed
e
not much of a reason to use kxs over java.xml if you're not going to use the generated serializers though
m
true
e
Yeah, that is my next option is just take java library
m
one reason is that it's only one maven coordinates to remember πŸ˜„
e
java.xml module is part of the JDK, no maven coordinates at all πŸ˜›
πŸ‘ 1
m
Ahaha even better !
Requires Java 9 😱
e
oh it was there pre-modularization, just not as a module
m
πŸ‘
For some reason I developped some PTSD whenever I see
javax.
πŸ˜‚ 1
But that's fine, I'm working on it!
e
I don't think there's any object mapping built in. I guess Jackson would work if you need, since that's all reflection-based and not compile-time
πŸ‘ 1
m
FWIW, I just remembered I wrote this: https://github.com/martinbonnin/xoxo It's very simple, not tested a lot (I think I'm the only user πŸ˜„ ) but feedbacks are welcome!
v
You could even use serialization with serializers fine in a Kotlin script. You just cannot define new serializable objects that have default serilalizers provided by the compiler plugin as you cannot right now apply the compiler plugin. But you could have serializable objects with custom serializers and you can use classes that were compiled with the compiler plugin.
e
Oke people, thank you. I’m just trying to cook a quick scrip to automate translations of some different yaml files with DeepL
The XML is needed to prevent DeepL from attempting to translate integers
I already spent around a weekend or more on this problem and looks like I have to give up.
m
Put it back at to the end of the pet projects circular queue πŸ˜„
That is harder than I thought - I’m trying to translate every yaml scalar from a file, but some are html (so xml). The yaml structure differs per file in the different content folders, and on top DeepL has a limit on the request size.
i
@Vampire
You just cannot define new serializable objects that have default serilalizers provided by the compiler plugin as you cannot right now apply the compiler plugin.
I'm not sure I understand, what is missing. There are two tests in the Kotlin repo that run this script - https://github.com/JetBrains/kotlin/blob/1.8.20/libraries/tools/kotlin-main-kts-test/testData/hello-kotlinx-serialization.main.kts - which uses plugin to generate serializers. You can add the plugin either via CLI, if cli compiler is used, or via compiler configuration.
The ticket mentioned above probably cannot be assumed as solved, because there are still difficulties in obtaining and applying the plugin for the script scenario, but it seems incorrect to say that you cannot apply the plugin.
v
Yeah, you are right @ilya.chernikov. If you call
kotlinc
explicitly, or call the script from a bash script or similar, or call the script programmatically. But there is no way to make it work with calling
./hello-kotlinx-serialization.main.kts
. See the Ticket @ephemient linked above. Note: the shebang hack is not appropriate for general usage. You either require a non-standard environment variable or an absolute path, and more important using more than one argument in the shebang line must be avoided for general usage, it is undefined and not portable and will behave majority different on different systems.
e
you could probably pull off some sort of polyglot trick. for example,
Copy code
#!/bin/sh
//bin/true<<.
/*
.
# any shell script goes here
kotlin "$0" "$@"
exit $?
*/
// any Kotlin script goes here
println("Hello, world!")
where you can put an arbitrarily complex shell script in there that Kotlin will skip over
v
Oh, amazing. Ugly, but amazing. πŸ˜„
Except that I don't like the
.
as end of comment marker that confused me and that it does not work properly in Git Bash on Windows. πŸ˜„ There the
//bin/true
does not work and needs quite some time, probably because it searches for a computer called
bin
Ah, you can use three slashes, then it works on both
e
grrrr windows paths
Copy code
#!/bin/sh
""":""" /*
kotlin "$0" "$@"
exit $?
*/
println("Hello, world!")
this would also work, using shell built-ins
v
Yay, thought about how to do it with colon, but didn't think about that quote trick, "nice" πŸ˜„
Hm, no, it doesn't work with that
Doesn't compile, probably because of the
@file:DependsOn
that is now preceded by a String literal
e
oh I see, if you need annotations that is tricky
v
So it seems the "best" way might indeed be
Copy code
#!/bin/sh

///bin/true <<EOC
/*
EOC
kotlinc ...
exit $?
*/

...
Now the only thing left is that you need to have the path to the serialization plugin, or require an envrionment variable, or need to add some logic to download it or something similar. Could be so easy if the feature request would be implmented. πŸ˜„
e
"just" implement a maven resolver in shell and download the kotlin compiler and plugin πŸ€ͺ
v
Yep, exactly. πŸ˜„
But still a nice trick to have an executable called
my-cool-tool
that is a main.kts script πŸ™‚
Thx
p
Did you try just throwing it at chatGPT and say sth like: translate this to XYZ?
It’s almost identical to what humans do πŸ™ˆ https://gist.github.com/PaulWoitaschek/c5984431a4b52b4bab920364c29bd91d
e
Nice, it is second suggestion try to use ai for thisbtask
Promise to try soon
v
Well, chatGPT is a fancy toy and nothing more imho. Imo you can only ask it to do work you could also do yourself and know exactly what the result should look like to fix the non-sense it produces, but just are too lazy to write yourself from ground up. All tries to let chatGPT generate code so far that I've seen was producing more ore less non-sense. It produced totally wrong code or missed important parts it only added after you told it to, or used methods on APIs that do not exist and never did exist at all. Besides that its data base is older than 2 years which is really very long in software development terms.
But maybe for language translations it is better, idk πŸ˜„
e
it's prone to making stuff up, but so is DeepL
v
How is a translator prone to making things up?
e
I've seen it add context to translations that completely doesn't exist in the source
🧐 1
also https://medium.com/mlearning-ai/deepl-wont-translate-this-phrase-a8310fcb2520 shows it producing words that don't exist in the target
🧐 1
v
Ok, I guess I keep sticking with Google translate. :-D
e
Google Translate has failures too. the big thing with DeepL and ChatGPT is that their output tends to sound more fluent even when it's totally wrong
v
Yeah, which is a big problem. Even worse than people copying SO answers blindly.
p
It's fine, you just need a human going over it in the end
v
That's what I said. You can only use it for things you could have done yourself, so that you can see where the non-sense is and fix it. So if you let the tool translate something from Arabic to Russian, you need someone speaking both languages to fix up the result, that could have done the work himself right away.
e
Bjorn, I can clean room floor with vacuum cleaner just by myself or I can finish the corners after vaccum cleaner robot. That are both possible ways to finish that task.
v
Yes, that's exactly what I said. If you could have done it yourself but are too lazy or whatever, you can let the tool do it and fix its non-sense. But if you don't have arms or are blind (not speaking Arabic or Russian), then you cannot fix the non-sense and should not depend on the result.
And please don't get "lazy" wrong, it is a positive "lazy". I'm lazy too, otherwise I would use a text editor, not an IDE for example, or wouldn't write scripts for repetitive work.
291 Views