aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/jackc/pgx/example_json_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/jackc/pgx/example_json_test.go')
-rw-r--r--vendor/github.com/jackc/pgx/example_json_test.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/vendor/github.com/jackc/pgx/example_json_test.go b/vendor/github.com/jackc/pgx/example_json_test.go
new file mode 100644
index 0000000..c153415
--- /dev/null
+++ b/vendor/github.com/jackc/pgx/example_json_test.go
@@ -0,0 +1,43 @@
+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
+ }
+
+ if _, ok := conn.PgTypes[pgx.JsonOid]; !ok {
+ // No JSON type -- must be running against very old PostgreSQL
+ // Pretend it works
+ fmt.Println("John", 42)
+ 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
+}