GithubHelp home page GithubHelp logo

Comments (3)

bemasher avatar bemasher commented on June 21, 2024

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}}

from jsongen.

emilebosch avatar emilebosch commented on June 21, 2024

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

from jsongen.

bemasher avatar bemasher commented on June 21, 2024

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.

from jsongen.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.