Use json instead

This commit is contained in:
Ben Sarmiento
2023-12-02 05:00:37 +01:00
parent a8e8bab853
commit b140dfd4d0
3 changed files with 124 additions and 69 deletions

View File

@@ -37,15 +37,11 @@ func (i *Torrent) UnmarshalJSON(data []byte) error {
}{
Alias: (*Alias)(i),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
i.Progress = int(math.Round(aux.Progress))
i.Progress = int(math.Floor(aux.Progress))
i.Added = strings.Replace(aux.Added, "Z", "+01:00", 1)
return nil
}
@@ -62,7 +58,24 @@ type TorrentInfo struct {
OriginalName string `json:"original_filename"`
OriginalBytes int64 `json:"original_bytes"`
Files []File `json:"files"`
Version string `json:"-"`
}
func (i *TorrentInfo) MarshalJSON() ([]byte, error) {
type Alias TorrentInfo
aux := &struct {
Progress float64 `json:"progress"`
Added string `json:"added"`
Ended string `json:"ended"`
*Alias
}{
Alias: (*Alias)(i),
Progress: float64(i.Progress), // Convert int to float64 for JSON representation
Added: i.Added,
}
if i.Ended != "" {
aux.Ended = i.Ended
}
return json.Marshal(aux)
}
func (i *TorrentInfo) UnmarshalJSON(data []byte) error {
@@ -75,19 +88,14 @@ func (i *TorrentInfo) UnmarshalJSON(data []byte) error {
}{
Alias: (*Alias)(i),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
i.Progress = int(math.Round(aux.Progress))
i.Progress = int(math.Floor(aux.Progress))
i.Added = strings.Replace(aux.Added, "Z", "+01:00", 1)
if aux.Ended != "" {
i.Ended = strings.Replace(aux.Ended, "Z", "+01:00", 1)
}
return nil
}