Opt for json.MarshalIndent for Improved JSON Readability
knbr13 opened this issue · comments
Hi Link-
Wouldn't it be better to utilize json.MarshalIndent
instead of json.Marshal
to enhance the readability of the JSON output?
In certain contexts, such as when developing web servers in Go, there can be performance considerations favoring the use of json.Marshal
due to its lower memory and time overhead. However, for projects like gh-stars
—intended for single-user use rather than handling concurrent requests like a web server—employing json.MarshalIndent
seems entirely reasonable, doesn't it?
Current Output Using json.Marshal
:
gh stars -u abdullah-alaadine -f go -j
[{"name":"jwt-go","full_name":"dgrijalva/jwt-go","private":false,"html_url":"https://github.com/dgrijalva/jwt-go","Owner":
{"login":"dgrijalva","url":"https://api.github.com/users/dgrijalva"},"description":"ARCHIVE - Golang implementation of JSON Web
Tokens (JWT). This project is now maintained at:","fork":false,"stargazers_count":10688,"topics":[]},{"name":"need-for-speed-in-
golang","full_name":"bek-shoyatbekov/need-for-speed-in-golang","private":false,"html_url":"https://github.com/bek-shoyatbekov
/need-for-speed-in-golang","Owner":{"login":"bek-shoyatbekov","url":"https://api.github.com/users/bek-
shoyatbekov"},"description":"just out of curiousity ","fork":false,"stargazers_count":2,"topics":[]},{"name":"gore","full_name":"x-
motemen/gore","private":false,"html_url":"https://github.com/x-motemen/gore","Owner":{"login":"x-motemen","url":"https:
//api.github.com/users/x-motemen"},"description":" Yet another Go REPL that works nicely. Featured with line editing, code
completion, and more.","fork":false,"stargazers_count":4903,"topics":[]},{"name":"build-your-own-x","full_name":"codecrafters-
io/build-your-own-x","private":false,"html_url":"https://github.com/codecrafters-io/build-your-own-x","Owner":{"login":"codecrafters-
io","url":"https://api.github.com/users/codecrafters-io"},"description":"Master programming by recreating your favorite technologies
from scratch.","fork":false,"stargazers_count":222565,"topics":["awesome-list","free","programming","tutorial-code","tutorial-
exercises","tutorials"]},{"name":"go","full_name":"golang/go","private":false,"html_url":"https://github.com/golang/go","Owner":
{"login":"golang","url":"https://api.github.com/users/golang"},"description":"The Go programming
language","fork":false,"stargazers_count":115216,"topics":["go","golang","language","programming-language"]},
{"name":"hugo","full_name":"gohugoio/hugo","private":false,"html_url":"https://github.com/gohugoio/hugo","Owner":
{"login":"gohugoio","url":"https://api.github.com/users/gohugoio"},"description":"The world’s fastest framework for building
websites.","fork":false,"stargazers_count":69490,"topics":["blog-engine","cms","content-management-system","documentation-
tool","go","hugo","static-site-generator"]},{"name":"design-patterns-in-go","full_name":"araujo88/design-patterns-in-
go","private":false,"html_url":"https://github.com/araujo88/design-patterns-in-go","Owner":{"login":"araujo88","url":"https:
//api.github.com/users/araujo88"},"description":"A list of design patterns implemented in Go with REAL WORLD
examples!","fork":false,"stargazers_count":32,"topics":["design-patterns","design-patterns-golang"]},{"name":"go-disposable-
email","full_name":"go-nerds/go-disposable-email","private":false,"html_url":"https://github.com/go-nerds/go-disposable-
email","Owner":{"login":"go-nerds","url":"https://api.github.com/users/go-nerds"},"description":"🌟 This small tool is to generate a
disposable temporary email using different domains quickly in 1 second, receive your mail and attachments, and save them to a
folder! 🪄🔮","fork":false,"stargazers_count":2,"topics":["1secmail","cybersecurity","disposable","disposable-email","disposable-
emails","go","golang","temporary-email"]},{"name":"go-checksums","full_name":"go-nerds/go-
checksums","private":false,"html_url":"https://github.com/go-nerds/go-checksums","Owner":{"login":"go-nerds","url":"https:
//api.github.com/users/go-nerds"},"description":"It's a small utility used for getting a checksum for any
file.","fork":false,"stargazers_count":2,"topics":["checksum","checksum-
calculation","checksums","cybersecurity","filehash","fileinfo","fileinformation","go","golang"]},{"name":"go-locator","full_name":"go-
nerds/go-locator","private":false,"html_url":"https://github.com/go-nerds/go-locator","Owner":{"login":"go-nerds","url":"https:
//api.github.com/users/go-nerds"},"description":"Go-locator is a Go package designed to get offline information about any IP
address and save it to a file!","fork":false,"stargazers_count":3,"topics":["cybersecurity","go","golang","information-
retrieval","ipaddress","ipaddress-tracker"]}]
Output Using json.MarshalIndent
:
./gh-stars -u abdullah-alaadine -f "curl" -j
[
{
"name": "curlie",
"full_name": "rs/curlie",
"private": false,
"html_url": "https://github.com/rs/curlie",
"Owner": {
"login": "rs",
"url": "https://api.github.com/users/rs"
},
"description": "The power of curl, the ease of use of httpie.",
"fork": false,
"stargazers_count": 2442,
"topics": [
"api-client",
"cli",
"curl",
"developer-tools",
"go",
"http-client",
"httpie",
"terminal",
"webdevelopment"
]
}
]
@abdullah-alaadine - Create a PR and I'll review it :) Sorry I didn't give much attention to the existing one. I'll try my best to get to it when I can 🙏
okay,
I updated the code to use json.MarshalIndent
, but it turned out, that the tests will fail, because in the tests, the expected value is a json output of hardcoded number of spaces or newlines, so updating the code without updating the tests will fail.
I will try to figure out how to modify the tests in order to test the results regardless of the number of spaces and newlines, then I will create a PR again.
Thanks!