初始版本.
Signed-off-by: rick.chan <cy@haoan119.com>
This commit is contained in:
parent
f4bb2225b4
commit
dd7f2d89cf
1
.gitignore
vendored
1
.gitignore
vendored
@ -21,3 +21,4 @@
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
json2nats
|
||||
|
24
default.json
Normal file
24
default.json
Normal 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
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
53
main.go
Normal file
53
main.go
Normal 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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user