diff --git a/bloom.go b/bloom.go index 7da9265..89dbe24 100644 --- a/bloom.go +++ b/bloom.go @@ -326,7 +326,7 @@ type bloomFilterJSON struct { } // MarshalJSON implements json.Marshaler interface. -func (f *BloomFilter) MarshalJSON() ([]byte, error) { +func (f BloomFilter) MarshalJSON() ([]byte, error) { return json.Marshal(bloomFilterJSON{f.m, f.k, f.b}) } diff --git a/bloom_test.go b/bloom_test.go index d66c99a..3f5ea2b 100644 --- a/bloom_test.go +++ b/bloom_test.go @@ -1,12 +1,15 @@ package bloom import ( + "fmt" "bytes" "encoding/binary" "encoding/gob" "encoding/json" "math" "testing" + + "github.com/bits-and-blooms/bitset" ) // This implementation of Bloom filters is _not_ @@ -291,6 +294,35 @@ func TestMarshalUnmarshalJSON(t *testing.T) { if err != nil { t.Fatal(err.Error()) } + fmt.Println(string(data)) + + var g BloomFilter + err = json.Unmarshal(data, &g) + if err != nil { + t.Fatal(err.Error()) + } + if g.m != f.m { + t.Error("invalid m value") + } + if g.k != f.k { + t.Error("invalid k value") + } + if g.b == nil { + t.Fatal("bitset is nil") + } + if !g.b.Equal(f.b) { + t.Error("bitsets are not equal") + } +} + + +func TestMarshalUnmarshalJSONValue(t *testing.T) { + f:= BloomFilter{1000, 4, bitset.New(1000)} + data, err := json.Marshal(f) + if err != nil { + t.Fatal(err.Error()) + } + fmt.Println(string(data)) var g BloomFilter err = json.Unmarshal(data, &g) diff --git a/go.mod b/go.mod index 4e272f7..64d7e4d 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/bits-and-blooms/bloom/v3 go 1.16 require ( - github.com/bits-and-blooms/bitset v1.8.0 + github.com/bits-and-blooms/bitset v1.9.0 github.com/twmb/murmur3 v1.1.6 ) diff --git a/go.sum b/go.sum index 80abfd2..119b90f 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ github.com/bits-and-blooms/bitset v1.3.1 h1:y+qrlmq3XsWi+xZqSaueaE8ry8Y127iMxlMfqcK8p0g= github.com/bits-and-blooms/bitset v1.3.1/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA= -github.com/bits-and-blooms/bitset v1.8.0 h1:FD+XqgOZDUxxZ8hzoBFuV9+cGWY9CslN6d5MS5JVb4c= -github.com/bits-and-blooms/bitset v1.8.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= +github.com/bits-and-blooms/bitset v1.9.0 h1:g1YivPG8jOtrN013Fe8OBXubkiTwvm7/vG2vXz03ANU= +github.com/bits-and-blooms/bitset v1.9.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8= github.com/twmb/murmur3 v1.1.6 h1:mqrRot1BRxm+Yct+vavLMou2/iJt0tNVTTC0QoIjaZg= github.com/twmb/murmur3 v1.1.6/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ=