# go: json-iterator

By [Primrose](https://paragraph.com/@primrose) · 2023-08-19

---

jsoniter
========

go에서 어떤 구조체를 byte array로 만들때 `encoding/json` 라이브러리를 많이들 사용한다.

json-iterator 패키지는 `encoding/json` 패키지와 완벽하게 호환되며, 성능은 훨씬 좋다고 한다.

물론 `encoding/json`이 표준 패키지이기 때문에 별 일 없으면 그대로 쓰면 된다.

    package main
    
    import (
        jsoniter "github.com/json-iterator/go"
    )
    
    func main() {
        var data = struct {
            Name string
            Age  int
        }{Name: "John Doe", Age: 20}
    
        var json = jsoniter.ConfigCompatibleWithStandardLibrary
    
        b, err := json.Marshal(data)
        if err != nil {
            panic(err)
        }
    
        println(string(b))
    }
    

사용법은 위와 같이 사용하면 된다.

jsoniter의 `ConfigCompatibleWithStandardLibrary` 를 해석해보면 딱봐도 완전호환이 되는 라이브러리라는 뜻이다. 이 외에도 여러 옵션이 있다.

`ConfigCompatibleWithStandardLibrary`는 아래와 같이 생겼다.

    // ConfigCompatibleWithStandardLibrary tries to be 100% compatible with standard library behavior
    var ConfigCompatibleWithStandardLibrary = Config{
        EscapeHTML:             true,
        SortMapKeys:            true,
        ValidateJsonRawMessage: true,
    }.Froze()

---

*Originally published on [Primrose](https://paragraph.com/@primrose/go-json-iterator)*
