initial commits

master
Charles Iliya Krempeaux 2023-09-27 13:45:47 +09:00
parent b38bf1fe82
commit e59961adc1
5 changed files with 82 additions and 0 deletions

10
errors.go 100644
View File

@ -0,0 +1,10 @@
package jsonpp
import (
"sourcecode.social/reiver/go-erorr"
)
const (
errInternalError = erorr.Error("jsonpp: internal error")
errNilWriter = erorr.Error("jsonpp: nil writer")
)

26
fprettyprint.go 100644
View File

@ -0,0 +1,26 @@
package jsonpp
import (
"bytes"
"io"
)
func FPrettyPrint(writer io.Writer, json []byte) error {
if nil == writer {
return errNilWriter
}
var buffer bytes.Buffer
err := jsonPrettyPrint(&buffer, json)
if nil != err {
return err
}
_, err = buffer.WriteTo(writer)
if nil != err {
return err
}
return nil
}

21
jsonprettyprint.go 100644
View File

@ -0,0 +1,21 @@
package jsonpp
import (
"bytes"
"encoding/json"
)
const indent string = "\t"
func jsonPrettyPrint(dst *bytes.Buffer, src []byte) error {
if nil == dst {
return errInternalError
}
err := json.Indent(dst, src, "", indent)
if nil != err {
return err
}
return nil
}

9
prettyprint.go 100644
View File

@ -0,0 +1,9 @@
package jsonpp
import (
"os"
)
func PrettyPrint(json []byte) error {
return FPrettyPrint(os.Stdout, json)
}

16
sprettyprint.go 100644
View File

@ -0,0 +1,16 @@
package jsonpp
import (
"bytes"
)
func SPrettyPrint(json []byte) string {
var buffer bytes.Buffer
err := jsonPrettyPrint(&buffer, json)
if nil != err {
return ""
}
return buffer.String()
}