manually generate json with struct fields in the correct order
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
34
main.go
34
main.go
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
@@ -8,6 +9,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -396,9 +398,37 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Output final results in JSON
|
// Output final results in JSON
|
||||||
|
// TODO - manually generate JSON since Marshal doesn't care about field ordering
|
||||||
if len(combined) > 0 {
|
if len(combined) > 0 {
|
||||||
j, _ := json.Marshal(combined)
|
var b []byte
|
||||||
fmt.Println(string(j))
|
buf := bytes.NewBuffer(b)
|
||||||
|
buf.WriteRune('{')
|
||||||
|
l := len(combined)
|
||||||
|
for i, entry := range combined {
|
||||||
|
|
||||||
|
v := reflect.ValueOf(entry)
|
||||||
|
t := v.Type()
|
||||||
|
|
||||||
|
for j := 0; j < v.NumField(); j++ {
|
||||||
|
field := v.Field(j)
|
||||||
|
fieldType := t.Field(j)
|
||||||
|
|
||||||
|
km, _ := json.Marshal(fieldType.Name)
|
||||||
|
buf.Write(km)
|
||||||
|
buf.WriteRune(':')
|
||||||
|
|
||||||
|
vm, _ := json.Marshal(field.Interface())
|
||||||
|
buf.Write(vm)
|
||||||
|
if i != l-1 {
|
||||||
|
buf.WriteRune(',')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buf.WriteRune('}')
|
||||||
|
fmt.Println(buf.String())
|
||||||
|
|
||||||
|
//j, _ := json.Marshal(combined)
|
||||||
|
//fmt.Println(string(j))
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("{}")
|
fmt.Println("{}")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user