Skip to content
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

Closed
emilebosch opened this issue Jul 25, 2014 · 3 comments
Closed

Populate generated struct example. #2

emilebosch opened this issue Jul 25, 2014 · 3 comments
Labels

Comments

@emilebosch
Copy link

Thanks!

@bemasher bemasher changed the title Please provide sample on populating the generated struct Populating the generated struct example. Jul 25, 2014
@bemasher bemasher changed the title Populating the generated struct example. Populating generated struct example. Jul 25, 2014
@bemasher
Copy link
Owner

Source file format.json:

{
    "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:

$ jsongen format.json
type _ 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"`
}

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:

{Format:{Duration:2554.144000 FormatLongName:Matroska / WebM FormatName:matroska,webm NbStreams:2 StartTime:0.000000 BitRate:4236261 Size:1352502623 Tags:{CreationTime:2012-10-11 12:29:58} Filename:video.mkv}}

@bemasher bemasher changed the title Populating generated struct example. Populate generated struct example. Jul 25, 2014
@emilebosch
Copy link
Author

Hi Douglas, Thanks but I meant the other way around. I.e. creating a struct and then serializing it to JSON.

@bemasher bemasher reopened this Jul 26, 2014
@bemasher
Copy link
Owner

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
Labels
Projects
None yet
Development

No branches or pull requests

2 participants