-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
43 lines (40 loc) · 931 Bytes
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package jjs_go
import (
"errors"
"github.com/nyan233/jjs-go/core/compiler"
"github.com/nyan233/jjs-go/core/mempool"
"github.com/nyan233/jjs-go/internal/rtype"
"reflect"
"runtime"
"unsafe"
)
func Marshal(data interface{}) (buf []byte, err error) {
if data == nil {
return nil, errors.New("data is nil")
}
ef := (*rtype.EmptyFace)(unsafe.Pointer(&data))
function, err := compiler.CompileEncoder(reflect.TypeOf(data), 0)
if err != nil {
panic(err)
}
// TODO grow
newBytes := make([]byte, 0, 512)
stack := mempool.StackPool.Get().(*mempool.Stack)
defer func() {
rErr := recover()
if rErr != nil {
err, _ = rErr.(error)
}
}()
function(stack.High, &newBytes, ef.Val, 0)
runtime.KeepAlive(stack)
runtime.KeepAlive(&newBytes)
runtime.KeepAlive(ef.Val)
return newBytes, nil
}
func Unmarshal(bytes []byte, data interface{}) error {
if data == nil {
return errors.New("data is nil")
}
return nil
}