Giter Club home page Giter Club logo

Comments (3)

mrutkows avatar mrutkows commented on July 19, 2024

See: https://stackoverflow.com/questions/46265751/omitting-json-for-empty-custom-type

if you really want to omit a field based and internal state, you need to implement json.Marshaller on your structure, not its children. The easiest way to do this would be as follows:

func (d Data) MarshalJSON() ([]byte, error) {
    if !d.Stderr.Valid {
        return json.Marshal(Data{d.X, d.Y, nil})
    }
    return json.Marshal(d)
}

from sbom-utility.

mrutkows avatar mrutkows commented on July 19, 2024

more help... https://stackoverflow.com/questions/65200070/how-to-marshal-nested-struct-to-flat-json

type A struct {
    Value float64
    Unit  string
}

type B struct {
    a1 A
    a2 A
}

func (b *B) MarshalJSON() ([]byte, error) {
    intermediate := map[string]interface{}{
        "a1":   b.a1.Value,
        "a1_u": b.a1.Unit,
        "a2":   b.a2.Value,
        "a2_u": b.a2.Unit,
    }
    return json.Marshal(intermediate)
}

from sbom-utility.

mrutkows avatar mrutkows commented on July 19, 2024

Fixed by #22

from sbom-utility.

Related Issues (20)

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.