MCPcopy Create free account
hub / github.com/bet365/jingo / Marshal

Method Marshal

structencoder.go:54–76  ·  view source on GitHub ↗

Marshal executes the instructions for a given type and writes the resulting json document to the io.Writer provided

(s interface{}, w *Buffer)

Source from the content-addressed store, hash-verified

52// Marshal executes the instructions for a given type and writes the resulting
53// json document to the io.Writer provided
54func (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.
79func NewStructEncoder(t interface{}) *StructEncoder {

Callers 7

valueInstMethod · 0.95
structInstrMethod · 0.95
ptrStrctInstrMethod · 0.95
ExampleFunction · 0.95
BenchmarkJsonFunction · 0.95
BenchmarkSmallPayloadFunction · 0.95
BenchmarkLargePayloadFunction · 0.95

Calls 3

ptrStringToBufFunction · 0.85
ptrIntToBufFunction · 0.85
WriteMethod · 0.80

Tested by 4

ExampleFunction · 0.76
BenchmarkJsonFunction · 0.76
BenchmarkSmallPayloadFunction · 0.76
BenchmarkLargePayloadFunction · 0.76