// This example uses a Decoder to decode a stream of distinct JSON values. func ExampleDecoder() { const rjsonStream = ` {"Name": "Ed", "Text": "Knock knock."} {"Name": "Sam", "Text": "Who's there?"} {"Name": "Ed", "Text": "Go fmt."} {"Name": "Sam", "Text": "Go fmt who?"} {"Name": "Ed", "Text": "Go fmt yourself!"} ` type Message struct { Name, Text string } dec := rjson.NewDecoder(strings.NewReader(rjsonStream)) for { var m Message if err := dec.Decode(&m); err == io.EOF { break } else if err != nil { log.Fatal(err) } fmt.Printf("%s: %s\n", m.Name, m.Text) } // Output: // Ed: Knock knock. // Sam: Who's there? // Ed: Go fmt. // Sam: Go fmt who? // Ed: Go fmt yourself! }
func readBaseConfiguration(config string) *Conf { conf := &Conf{} dec := rjson.NewDecoder(bytes.NewBufferString(config)) if err := dec.Decode(conf); err != nil { log.Println("Error in base configuration") log.Fatal(err) } return conf }
func ApplyObject(src interface{}, dest interface{}) { var buf bytes.Buffer enc := rjson.NewEncoder(&buf) if err := enc.Encode(src); err != nil { log.Println(err) return } dec := rjson.NewDecoder(&buf) if err := dec.Decode(dest); err != nil { log.Println(err) } }
func main() { flag.Parse() if flag.NArg() > 0 { fmt.Fprintf(os.Stderr, "rjson reads from stdin only") os.Exit(2) } var marshal func(interface{}) ([]byte, error) if *jsonOut { if *indent == "" { marshal = json.Marshal } else { marshal = func(v interface{}) ([]byte, error) { return json.MarshalIndent(v, "", *indent) } } } else { if *indent == "" { marshal = func(v interface{}) ([]byte, error) { data, err := rjson.MarshalIndent(v, "", "") if err != nil { return data, err } // TODO: use Compact only when it compacts // identifiers too. var b bytes.Buffer if err := rjson.Compact(&b, data); err != nil { return nil, err } return b.Bytes(), nil } } else { marshal = func(v interface{}) ([]byte, error) { return rjson.MarshalIndent(v, "", *indent) } } } d := rjson.NewDecoder(os.Stdin) d.UseNumber() w := bufio.NewWriter(os.Stdout) err := stream(w, d, marshal) if err != nil { fmt.Fprintf(os.Stderr, "rjson: %v", err) os.Exit(1) } }
func NewConf(configFile string) *Conf { conf := readBaseConfiguration(baseConfiguration) if configFile == "" { return conf } data, err := ioutil.ReadFile(configFile) if err != nil { log.Println("Unable to read configuration file: ", configFile) log.Fatal(err) } regArg, _ := regexp.Compile(`^\s*(.*)=(.*)$`) for _, arg := range flag.Args() { if !regArg.MatchString(arg) { log.Fatal("Argument was not in correct form: ", arg) } tokens := regArg.FindStringSubmatch(arg) replace, _ := regexp.Compile(`\$` + tokens[1] + `(=[^$]*)?\$`) replacement := ([]byte)(tokens[2]) data = replace.ReplaceAll(data, replacement) } regDefaults, _ := regexp.Compile(`\$[^\$]+\$`) regDefault, _ := regexp.Compile(`\$[^=]+=(.*)\$`) data = regDefaults.ReplaceAllFunc(data, func(repl []byte) []byte { defaults := regDefault.FindSubmatch(repl) if defaults != nil { return defaults[1] } return nil }) dec := rjson.NewDecoder(bytes.NewReader(data)) if err = dec.Decode(conf); err != nil { log.Println("Error in configuration file: ", configFile) log.Fatal(err) } return conf }