Add docs and example for JSON

pull/89/head
Jack Christensen 2015-09-04 14:00:21 -05:00
parent fff5b9759b
commit 5ea6b04624
4 changed files with 46 additions and 0 deletions

View File

@ -1,3 +1,7 @@
# Master
* Add JSON and JSONB type support (Joseph Glanville)
# 2.6.0 (September 3, 2015)
* Add inet and cidr type support

View File

@ -19,6 +19,7 @@ Pgx supports many additional features beyond what is available through database/
* Configurable connection pool with after connect hooks to do arbitrary connection setup
* PostgreSQL array to Go slice mapping for integers, floats, and strings
* Hstore support
* JSON and JSONB support
* Maps inet and cidr PostgreSQL types to net.IPNet
* Large object support

5
doc.go
View File

@ -137,6 +137,11 @@ pgx includes an Hstore type and a NullHstore type. Hstore is simply a
map[string]string and is preferred when the hstore contains no nulls. NullHstore
follows the Null* pattern and supports null values.
JSON and JSONB Mapping
pgx includes built-in support to marshal and unmarshal between Go types and
the PostgreSQL JSON and JSONB.
Custom Type Support
pgx includes support for the common data types like integers, floats, strings,

36
example_json_test.go Normal file
View File

@ -0,0 +1,36 @@
package pgx_test
import (
"fmt"
"github.com/jackc/pgx"
)
func Example_JSON() {
conn, err := pgx.Connect(*defaultConnConfig)
if err != nil {
fmt.Printf("Unable to establish connection: %v", err)
return
}
type person struct {
Name string `json:"name"`
Age int `json:"age"`
}
input := person{
Name: "John",
Age: 42,
}
var output person
err = conn.QueryRow("select $1::json", input).Scan(&output)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(output.Name, output.Age)
// Output:
// John 42
}