From 480eac7235c2b0ad3d04de1f10389ea9a54606ac Mon Sep 17 00:00:00 2001 From: yiguous <147401898+yiguous@users.noreply.github.com> Date: Wed, 1 Jan 2025 18:16:36 +0800 Subject: [PATCH] Config: Correctly marshal Int32Range to JSON (#4234) Fixes https://github.com/XTLS/libXray/issues/62 --- infra/conf/common.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/infra/conf/common.go b/infra/conf/common.go index fd6e732a..1cda2459 100644 --- a/infra/conf/common.go +++ b/infra/conf/common.go @@ -2,6 +2,7 @@ package conf import ( "encoding/json" + "fmt" "strconv" "strings" @@ -258,6 +259,18 @@ type Int32Range struct { To int32 } +func (v Int32Range) MarshalJSON() ([]byte, error) { + return json.Marshal(v.String()) +} + +func (v Int32Range) String() string { + if v.Left == v.Right { + return strconv.Itoa(int(v.Left)) + } else { + return fmt.Sprintf("%d-%d", v.Left, v.Right) + } +} + func (v *Int32Range) UnmarshalJSON(data []byte) error { defer v.ensureOrder() var str string