From 5ea6b0462409ac7b14116a7f24f3881d169ff734 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 4 Sep 2015 14:00:21 -0500 Subject: [PATCH] Add docs and example for JSON --- CHANGELOG.md | 4 ++++ README.md | 1 + doc.go | 5 +++++ example_json_test.go | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 example_json_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a92de90..cd24a1d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 1357f1f2..8d579e5c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/doc.go b/doc.go index d8fec79d..8b6cf2ff 100644 --- a/doc.go +++ b/doc.go @@ -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, diff --git a/example_json_test.go b/example_json_test.go new file mode 100644 index 00000000..113c38d9 --- /dev/null +++ b/example_json_test.go @@ -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 +}