dt250827
dt250827
2018-08-31 10:24
浏览 15
已采纳

更改以JSON对象的字节表示形式归档

I write A object to a file f.

a := A{42}
bytes, _ := json.MarshalIndent(a, "", "\t")
f.Write(bytes)

Where A looks like:

type A struct {
    A int `json:"a"`
} 

Then I change field of this object and write it to the file:

a.A = 666
f.Write(bytes)

As a result, I see only

{
    "a": 42
}{
    "a": 42
}

While I expected:

{
    "a": 42
}{
    "a": 666
}

I know that I can overcome it by using json.MarshalIndent again. But I need to do a lot of (~10^6) writings to the file, so using json.MarshalIndent again and again seems to be a heavy task.

How can I directly change a bytes variable?

Code is located at https://play.golang.org/p/8CMpwehMidR

图片转代码服务由CSDN问答提供 功能建议

我将 A 对象写入文件 f 。 / p>

  a:= A {42} 
bytes,_:= json.MarshalIndent(a,“”,“ \ t”)
f.Write(bytes)
 <  / code>  
 
 

A 的位置:

  type A struct {
 A int`json  :“ a”`
} 
   
 
 

然后我更改该对象的字段并将其写入文件:

   aA = 666 
f。写入(字节)
   
 
 

结果,我只能看到

  {
“ a”:42 
} {
“ a”:42 
} 
   
 
 

我期望:

  {
“ a”:42 
} {
“ a”:666 
} 
   
 
 

我知道 我可以通过再次使用 json.MarshalIndent 克服它。 但是我需要对文件进行大量(〜10 ^ 6)的编写,因此一次又一次地使用 json.MarshalIndent 似乎是一项繁重的任务。

如何直接更改 bytes 变量?

代码位于 https://play.golang.org/p/8CMpwehMidR

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhang1987
    dongzhang1987 2018-08-31 10:53
    已采纳

    You have no choice but to marshal repeatedly. Use a *json.Encoder for improved ergonomics and efficiency:

    package main
    
    import (
        "encoding/json"
        "log"
        "os"
    )
    
    type A struct {
        A int `json:"a"`
    }
    
    func main() {
        f, err := os.Create("foo.json")
        if err != nil {
            log.Fatal(err)
        }
        defer f.Close()
    
        enc := json.NewEncoder(f)
        enc.SetIndent("", "\t")
    
        a := A{42} // Using a pointer may improve performance if A is large.
        enc.Encode(a)
    
        a.A = 666
        enc.Encode(a)
    }
    

    Wrapping the file with a buffered writer may also improve performance, depending on how quickly you can compute successive values for the As and how fast the disk is.

    点赞 评论
  • dryb38654
    dryb38654 2018-08-31 11:09

    you can use the standard library to replace bytes within the given bytes slice.

    https://golang.org/pkg/bytes/#Replace

    package main
    
    import (
        "bufio"
        "bytes"
        "encoding/json"
        "os"
    )
    
    type A struct {
        A int `json:"a"`
    }
    
    func main() {
        out := bufio.NewWriterSize(os.Stdout, 20)
        // defer out.Flush() // commented for demonstration purpose. Uncomment this to finalize the flush.
        a := A{42}
        b, _ := json.MarshalIndent(a, "", "\t")
        out.Write(b)
        b = bytes.Replace(b, []byte("42"), []byte("666"), -1)
        out.Write(b)
    }
    

    It is not recommended to do it, but ultimately this is possible.

    I included a buffered writer for demonstration of others answers and comments, don t forget to flush it.

    点赞 评论