Last active
September 14, 2023 18:33
-
-
Save roeniss/9f1c2c1220ceb926f19acd36a1e1ca30 to your computer and use it in GitHub Desktop.
convert kotlin data class toString into json in terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# motivation: it's not easy to look println(dataClass) | |
# | |
# example: | |
# input: Movie(title=Inception, year=2010, director=Person(firstName=Christopher, lastName=Nolan)) | |
# output: { | |
# "title": "Inception", | |
# "year": "2010", | |
# "director": { | |
# "firstName": "Christopher", | |
# "lastName": "Nolan" | |
# } | |
# } | |
# | |
# note: I used gnu-sed (brew install gnu-sed) on macOS | |
echo "$DATA" \ | |
| sed -E 's/\w+\(/\(/g' \ | |
| sed -E 's/=/"="/g' \ | |
| sed -E 's/, /", "/g' \ | |
| sed -E 's/\(/\{"/g' \ | |
| sed -E 's/(\w)\)/\1"\)/g' \ | |
| sed -E 's/\)/\}/g' \ | |
| sed -E 's/="\{/:\{/g' \ | |
| sed -E 's/\}"/\}/g' \ | |
| sed -E 's/=/:/g' \ | |
| jq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: this snippet has many caveats, so I don't use it anymore.