When will the official encoding/json package support parsing json5?

643 views
Skip to first unread message

fliter

unread,
Mar 14, 2022, 12:07:37 PM3/14/22
to golang-nuts
I need to add comments to json, but now json version does not support it

Rob Muhlestein

unread,
Mar 17, 2022, 8:59:21 PM3/17/22
to golang-nuts
It is my sincere hope that Go will never support anything as poorly designed as JSON5, using reflection is already slow enough. Comments were never intended for JSON and never should be added, ever. But since most discerning development shops are moving to Protobuf for everything that matters, perhaps that would be less of a problem.

ben...@gmail.com

unread,
Mar 18, 2022, 8:03:17 PM3/18/22
to golang-nuts
Yeah, Go's encoding/json will almost certainly never support json5. However, one very simple approach: if you're using JSON for a config file and just need ability to add // line comments, you can just write a simple transformer which reads the file line by line and filters out lines that start with "//" (optionally preceded by whitespace), and then send the result to encoding/json.

christoph...@gmail.com

unread,
Mar 20, 2022, 9:16:20 AM3/20/22
to golang-nuts
You might want to try qjson that I wrote a year ago (https://github.com/qjson). 
It's basically just one function that converts qjson text into json. Qjson is inspired by hjson, but extend it in many ways. 
It is convenient for config files. 

Maybe I extend it a bit too far by supporting unquoted strings or single quoted strings. Lol

Brian Candler

unread,
Mar 20, 2022, 10:27:05 AM3/20/22
to golang-nuts
If you're going down that route, you could also consider jsonnet (which does have a Go implementation)

It will almost certainly be much more powerful than you need, but it *is* another superset of JSON which includes comments.

jan.f...@gmail.com

unread,
Mar 20, 2022, 3:00:07 PM3/20/22
to golang-nuts
Just googled for "json5 golang" and there seem to be several 3rd party implementations available.
Can you not use one of those?

Nigel Tao

unread,
Mar 20, 2022, 10:50:03 PM3/20/22
to jan.f...@gmail.com, golang-nuts
On Mon, Mar 21, 2022 at 6:00 AM jan.f...@gmail.com <jan.f...@gmail.com> wrote:
Just googled for "json5 golang" and there seem to be several 3rd party implementations available.
Can you not use one of those?

Reply all
Reply to author
Forward
0 new messages