I have the following struct in in my Web Application using "encoding/json"
type CourseAssignment struct {
Semester int `json:"semester" xml:"semester"`
Lecture Lecture `json:"-" xml:"-"`
Cos Cos `json:"-" xml:"-"`
Links map[string][]Link `json:"links,omitempty" xml:"links,omitempty"`
}
Lecture and Cos are complex structs themselves that i dont want to be included in my serialized json which i indicate by setting json:"-"
This works perfectly.
I want to override that behavior on demand during runtime, how do i do that without writing my own Serialization code ?
Edit: My Own Solution:
func (r *CourseAssignment) Expand(depth int) CourseAssignment {
if depth <= 0 {
return *r
}
tmp := *r
tmp.LectureEx = tmp.Lecture
tmp.CosEx = tmp.Cos
tmp.Links = nil
return tmp
}
type CourseAssignment struct {
Semester int `json:"semester" xml:"semester"`
Lecture *Lecture `json:"-" xml:"-"`
Cos *Cos `json:"-" xml:"-"`
Links map[string][]Link `json:"links,omitempty" xml:"links,omitempty"`
LectureEx *Lecture `json:"lecture,omitempty" xml:"lecture,omitempty"`
CosEx *Cos `json:"course_of_study,omitempty" xml:"course_of_study,omitempty"`
}
When i want to include the fields i create a Copy of the Object using expand that fills fields that contain the same references but show up in the serialization.