1+ using System ;
2+ using System . Collections ;
13using System . IO ;
4+ using System . Linq ;
5+ using System . Reflection ;
26using System . Text ;
37using System . Threading ;
48using System . Threading . Tasks ;
59using Newtonsoft . Json ;
610using Newtonsoft . Json . Converters ;
11+ using Newtonsoft . Json . Serialization ;
712
813namespace Sentry . Internal
914{
15+ [ AttributeUsage ( AttributeTargets . Property ) ]
16+ internal class DontSerializeEmptyAttribute : Attribute { }
17+
1018 internal static class Json
1119 {
20+ private class ContractResolver : DefaultContractResolver
21+ {
22+ protected override JsonProperty CreateProperty ( MemberInfo member , MemberSerialization memberSerialization )
23+ {
24+ var jsonProperty = base . CreateProperty ( member , memberSerialization ) ;
25+ var property = jsonProperty . DeclaringType . GetProperty ( jsonProperty . UnderlyingName ) ;
26+
27+ // DontSerializeEmpty
28+ if ( jsonProperty . ShouldSerialize is null &&
29+ property ? . GetCustomAttribute < DontSerializeEmptyAttribute > ( ) is { } )
30+ {
31+ // Collections
32+ if ( property . PropertyType . GetInterfaces ( ) . Any ( i => i == typeof ( IEnumerable ) ) )
33+ {
34+ jsonProperty . ShouldSerialize = o =>
35+ {
36+ if ( property . GetValue ( o ) is IEnumerable value )
37+ {
38+ return ! value . Cast < object > ( ) . Any ( ) ;
39+ }
40+
41+ return true ;
42+ } ;
43+ }
44+ }
45+
46+ return jsonProperty ;
47+ }
48+ }
49+
1250 private static readonly Encoding Encoding = new UTF8Encoding ( false , true ) ;
1351 private static readonly StringEnumConverter StringEnumConverter = new StringEnumConverter ( ) ;
1452
@@ -19,7 +57,8 @@ internal static class Json
1957 ReferenceLoopHandling = ReferenceLoopHandling . Ignore ,
2058 Formatting = Formatting . None ,
2159 Converters = { StringEnumConverter } ,
22- DateFormatHandling = DateFormatHandling . IsoDateFormat
60+ DateFormatHandling = DateFormatHandling . IsoDateFormat ,
61+ ContractResolver = new ContractResolver ( )
2362 } ;
2463
2564 private static JsonTextWriter CreateWriter ( Stream stream ) => new JsonTextWriter (
0 commit comments