实现MarshalJSON和UnmarshalJSON
This commit is contained in:
parent
ebc476f7bd
commit
2908670e0d
|
@ -233,6 +233,7 @@ func (slf *RankingList[CompetitorID, Score]) UnmarshalJSON(bytes []byte) error {
|
|||
var t struct {
|
||||
Competitors *synchronization.Map[CompetitorID, Score] `json:"competitors,omitempty"`
|
||||
Scores []*scoreItem[CompetitorID, Score] `json:"scores,omitempty"`
|
||||
Asc bool `json:"asc,omitempty"`
|
||||
}
|
||||
t.Competitors = synchronization.NewMap[CompetitorID, Score]()
|
||||
if err := json.Unmarshal(bytes, &t); err != nil {
|
||||
|
@ -240,6 +241,7 @@ func (slf *RankingList[CompetitorID, Score]) UnmarshalJSON(bytes []byte) error {
|
|||
}
|
||||
slf.competitors = t.Competitors
|
||||
slf.scores = t.Scores
|
||||
slf.asc = t.Asc
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -247,9 +249,11 @@ func (slf *RankingList[CompetitorID, Score]) MarshalJSON() ([]byte, error) {
|
|||
var t struct {
|
||||
Competitors *synchronization.Map[CompetitorID, Score] `json:"competitors,omitempty"`
|
||||
Scores []*scoreItem[CompetitorID, Score] `json:"scores,omitempty"`
|
||||
Asc bool `json:"asc,omitempty"`
|
||||
}
|
||||
t.Competitors = slf.competitors
|
||||
t.Scores = slf.scores
|
||||
t.Asc = slf.asc
|
||||
|
||||
return json.Marshal(&t)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue