How can I override json tags in a Go struct?

霸气de小男生 提交于 2021-01-28 06:16:24

问题


I'd like to marshal part of this struct:

type ValueSet struct {
    Id           string                       `json:"id" bson:"_id"`
    Url          string                       `bson:"url,omitempty" json:"url,omitempty"`
    Identifier   *Identifier                  `bson:"identifier,omitempty" json:"identifier,omitempty"`
    Version      string                       `bson:"version,omitempty" json:"version,omitempty"`
    Name         string                       `bson:"name,omitempty" json:"name,omitempty"`
    Status       string                       `bson:"status,omitempty" json:"status,omitempty"`
    Experimental *bool                        `bson:"experimental,omitempty" json:"experimental,omitempty"`
    Publisher    string                       `bson:"publisher,omitempty" json:"publisher,omitempty"`
    Contact      []ValueSetContactComponent   `bson:"contact,omitempty" json:"contact,omitempty"`
    Date         *FHIRDateTime                `bson:"date,omitempty" json:"date,omitempty"`
    LockedDate   *FHIRDateTime                `bson:"lockedDate,omitempty" json:"lockedDate,omitempty"`
    Description  string                       `bson:"description,omitempty" json:"description,omitempty"`
    UseContext   []CodeableConcept            `bson:"useContext,omitempty" json:"useContext,omitempty"`
    Immutable    *bool                        `bson:"immutable,omitempty" json:"immutable,omitempty"`
    Requirements string                       `bson:"requirements,omitempty" json:"requirements,omitempty"`
    Copyright    string                       `bson:"copyright,omitempty" json:"copyright,omitempty"`
    Extensible   *bool                        `bson:"extensible,omitempty" json:"extensible,omitempty"`
    CodeSystem   *ValueSetCodeSystemComponent `bson:"codeSystem,omitempty" json:"codeSystem,omitempty"`
    Compose      *ValueSetComposeComponent    `bson:"compose,omitempty" json:"compose,omitempty"`
    Expansion    *ValueSetExpansionComponent  `bson:"expansion,omitempty" json:"expansion,omitempty"`
}

which is part of a Go implementation of HL7 FHIR, including only the metadata fields, and omitting the three content three fields (codeSystem, compose and expansion). I can't (and shouldn't) change the JSON tags in the original source code, since other code strongly depends on it working the way it's written. How can I tell json.Marshal to override the existing JSON tags on these struct elements?


回答1:


You can't change it, but you don't have to.

Easiest solution is to create your own struct, define your own json tags (how you want them to appear in the output), copy the fields, and marshal a value of your own struct.

E.g. let's say you want to marshal the Id and Urlfields, then:

type MyValueSet struct {
    Id string  `json:"MyId"`
    Url string `json:"MyUrl"`
}

var vs ValueSet = ... // Comes from somewhere

mvs := MyValueSet {
    Id:  vs.Id,
    Url: vs.Url,
}

data, err := json.Marshal(&mvs)
// Check err


来源:https://stackoverflow.com/questions/34909177/how-can-i-override-json-tags-in-a-go-struct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!