Skip to content

Instantly share code, notes, and snippets.

@james4k
Created October 13, 2013 05:19
Show Gist options
  • Save james4k/6958451 to your computer and use it in GitHub Desktop.
Save james4k/6958451 to your computer and use it in GitHub Desktop.
diff -r 414057ac1f1f src/pkg/encoding/json/bench_test.go
--- a/src/pkg/encoding/json/bench_test.go Tue Aug 13 15:33:06 2013 +1000
+++ b/src/pkg/encoding/json/bench_test.go Sun Oct 13 00:17:22 2013 -0500
@@ -24,6 +24,10 @@
}
type codeNode struct {
+ codeNodeData
+}
+
+type codeNodeData struct {
Name string `json:"name"`
Kids []*codeNode `json:"kids"`
CLWeight float64 `json:"cl_weight"`
@@ -33,6 +37,14 @@
MeanT int64 `json:"mean_t"`
}
+func (c *codeNode) MarshalJSON() ([]byte, error) {
+ return Marshal(&c.codeNodeData)
+}
+
+func (c *codeNode) UnmarshalJSON(data []byte) error {
+ return Unmarshal(data, &c.codeNodeData)
+}
+
var codeJSON []byte
var codeStruct codeResponse
diff -r 414057ac1f1f src/pkg/encoding/json/encode.go
--- a/src/pkg/encoding/json/encode.go Tue Aug 13 15:33:06 2013 +1000
+++ b/src/pkg/encoding/json/encode.go Sun Oct 13 00:17:22 2013 -0500
@@ -225,6 +225,7 @@
type encodeState struct {
bytes.Buffer // accumulated output
scratch [64]byte
+ scan *scanner
}
func (e *encodeState) marshal(v interface{}) (err error) {
@@ -290,7 +291,10 @@
b, err := m.MarshalJSON()
if err == nil {
// copy JSON into buffer, checking validity.
- err = compact(&e.Buffer, b, true)
+ if e.scan == nil {
+ e.scan = &scanner{}
+ }
+ err = compact(e.scan, &e.Buffer, b, true)
}
if err != nil {
e.error(&MarshalerError{v.Type(), err})
diff -r 414057ac1f1f src/pkg/encoding/json/indent.go
--- a/src/pkg/encoding/json/indent.go Tue Aug 13 15:33:06 2013 +1000
+++ b/src/pkg/encoding/json/indent.go Sun Oct 13 00:17:22 2013 -0500
@@ -9,12 +9,12 @@
// Compact appends to dst the JSON-encoded src with
// insignificant space characters elided.
func Compact(dst *bytes.Buffer, src []byte) error {
- return compact(dst, src, false)
+ var scan scanner
+ return compact(&scan, dst, src, false)
}
-func compact(dst *bytes.Buffer, src []byte, escape bool) error {
+func compact(scan *scanner, dst *bytes.Buffer, src []byte, escape bool) error {
origLen := dst.Len()
- var scan scanner
scan.reset()
start := 0
for i, c := range src {
@@ -27,7 +27,7 @@
dst.WriteByte(hex[c&0xF])
start = i + 1
}
- v := scan.step(&scan, int(c))
+ v := scan.step(scan, int(c))
if v >= scanSkipSpace {
if v == scanError {
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment