programing

대규모 스트림 JSON 디코딩

mytipbox 2023. 3. 14. 21:18
반응형

대규모 스트림 JSON 디코딩

파일(「file.json」)에 대량의 JSON 어레이를 보존하고 있기 때문에, 어레이를 반복해 각 요소에 대해 몇개의 조작을 실시할 필요가 있습니다.

err = json.Unmarshal(dat, &all_data)

메모리 부족의 원인이 됩니다.모든 것을 먼저 메모리에 로드하기 때문일 것입니다.

JSON 요소를 요소별로 스트리밍하는 방법이 있습니까?

문서에는 이러한 종류의 예가 있습니다.

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "strings"
)

func main() {
    const jsonStream = `
                [
                    {"Name": "Ed", "Text": "Knock knock."},
                    {"Name": "Sam", "Text": "Who's there?"},
                    {"Name": "Ed", "Text": "Go fmt."},
                    {"Name": "Sam", "Text": "Go fmt who?"},
                    {"Name": "Ed", "Text": "Go fmt yourself!"}
                ]
            `
    type Message struct {
        Name, Text string
    }
    dec := json.NewDecoder(strings.NewReader(jsonStream))

    // read open bracket
    t, err := dec.Token()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%T: %v\n", t, t)

    // while the array contains values
    for dec.More() {
        var m Message
        // decode an array value (Message)
        err := dec.Decode(&m)
        if err != nil {
            log.Fatal(err)
        }

        fmt.Printf("%v: %v\n", m.Name, m.Text)
    }

    // read closing bracket
    t, err = dec.Token()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%T: %v\n", t, t)

}

따라서 코멘트의 제안대로 "encoding/json" 스트리밍 API를 사용하여 한 번에 한 문자열을 읽을 수 있습니다.

r := ... // get some io.Reader (e.g. open the big array file)
d := json.NewDecoder(r)
// read "["
d.Token()
// read strings one by one
for d.More() {
    s, _ := d.Token()
    // do something with s which is the newly read string
    fmt.Printf("read %q\n", s)
}
// (optionally) read "]"
d.Token()

간단하게 하기 위해서, 실장할 필요가 있는 에러 처리는 생략하고 있습니다.

언급URL : https://stackoverflow.com/questions/31794355/decode-large-stream-json

반응형