Skip to content
Permalink
Browse files

use djson and JSONString for faster json handling in tree and leaf.

  • Loading branch information...
fiatjaf committed Apr 16, 2017
1 parent f872409 commit 6f84994e58ea2b2c968206f9fdb947dc304d2da1
Showing with 13 additions and 11 deletions.
  1. +1 −0 .travis.yml
  2. +1 −1 README.md
  3. +5 −5 types/leaf.go
  4. +6 −5 types/tree.go
@@ -10,6 +10,7 @@ install:
- go get github.com/mgutz/logxi/v1
- go get github.com/kr/pretty
- go get github.com/spf13/viper
- go get github.com/a8m/djson
- go get github.com/yuin/gopher-lua
- go get gopkg.in/check.v1
- go get github.com/gorilla/websocket
@@ -1 +1 @@
[![travis badge](https://travis-ci.org/summadb/summadb.svg?branch=v1)](https://travis-ci.org/summadb/summadb)
[![travis badge](https://travis-ci.org/summadb/summadb.svg?branch=master)](https://travis-ci.org/summadb/summadb)
@@ -1,10 +1,11 @@
package types

import (
"encoding/json"
"errors"
"strconv"
"strings"

"github.com/a8m/djson"
"github.com/summadb/summadb/utils"
)

const (
@@ -33,7 +34,7 @@ func (l Leaf) Bool() bool { return l.bool }
func (l Leaf) MarshalJSON() ([]byte, error) {
switch l.Kind {
case STRING:
return []byte(`"` + strings.Replace(l.string, `"`, `\"`, -1) + `"`), nil
return utils.JSONString(l.string), nil
case NUMBER:
return []byte(strconv.FormatFloat(l.float64, 'f', -1, 32)), nil
case BOOL:
@@ -48,8 +49,7 @@ func (l Leaf) MarshalJSON() ([]byte, error) {
}

func (l *Leaf) UnmarshalJSON(j []byte) error {
var v interface{}
err := json.Unmarshal(j, &v)
v, err := djson.Decode(j)
if err != nil {
return err
}
@@ -2,8 +2,10 @@ package types

import (
"bytes"
"encoding/json"
"strings"

"github.com/a8m/djson"
"github.com/summadb/summadb/utils"
)

type Tree struct {
@@ -21,8 +23,7 @@ type Branches map[string]*Tree
func NewTree() *Tree { return &Tree{Branches: make(Branches)} }

func (t *Tree) UnmarshalJSON(j []byte) error {
var v interface{}
err := json.Unmarshal(j, &v)
v, err := djson.Decode(j)
if err != nil {
return err
}
@@ -91,14 +92,14 @@ func (t Tree) MarshalJSON() ([]byte, error) {
// key
if t.Key != "" {
buffer := bytes.NewBufferString(`"_key":`)
buffer.WriteString(`"` + t.Key + `"`)
buffer.Write(utils.JSONString(t.Key))
parts = append(parts, buffer.Bytes())
}

// rev
if t.Rev != "" {
buffer := bytes.NewBufferString(`"_rev":`)
buffer.WriteString(`"` + t.Rev + `"`)
buffer.Write(utils.JSONString(t.Rev))
parts = append(parts, buffer.Bytes())
}

0 comments on commit 6f84994

Please sign in to comment.
You can’t perform that action at this time.