-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Populate generated struct example. #2
Labels
Comments
Source file {
"format": {
"bit_rate": "4236261",
"duration": "2554.144000",
"filename": "video.mkv",
"format_long_name": "Matroska / WebM",
"format_name": "matroska,webm",
"nb_streams": 2,
"size": "1352502623",
"start_time": "0.000000",
"tags": {
"creation_time": "2012-10-11 12:29:58"
}
}
} Generate type with JSONGen:
Populate generated struct with source file: package main
import (
"encoding/json"
"fmt"
"log"
"os"
)
const (
SourceFile = "format.json"
)
// Type generated with JSONGen.
type FFProbe struct {
Format struct {
Duration string `json:"duration"`
FormatLongName string `json:"format_long_name"`
FormatName string `json:"format_name"`
NbStreams int64 `json:"nb_streams"`
StartTime string `json:"start_time"`
BitRate string `json:"bit_rate"`
Size string `json:"size"`
Tags struct {
CreationTime string `json:"creation_time"`
} `json:"tags"`
Filename string `json:"filename"`
} `json:"format"`
}
func main() {
// Open source file.
sourceFile, err := os.Open(SourceFile)
if err != nil {
log.Fatal("Error opening source file:", err)
}
defer sourceFile.Close()
// Make a new json decoder reading from the source string.
ffprobeDecoder := json.NewDecoder(sourceFile)
// Decode into ffprobe.
var ffprobe FFProbe
err = ffprobeDecoder.Decode(&ffprobe)
if err != nil {
log.Fatal("Error decoding ffprobe:", err)
}
// Print populated ffprobe.
fmt.Printf("%+v\n", ffprobe)
} Output:
|
Hi Douglas, Thanks but I meant the other way around. I.e. creating a struct and then serializing it to JSON. |
That's not really related to this project. But it would operate more or less the same way as the example above just substituting NewDecoder with NewEncoder or any of the Marshal functions. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!
The text was updated successfully, but these errors were encountered: