diff --git a/.gitignore b/.gitignore index adf8f72..de7a4ff 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ # Go workspace file go.work +json2nats diff --git a/default.json b/default.json new file mode 100644 index 0000000..f9e452a --- /dev/null +++ b/default.json @@ -0,0 +1,24 @@ +{ + "src": "ZDMS.demo", + "topic": "topic.demo", + "content": { + "areas": [3, 5, 6, 8, 9], + "current": 3, + "local": 1, + "total": 39, + "printer": 1, + "card": 0, + "bus-op": 2, + "other": 0, + "details": [ + { + "type": "10L水炮", + "count": 3 + }, + { + "type": "20L水炮", + "count": 3 + } + ] + } +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b6ffed3 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module json2nats + +go 1.21.3 diff --git a/main.go b/main.go new file mode 100644 index 0000000..1d1eed2 --- /dev/null +++ b/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "flag" + "fmt" + "os" +) + +func main() { + var fi, fo string + flag.Usage = func() { + fmt.Println("json2nats version v0.1") + flag.PrintDefaults() + } + flag.StringVar(&fi, "i", "default.json", "Json file name") + flag.StringVar(&fo, "o", "", "Output file name") + flag.Parse() + + b, err := os.ReadFile(fi) + if err != nil { + panic(err) + } + fmt.Println("[OK] Read json file:", fi) + + var stri, stro string + stri = string(b) + for i := range stri { + switch stri[i] { + case ' ': + case '\n': + case '\r': + // do nothing. + case '"': + stro += `\"` + case ',': + stro += `, ` + default: + stro += string(stri[i]) + } + } + + fmt.Println("[OK] Make nats tool string:") + + if fo != "" { + err = os.WriteFile(fo, []byte(stro), 0644) + if err != nil { + panic(err) + } + fmt.Println("[OK] Write to:", fo) + } + + fmt.Println(stro) +}