go
go copied to clipboard
Unmarshal with indentation only indent the first level
Hi,
I tested it with standard json it works fine
package main
import (
"encoding/json"
"fmt"
"log"
)
var (
// json = jsoniter.ConfigCompatibleWithStandardLibrary
)
func main() {
data := `{"a": 23, "b":"value b", "c": {"d": "d value", "e": 544, "f":{"g": 43, "h": "value og h"} } }`
var v interface{}
json.Unmarshal([]byte(data), &v)
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
/* output
{
"a": 23,
"b": "value b",
"c": {
"d": "d value",
"e": 544,
"f": {
"g": 43,
"h": "value og h"
}
}
}
*/
However the following does not produce sub level indentation
package main
import (
"fmt"
"log"
jsoniter "github.com/json-iterator/go"
)
var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
)
func main() {
fmt.Println("vim-go")
data := `{"a": 23, "b":"value b", "c": {"d": "d value", "e": 544, "f":{"g": 43, "h": "value og h"} } }`
var v interface{}
json.Unmarshal([]byte(data), &v)
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
/* output
{
"a": 23,
"b": "value b",
"c": {
"d": "d value",
"e": 544,
"f": {
"g": 43,
"h": "value og h"
}
}
}
*/
It does not looks nice for jsoniter part :D
Not sure how much effort to fix this or is it known bug but I wish that could be fixed.
Thanks
There is a bug for indent. Because the substream doesn't set idention with stream's idention. In my fork https://github.com/simonwu-os/json-iterator-go/tree/owned, I fixed it. You can try to use "github.com/simonwu-os/json-iterator-go v1.1.5 " to check the issue is fixed or not.
func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) { stream.WriteObjectStart() mapIter := encoder.mapType.UnsafeIterate(ptr) subStream := stream.cfg.BorrowStream(nil) subStream.Attachment = stream.Attachment
///added by simon. fix sorted map with indent bug 2023.1.24 cur_idention := stream.indention subStream.indention = cur_idention ///end of added 2023.1.24.
subIter := stream.cfg.BorrowIterator(nil) }