File tree 3 files changed +32
-2
lines changed
3 files changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -3,12 +3,25 @@ package badjson
3
3
import (
4
4
"bytes"
5
5
6
+ "github.com/sagernet/sing/common"
6
7
E "github.com/sagernet/sing/common/exceptions"
7
8
"github.com/sagernet/sing/common/json"
8
9
)
9
10
10
11
type JSONArray []any
11
12
13
+ func (a JSONArray ) IsEmpty () bool {
14
+ if len (a ) == 0 {
15
+ return true
16
+ }
17
+ return common .All (a , func (it any ) bool {
18
+ if valueInterface , valueMaybeEmpty := it .(isEmpty ); valueMaybeEmpty && valueInterface .IsEmpty () {
19
+ return true
20
+ }
21
+ return false
22
+ })
23
+ }
24
+
12
25
func (a JSONArray ) MarshalJSON () ([]byte , error ) {
13
26
return json .Marshal ([]any (a ))
14
27
}
Original file line number Diff line number Diff line change
1
+ package badjson
2
+
3
+ type isEmpty interface {
4
+ IsEmpty () bool
5
+ }
Original file line number Diff line number Diff line change @@ -15,11 +15,23 @@ type JSONObject struct {
15
15
linkedhashmap.Map [string , any ]
16
16
}
17
17
18
- func (m JSONObject ) MarshalJSON () ([]byte , error ) {
18
+ func (m * JSONObject ) IsEmpty () bool {
19
+ if m .Size () == 0 {
20
+ return true
21
+ }
22
+ return common .All (m .Entries (), func (it collections.MapEntry [string , any ]) bool {
23
+ if valueInterface , valueMaybeEmpty := it .Value .(isEmpty ); valueMaybeEmpty && valueInterface .IsEmpty () {
24
+ return true
25
+ }
26
+ return false
27
+ })
28
+ }
29
+
30
+ func (m * JSONObject ) MarshalJSON () ([]byte , error ) {
19
31
buffer := new (bytes.Buffer )
20
32
buffer .WriteString ("{" )
21
33
items := common .Filter (m .Entries (), func (it collections.MapEntry [string , any ]) bool {
22
- if valueObject , valueIsJSONObject := it .Value .(* JSONObject ); valueIsJSONObject && valueObject .IsEmpty () {
34
+ if valueInterface , valueMaybeEmpty := it .Value .(isEmpty ); valueMaybeEmpty && valueInterface .IsEmpty () {
23
35
return false
24
36
}
25
37
return true
You can’t perform that action at this time.
0 commit comments