JSON in Go: Difference between revisions
Jump to navigation
Jump to search
(→TODO) |
|||
(28 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Internal= | =Internal= | ||
* [[Go Code Examples#Code_Examples|Go Code Examples]] | * [[Go Code Examples#Code_Examples|Go Code Examples]] | ||
* [[Go Package json]] | * [[Go Package encoding/json]] | ||
* [[JSON#Subjects|JSON]] | * [[JSON#Subjects|JSON]] | ||
=TODO= | |||
<font color=darkkhaki> | |||
TO PROCESS: | |||
* https://www.digitalocean.com/community/tutorials/how-to-use-struct-tags-in-go | |||
* https://golang.cafe/blog/golang-json-marshal-example.html | |||
* Reading into an array of custom structs: | |||
<syntaxhighlight lang='go'> | |||
var ratings []model.RatingEvent | |||
if err := json.NewDecoder(f).Decode(&ratings); err != nil { | |||
return nil, err | |||
} | |||
</syntaxhighlight> | |||
</font> | |||
=Overview= | =Overview= | ||
Struct [[Go_Structs#Tags|tags]] can be used in the JAML serialization/deserialization process. | |||
=JSON Marshalling Go → JSON= | =JSON Marshalling Go → JSON= | ||
JSON marshalling means generating a JSON representation from a Go object. | JSON marshalling means generating a JSON representation from a Go object. It is done with <code>json.Marshal()</code>. The result is a byte array. To convert the byte array to a string, see: {{Internal|Go_Integers#Conversion_between_bytes_and_strings|Go Integers | Conversion between bytes and strings}} | ||
==Map Marshalling== | |||
==Struct Marshalling== | |||
By default, only fields that start with capital letters are marshaled. The fields that start with lower caps are invisible to the marshaling process. | |||
<syntaxhighlight lang='go'> | |||
type Item struct { | |||
Color string | |||
Size int | |||
Options []string | |||
} | |||
i := Item{Color: "blue", Size: 5, Options: []string{"brake", "acceleration", "lights"}} | |||
ba, err := json.Marshal(i) // pass the value | |||
fmt.Println(string(ba)) | |||
</syntaxhighlight> | |||
=JSON Unmarshalling JSON → Go= | =JSON Unmarshalling JSON → Go= | ||
==Unmarshalling into Struct== | |||
===With a Byte Slice=== | |||
<syntaxhighlight lang='go'> | |||
type Item struct { | |||
Color string | |||
Size int | |||
Options []string | |||
} | |||
s := "{\"Color\":\"blue\",\"Size\":5,\"Options\":[\"brake\",\"acceleration\",\"lights\"]}" | |||
ba := []byte(s) | |||
var i Item | |||
err := json.Unmarshal(ba, &i) // pass the pointer | |||
fmt.Printf("%+v\n", i) | |||
</syntaxhighlight> | |||
===With a <tt>Reader</tt>=== | |||
Using a <code>[[Go_Package_io#Reader|Reader]]</code>: | |||
<syntaxhighlight lang='go'> | |||
var req *http.Request = ... | |||
reader := req.Body // body implements Reader | |||
jsonDecoder := json.NewDecoder(reader) | |||
var s SomeStruct | |||
if e := jsonDecoder.Decode(&s); e != nil { | |||
// ... | |||
} | |||
fmt.Printf("%+v\n", s) | |||
</syntaxhighlight> | |||
This only works if the HTTP requests has a "Content-Type: application/json" header, otherwise the decoding fails with an EOF error. Also see: {{Internal|Go_Package_net/http#Reading_the_Request_Body|net/http | Reading the Request Body}} |
Latest revision as of 20:55, 14 May 2024
Internal
TODO
TO PROCESS:
- https://www.digitalocean.com/community/tutorials/how-to-use-struct-tags-in-go
- https://golang.cafe/blog/golang-json-marshal-example.html
- Reading into an array of custom structs:
var ratings []model.RatingEvent
if err := json.NewDecoder(f).Decode(&ratings); err != nil {
return nil, err
}
Overview
Struct tags can be used in the JAML serialization/deserialization process.
JSON Marshalling Go → JSON
JSON marshalling means generating a JSON representation from a Go object. It is done with json.Marshal()
. The result is a byte array. To convert the byte array to a string, see:
Map Marshalling
Struct Marshalling
By default, only fields that start with capital letters are marshaled. The fields that start with lower caps are invisible to the marshaling process.
type Item struct {
Color string
Size int
Options []string
}
i := Item{Color: "blue", Size: 5, Options: []string{"brake", "acceleration", "lights"}}
ba, err := json.Marshal(i) // pass the value
fmt.Println(string(ba))
JSON Unmarshalling JSON → Go
Unmarshalling into Struct
With a Byte Slice
type Item struct {
Color string
Size int
Options []string
}
s := "{\"Color\":\"blue\",\"Size\":5,\"Options\":[\"brake\",\"acceleration\",\"lights\"]}"
ba := []byte(s)
var i Item
err := json.Unmarshal(ba, &i) // pass the pointer
fmt.Printf("%+v\n", i)
With a Reader
Using a Reader
:
var req *http.Request = ...
reader := req.Body // body implements Reader
jsonDecoder := json.NewDecoder(reader)
var s SomeStruct
if e := jsonDecoder.Decode(&s); e != nil {
// ...
}
fmt.Printf("%+v\n", s)
This only works if the HTTP requests has a "Content-Type: application/json" header, otherwise the decoding fails with an EOF error. Also see: