Skip to content

Commit

Permalink
Added support for JSON serialization - closes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 4, 2024
1 parent b3e5fed commit 88d27fa
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.2.0 (unreleased)

- Added support for `halfvec` and `sparsevec` types
- Added support for JSON serialization
- Improved performance of serialization
- Dropped support for Go < 1.21

Expand Down
11 changes: 11 additions & 0 deletions halfvec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgvector
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -75,3 +76,13 @@ var _ driver.Valuer = (*HalfVector)(nil)
func (v HalfVector) Value() (driver.Value, error) {
return v.String(), nil
}

// MarshalJSON implements the json.Marshaler interface.
func (v HalfVector) MarshalJSON() ([]byte, error) {
return json.Marshal(v.vec)
}

// UnmarshalJSON implements the json.Unmarshaler interface.
func (v *HalfVector) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &v.vec)
}
31 changes: 31 additions & 0 deletions halfvec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pgvector_test

import (
"encoding/json"
"reflect"
"testing"

"github.com/pgvector/pgvector-go"
)

func TestHalfVectorMarshal(t *testing.T) {
vec := pgvector.NewHalfVector([]float32{1, 2, 3})
data, err := json.Marshal(vec)
if err != nil {
panic(err)
}
if string(data) != `[1,2,3]` {
t.Errorf("Bad marshal")
}
}

func TestHalfVectorUnmarshal(t *testing.T) {
var vec pgvector.HalfVector
err := json.Unmarshal([]byte(`[1,2,3]`), &vec)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(vec.Slice(), []float32{1, 2, 3}) {
t.Errorf("Bad unmarshal")
}
}
11 changes: 11 additions & 0 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pgvector
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -75,3 +76,13 @@ var _ driver.Valuer = (*Vector)(nil)
func (v Vector) Value() (driver.Value, error) {
return v.String(), nil
}

// MarshalJSON implements the json.Marshaler interface.
func (v Vector) MarshalJSON() ([]byte, error) {
return json.Marshal(v.vec)
}

// UnmarshalJSON implements the json.Unmarshaler interface.
func (v *Vector) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &v.vec)
}
31 changes: 31 additions & 0 deletions vector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pgvector_test

import (
"encoding/json"
"reflect"
"testing"

"github.com/pgvector/pgvector-go"
)

func TestVectorMarshal(t *testing.T) {
vec := pgvector.NewVector([]float32{1, 2, 3})
data, err := json.Marshal(vec)
if err != nil {
panic(err)
}
if string(data) != `[1,2,3]` {
t.Errorf("Bad marshal")
}
}

func TestVectorUnmarshal(t *testing.T) {
var vec pgvector.Vector
err := json.Unmarshal([]byte(`[1,2,3]`), &vec)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(vec.Slice(), []float32{1, 2, 3}) {
t.Errorf("Bad unmarshal")
}
}

0 comments on commit 88d27fa

Please sign in to comment.