初始版本.

Signed-off-by: rick.chan <cy@haoan119.com>
This commit is contained in:
rick.chan 2023-10-19 11:13:18 +08:00
parent f4bb2225b4
commit dd7f2d89cf
4 changed files with 81 additions and 0 deletions

1
.gitignore vendored
View File

@ -21,3 +21,4 @@
# Go workspace file # Go workspace file
go.work go.work
json2nats

24
default.json Normal file
View File

@ -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
}
]
}
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module json2nats
go 1.21.3

53
main.go Normal file
View File

@ -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)
}