Skip to content

Commit

Permalink
prioritize handling invalid json over pretty json
Browse files Browse the repository at this point in the history
  • Loading branch information
dcilke committed Aug 3, 2022
1 parent ad22afd commit 6c18ff7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ bin/

# Dependency directories (remove the comment below to include it)
# vendor/
samples/
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ all: build

.PHONY=build
build:
@go build -o bin/hz
@go build -o bin/hz

.PHONY=install
install: build
@go install
36 changes: 24 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bufio"
"encoding/json"
"fmt"
"os"

Expand All @@ -11,10 +10,12 @@ import (
"github.com/rs/zerolog/log"
)

var writer = zerolog.NewConsoleWriter()

type cmd struct{}

func init() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
}

func main() {
Expand Down Expand Up @@ -42,16 +43,27 @@ func main() {

func process(file *os.File) {
reader := bufio.NewReader(file)
decoder := json.NewDecoder(reader)
writer := zerolog.NewConsoleWriter()
for decoder.More() {
var line interface{}
err := decoder.Decode(&line)
check(err, "unable to decode object")
data, err := json.Marshal(line)
check(err, "unable to marshal object")
_, err = writer.Write(data)
check(err, "unable to write object")

for {
b, err := reader.ReadBytes('\n')
if err != nil {
break
}
_, err = writer.Write(b)
if err != nil {
/**
TODO: handle "pretty printed" objects
"Better" json stream parser? The need is a parser which will handle
the json and output the invalid characters while skipping over them.
Read into buffer, classifying as valid or invalid json. Something
with an api like:
var obj interface{}
var str string
err := reader.Decode(&obj, &str)
**/
fmt.Print(string(b))
}
}
}

Expand Down

0 comments on commit 6c18ff7

Please sign in to comment.