steveyen / gkvlite

Simple, ordered, key-value persistence library for the Go Language
MIT License
263 stars 32 forks source link

What does MarshalJSON return? #11

Closed hawkhsieh closed 8 years ago

hawkhsieh commented 8 years ago

I wrote a code to test the JSON output. But I got a strange result. Here is the code

package main

import(
        "os"
        "fmt"
        "github.com/steveyen/gkvlite"
)

func main(){
    f,_ := os.Create( "test.db" )
    s,_ := gkvlite.NewStore(f)
    c := s.SetCollection("car", nil)
    c.Set([]byte("Honda"), []byte("good"))
    s.Flush()
    f.Sync()

    b,_ := c.MarshalJSON()
    fmt.Printf( string(b) )
    s.Close()
    f.Close()

}

Then output is

{"o":23,"l":52}

What does this mean? I expect the MarshalJSON function returns a Json maybe like

{"Honda":"good"}
steveyen commented 8 years ago

Hi - that "o" and "l" fields refer to offset of bytes and length of bytes of the collection header in the gkvlite file.

To get all the data from a collection, you'll instead need to iterate through the collection.