10 examples of 'golang json marshal' in Go

Every line of 'golang json marshal' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
181func Marshal(data interface{}, args ...interface{}) (out []byte, err error) {
182 var wr *Writer
183 if 0 < len(args) {
184 wr = pickWriter(args[0], true)
185 }
186 if wr == nil {
187 wr, _ = marshalPool.Get().(*Writer)
188 defer marshalPool.Put(wr)
189 } else {
190 wr.strict = true
191 }
192 defer func() {
193 if r := recover(); r != nil {
194 wr.buf = wr.buf[:0]
195 err = ojg.NewError(r)
196 }
197 }()
198 wr.MustJSON(data)
199 out = wr.buf
200
201 return
202}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

383func jsonMarshal(v interface{}) ([]byte, byte, error) {
384 msg, err := json.Marshal(v)
385 return msg, TextFrame, err
386}
22func (response *SubmitTransactionResponse) Marshal() []byte {
23 json, _ := json.MarshalIndent(response, "", " ")
24 return json
25}
96func (error *ErrorResponse) Marshal() []byte {
97 json, _ := json.MarshalIndent(error, "", " ")
98 return json
99}
89func JSONMarshal(t interface{}, prefix string, indent string) ([]byte, error) {
90 buffer := &bytes.Buffer{}
91 encoder := json.NewEncoder(buffer)
92 encoder.SetEscapeHTML(false)
93 encoder.SetIndent(prefix, indent)
94 err := encoder.Encode(t)
95 return buffer.Bytes(), err
96}
78func (m *jsonMarshaler) Marshal(v interface{}) ([]byte, error) {
79 return json.MarshalIndent(v, "", " ")
80}
569func (v *DestinationServiceSpanContext) MarshalFastJSON(w *fastjson.Writer) error {
570 w.RawByte('{')
571 first := true
572 if v.Name != "" {
573 const prefix = ",\"name\":"
574 if first {
575 first = false
576 w.RawString(prefix[1:])
577 } else {
578 w.RawString(prefix)
579 }
580 w.String(v.Name)
581 }
582 if v.Resource != "" {
583 const prefix = ",\"resource\":"
584 if first {
585 first = false
586 w.RawString(prefix[1:])
587 } else {
588 w.RawString(prefix)
589 }
590 w.String(v.Resource)
591 }
592 if v.Type != "" {
593 const prefix = ",\"type\":"
594 if first {
595 first = false
596 w.RawString(prefix[1:])
597 } else {
598 w.RawString(prefix)
599 }
600 w.String(v.Type)
601 }
602 w.RawByte('}')
603 return nil
604}
77func (j JSONEncoding) Marshal(v interface{}) ([]byte, error) {
78 buf, err := json.Marshal(v)
79 return buf, err
80}
102func JSONEncode(variable interface{}) ([]byte, error) {
103 str, err := json.Marshal(variable)
104 if err != nil {
105 return nil, err
106 }
107 return str, nil
108}
1291func (v *Metric) MarshalFastJSON(w *fastjson.Writer) error {
1292 w.RawByte('{')
1293 w.RawString("\"value\":")
1294 w.Float64(v.Value)
1295 w.RawByte('}')
1296 return nil
1297}

Related snippets