11package badjson
22
33import (
4+ "context"
45 "os"
56 "reflect"
67
@@ -9,100 +10,100 @@ import (
910 "github.com/sagernet/sing/common/json"
1011)
1112
12- func Omitempty [T any ](value T ) (T , error ) {
13+ func Omitempty [T any ](ctx context. Context , value T ) (T , error ) {
1314 objectContent , err := json .Marshal (value )
1415 if err != nil {
1516 return common .DefaultValue [T ](), E .Cause (err , "marshal object" )
1617 }
17- rawNewObject , err := Decode (objectContent )
18+ rawNewObject , err := Decode (ctx , objectContent )
1819 if err != nil {
1920 return common .DefaultValue [T ](), err
2021 }
21- newObjectContent , err := json .Marshal ( rawNewObject )
22+ newObjectContent , err := json .MarshalContext ( ctx , rawNewObject )
2223 if err != nil {
2324 return common .DefaultValue [T ](), E .Cause (err , "marshal new object" )
2425 }
2526 var newObject T
26- err = json .Unmarshal ( newObjectContent , & newObject )
27+ err = json .UnmarshalContext ( ctx , newObjectContent , & newObject )
2728 if err != nil {
2829 return common .DefaultValue [T ](), E .Cause (err , "unmarshal new object" )
2930 }
3031 return newObject , nil
3132}
3233
33- func Merge [T any ](source T , destination T , disableAppend bool ) (T , error ) {
34- rawSource , err := json .Marshal ( source )
34+ func Merge [T any ](ctx context. Context , source T , destination T , disableAppend bool ) (T , error ) {
35+ rawSource , err := json .MarshalContext ( ctx , source )
3536 if err != nil {
3637 return common .DefaultValue [T ](), E .Cause (err , "marshal source" )
3738 }
38- rawDestination , err := json .Marshal ( destination )
39+ rawDestination , err := json .MarshalContext ( ctx , destination )
3940 if err != nil {
4041 return common .DefaultValue [T ](), E .Cause (err , "marshal destination" )
4142 }
42- return MergeFrom [T ](rawSource , rawDestination , disableAppend )
43+ return MergeFrom [T ](ctx , rawSource , rawDestination , disableAppend )
4344}
4445
45- func MergeFromSource [T any ](rawSource json.RawMessage , destination T , disableAppend bool ) (T , error ) {
46+ func MergeFromSource [T any ](ctx context. Context , rawSource json.RawMessage , destination T , disableAppend bool ) (T , error ) {
4647 if rawSource == nil {
4748 return destination , nil
4849 }
49- rawDestination , err := json .Marshal ( destination )
50+ rawDestination , err := json .MarshalContext ( ctx , destination )
5051 if err != nil {
5152 return common .DefaultValue [T ](), E .Cause (err , "marshal destination" )
5253 }
53- return MergeFrom [T ](rawSource , rawDestination , disableAppend )
54+ return MergeFrom [T ](ctx , rawSource , rawDestination , disableAppend )
5455}
5556
56- func MergeFromDestination [T any ](source T , rawDestination json.RawMessage , disableAppend bool ) (T , error ) {
57+ func MergeFromDestination [T any ](ctx context. Context , source T , rawDestination json.RawMessage , disableAppend bool ) (T , error ) {
5758 if rawDestination == nil {
5859 return source , nil
5960 }
60- rawSource , err := json .Marshal ( source )
61+ rawSource , err := json .MarshalContext ( ctx , source )
6162 if err != nil {
6263 return common .DefaultValue [T ](), E .Cause (err , "marshal source" )
6364 }
64- return MergeFrom [T ](rawSource , rawDestination , disableAppend )
65+ return MergeFrom [T ](ctx , rawSource , rawDestination , disableAppend )
6566}
6667
67- func MergeFrom [T any ](rawSource json.RawMessage , rawDestination json.RawMessage , disableAppend bool ) (T , error ) {
68- rawMerged , err := MergeJSON (rawSource , rawDestination , disableAppend )
68+ func MergeFrom [T any ](ctx context. Context , rawSource json.RawMessage , rawDestination json.RawMessage , disableAppend bool ) (T , error ) {
69+ rawMerged , err := MergeJSON (ctx , rawSource , rawDestination , disableAppend )
6970 if err != nil {
7071 return common .DefaultValue [T ](), E .Cause (err , "merge options" )
7172 }
7273 var merged T
73- err = json .Unmarshal ( rawMerged , & merged )
74+ err = json .UnmarshalContext ( ctx , rawMerged , & merged )
7475 if err != nil {
7576 return common .DefaultValue [T ](), E .Cause (err , "unmarshal merged options" )
7677 }
7778 return merged , nil
7879}
7980
80- func MergeJSON (rawSource json.RawMessage , rawDestination json.RawMessage , disableAppend bool ) (json.RawMessage , error ) {
81+ func MergeJSON (ctx context. Context , rawSource json.RawMessage , rawDestination json.RawMessage , disableAppend bool ) (json.RawMessage , error ) {
8182 if rawSource == nil && rawDestination == nil {
8283 return nil , os .ErrInvalid
8384 } else if rawSource == nil {
8485 return rawDestination , nil
8586 } else if rawDestination == nil {
8687 return rawSource , nil
8788 }
88- source , err := Decode (rawSource )
89+ source , err := Decode (ctx , rawSource )
8990 if err != nil {
9091 return nil , E .Cause (err , "decode source" )
9192 }
92- destination , err := Decode (rawDestination )
93+ destination , err := Decode (ctx , rawDestination )
9394 if err != nil {
9495 return nil , E .Cause (err , "decode destination" )
9596 }
9697 if source == nil {
97- return json .Marshal ( destination )
98+ return json .MarshalContext ( ctx , destination )
9899 } else if destination == nil {
99100 return json .Marshal (source )
100101 }
101102 merged , err := mergeJSON (source , destination , disableAppend )
102103 if err != nil {
103104 return nil , err
104105 }
105- return json .Marshal ( merged )
106+ return json .MarshalContext ( ctx , merged )
106107}
107108
108109func mergeJSON (anySource any , anyDestination any , disableAppend bool ) (any , error ) {
0 commit comments