-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
187 lines (165 loc) · 3.91 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package goquery
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"reflect"
"time"
"github.com/stoewer/go-strcase"
)
var comma []byte = []byte(",")
var openobj []byte = []byte("{")
var closeobj []byte = []byte("}")
var openarray []byte = []byte("[")
var closearray []byte = []byte("]")
type jsonNullString struct {
sql.NullString
}
func (v jsonNullString) MarshalJSON() ([]byte, error) {
if !v.Valid {
return json.Marshal(nil)
}
return json.Marshal(v.String)
}
type jsonNullInt32 struct {
sql.NullInt32
}
func (v jsonNullInt32) MarshalJSON() ([]byte, error) {
if !v.Valid {
return json.Marshal(nil)
}
return json.Marshal(v.Int32)
}
type jsonNullInt64 struct {
sql.NullInt64
}
func (v jsonNullInt64) MarshalJSON() ([]byte, error) {
if !v.Valid {
return json.Marshal(nil)
}
return json.Marshal(v.Int64)
}
type jsonNullFloat64 struct {
sql.NullFloat64
}
func (v jsonNullFloat64) MarshalJSON() ([]byte, error) {
if !v.Valid {
return json.Marshal(nil)
}
return json.Marshal(v.Float64)
}
type jsonNullTime struct {
sql.NullTime
Fmt string
}
func (v jsonNullTime) MarshalJSON() ([]byte, error) {
if !v.Valid {
return json.Marshal(nil)
}
if v.Fmt != "" {
//fmt.Println(v.Time.Format(v.Fmt))
return json.Marshal(v.Time.Format(v.Fmt))
}
return json.Marshal(v.Time)
}
var jsonNullStringType = reflect.TypeOf(jsonNullString{})
var jsonNullInt32Type = reflect.TypeOf(jsonNullInt32{})
var jsonNullInt64Type = reflect.TypeOf(jsonNullInt64{})
var jsonNullFloat64Type = reflect.TypeOf(jsonNullFloat64{})
var jsonNullTimeType = reflect.TypeOf(jsonNullTime{})
var nullStringType = reflect.TypeOf(sql.NullString{})
var nullI32Type = reflect.TypeOf(sql.NullInt32{})
var nullI64Type = reflect.TypeOf(sql.NullInt64{})
var nullF64Type = reflect.TypeOf(sql.NullFloat64{})
var nullTimeType = reflect.TypeOf(sql.NullTime{})
var i32 int32
var i32Type = reflect.TypeOf(i32)
var i64 int64
var i64Type = reflect.TypeOf(i64)
var f64 float64
var f64Type = reflect.TypeOf(f64)
var str string
var strType = reflect.TypeOf(str)
var dte time.Time
var dateType = reflect.TypeOf(dte)
func RowsToJSON(builder io.Writer, rows Rows, toCamelCase bool, isArray bool, dateFormat string, omitNull bool) error {
columns, err := rows.Columns()
if err != nil {
return fmt.Errorf("Column error: %v", err)
}
tt, err := rows.ColumnTypes()
if err != nil {
return fmt.Errorf("Column type error: %v", err)
}
types := make([]reflect.Type, len(tt))
for i, tp := range tt {
//st := tp.ScanType()
if tp == nil {
return fmt.Errorf("Scantype is null for column: %v", err)
}
switch tp {
case strType, nullStringType:
types[i] = jsonNullStringType
case i32Type, nullI32Type:
types[i] = jsonNullInt32Type
case i64Type, nullI64Type:
types[i] = jsonNullInt64Type
case f64Type, nullF64Type:
types[i] = jsonNullFloat64Type
case dateType, nullTimeType:
types[i] = jsonNullTimeType
default:
types[i] = tp
}
}
values := make([]interface{}, len(tt))
//var builder strings.Builder
count := 0
if isArray {
builder.Write(openarray)
}
for rows.Next() {
if count > 0 {
builder.Write(comma)
}
builder.Write(openobj)
for i := range values {
nv := reflect.New(types[i]) //.Interface()
if types[i] == jsonNullTimeType {
if dateFormat != "" {
nv.Elem().Field(1).SetString(dateFormat)
}
}
values[i] = nv.Interface()
}
err = rows.Scan(values...)
if err != nil {
return fmt.Errorf("failed to scan values: %v", err)
}
for i, v := range values {
jsonb, err := json.Marshal(v)
if err != nil {
return err
}
jsons := string(jsonb)
if omitNull && jsons == "null" {
continue
}
if i > 0 {
builder.Write(comma)
}
fieldName := columns[i]
if toCamelCase {
fieldName = strcase.LowerCamelCase(fieldName)
}
builder.Write([]byte(fmt.Sprintf(`"%s":%s`, fieldName, jsons)))
}
builder.Write(closeobj)
count++
}
if isArray {
builder.Write(closearray)
}
return nil
}