Marshal executes the instructions for a given type and writes the resulting json document to the io.Writer provided
(s interface{}, w *Buffer)
| 52 | // Marshal executes the instructions for a given type and writes the resulting |
| 53 | // json document to the io.Writer provided |
| 54 | func (e *StructEncoder) Marshal(s interface{}, w *Buffer) { |
| 55 | |
| 56 | p := (*(*iface)(unsafe.Pointer(&s))).Data |
| 57 | |
| 58 | for i := 0; i < len(e.instructions); i++ { |
| 59 | |
| 60 | if e.instructions[i].kind == kindStatic { // static data fast path |
| 61 | w.Write(e.instructions[i].static) |
| 62 | continue |
| 63 | } else if e.instructions[i].kind == kindStringField { // string fields fast path, allows inlining of whole write |
| 64 | ptrStringToBuf(unsafe.Pointer(uintptr(p)+e.instructions[i].offset), w) |
| 65 | continue |
| 66 | } else if e.instructions[i].kind == kindInt { // int fields fast path, allows inlining of whole write |
| 67 | ptrIntToBuf(unsafe.Pointer(uintptr(p)+e.instructions[i].offset), w) |
| 68 | continue |
| 69 | } else if e.instructions[i].leapFun != nil { // simple 'conv' function fast path |
| 70 | e.instructions[i].leapFun(unsafe.Pointer(uintptr(p)+e.instructions[i].offset), w) |
| 71 | continue |
| 72 | } |
| 73 | |
| 74 | e.instructions[i].fun(p, w) // all other instruction types |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // NewStructEncoder compiles a set of instructions for marhsaling a struct shape to a JSON document. |
| 79 | func NewStructEncoder(t interface{}) *StructEncoder { |