1
+ using System ;
1
2
using System . Collections . Generic ;
2
3
using System . Text . Json ;
4
+ using System . IO ;
5
+ using NewtonsoftJson = Newtonsoft . Json ;
6
+ using Newtonsoft . Json . Linq ;
3
7
4
8
namespace ConsentManagementProviderLib . Json
5
9
{
@@ -8,69 +12,118 @@ internal static class JsonUnwrapper
8
12
#region Android
9
13
public static SpConsents UnwrapSpConsentsAndroid ( string json )
10
14
{
11
- SpConsentsWrapperAndroid wrapped = JsonSerializer . Deserialize < SpConsentsWrapperAndroid > ( json ) ;
12
- SpGdprConsent unwrappedGdpr = null ;
13
- SpCcpaConsent unwrappedCcpa = null ;
14
- if ( wrapped . gdpr != null )
15
+ try
15
16
{
16
- unwrappedGdpr = UnwrapSpGdprConsentAndroid ( wrapped . gdpr ) ;
17
+ using StringReader stringReader = new StringReader ( json ) ;
18
+ using NewtonsoftJson . JsonTextReader reader = new NewtonsoftJson . JsonTextReader ( stringReader ) ;
19
+
20
+ NewtonsoftJson . JsonSerializer serializer = new NewtonsoftJson . JsonSerializer ( ) ;
21
+ SpConsentsWrapperAndroid wrapped = serializer . Deserialize < SpConsentsWrapperAndroid > ( reader ) ;
22
+
23
+ if ( wrapped == null )
24
+ throw new NewtonsoftJson . JsonException ( "JSON deserialization returned null." ) ;
25
+
26
+ SpGdprConsent unwrappedGdpr = wrapped . gdpr != null ? UnwrapSpGdprConsentAndroid ( wrapped . gdpr ) : null ;
27
+ SpCcpaConsent unwrappedCcpa = wrapped . ccpa != null ? UnwrapSpCcpaConsentAndroid ( wrapped . ccpa ) : null ;
28
+
29
+ return new SpConsents ( unwrappedGdpr , unwrappedCcpa ) ;
17
30
}
18
- if ( wrapped . ccpa != null )
31
+ catch ( NewtonsoftJson . JsonException ex )
19
32
{
20
- unwrappedCcpa = UnwrapSpCcpaConsentAndroid ( wrapped . ccpa ) ;
33
+ throw new ApplicationException ( "Error deserializing JSON." , ex ) ;
34
+ }
35
+ catch ( Exception ex )
36
+ {
37
+ throw new ApplicationException ( "An error occurred during JSON unwrapping." , ex ) ;
21
38
}
22
- return new SpConsents ( unwrappedGdpr , unwrappedCcpa ) ;
23
39
}
24
40
25
41
private static SpCcpaConsent UnwrapSpCcpaConsentAndroid ( CcpaConsentWrapper wrappedCcpa )
26
42
{
27
- CcpaConsent unwrapped = new CcpaConsent ( uuid : wrappedCcpa . uuid ,
28
- status : wrappedCcpa . status ,
29
- uspstring : wrappedCcpa . uspstring ,
30
- rejectedVendors : wrappedCcpa . rejectedVendors ,
31
- rejectedCategories : wrappedCcpa . rejectedCategories ,
32
- childPmId : wrappedCcpa . childPmId ,
33
- applies : wrappedCcpa . applies ,
34
- signedLspa : wrappedCcpa . signedLspa ,
35
- webConsentPayload : wrappedCcpa . webConsentPayload ,
36
- null ) ;
43
+ CcpaConsent unwrapped = new CcpaConsent (
44
+ uuid : wrappedCcpa . uuid ,
45
+ status : wrappedCcpa . status ,
46
+ uspstring : wrappedCcpa . uspstring ,
47
+ rejectedVendors : wrappedCcpa . rejectedVendors ,
48
+ rejectedCategories : wrappedCcpa . rejectedCategories ,
49
+ childPmId : wrappedCcpa . childPmId ,
50
+ applies : wrappedCcpa . applies ,
51
+ signedLspa : wrappedCcpa . signedLspa ,
52
+ webConsentPayload : wrappedCcpa . webConsentPayload ,
53
+ null
54
+ ) ;
55
+
37
56
return new SpCcpaConsent ( unwrapped ) ;
38
57
}
39
58
40
59
public static SpGdprConsent UnwrapSpGdprConsentAndroid ( SpGdprConsentWrapperAndroid wrappedGdpr )
41
60
{
61
+ if ( wrappedGdpr == null )
62
+ throw new ArgumentNullException ( nameof ( wrappedGdpr ) , "The GDPR consent wrapper cannot be null." ) ;
63
+
64
+ if ( wrappedGdpr . grants == null )
65
+ throw new InvalidOperationException ( "The grants dictionary is null." ) ;
66
+
42
67
GdprConsent unwrapped = new GdprConsent
43
68
{
44
69
uuid = wrappedGdpr . uuid ,
45
70
euconsent = wrappedGdpr . euconsent ,
46
- TCData = wrappedGdpr . tcData ,
71
+ TCData = wrappedGdpr . tcData ,
47
72
grants = new Dictionary < string , SpVendorGrant > ( )
48
73
} ;
49
- foreach ( KeyValuePair < string , Dictionary < string , object > > vendorGrantWrapper in wrappedGdpr . grants )
74
+
75
+ foreach ( var vendorGrantWrapper in wrappedGdpr . grants )
50
76
{
51
- Dictionary < string , bool > purposeGrants = new Dictionary < string , bool > ( ) ;
77
+ var purposeGrants = new Dictionary < string , bool > ( ) ;
52
78
bool isGranted = false ;
53
79
54
- if ( vendorGrantWrapper . Value . ContainsKey ( "granted" ) )
55
- isGranted = ( ( JsonElement ) vendorGrantWrapper . Value [ "granted" ] ) . GetBoolean ( ) ;
56
- if ( vendorGrantWrapper . Value . ContainsKey ( "purposeGrants" ) )
80
+ var vendorGrantValue = JToken . FromObject ( vendorGrantWrapper . Value ) ;
81
+
82
+ if ( vendorGrantValue [ "granted" ] != null )
83
+ isGranted = vendorGrantValue [ "granted" ] . ToObject < bool > ( ) ;
84
+
85
+ if ( vendorGrantValue [ "purposeGrants" ] != null )
57
86
{
58
- JsonElement purposeGrantsElement = ( JsonElement ) vendorGrantWrapper . Value [ "purposeGrants" ] ;
59
- foreach ( JsonProperty purpGrant in purposeGrantsElement . EnumerateObject ( ) )
60
- {
61
- purposeGrants . Add ( purpGrant . Name , purpGrant . Value . GetBoolean ( ) ) ;
62
- }
87
+ var purposeGrantsElement = ( JObject ) vendorGrantValue [ "purposeGrants" ] ;
88
+
89
+ foreach ( var purposeGrant in purposeGrantsElement )
90
+ purposeGrants . Add ( purposeGrant . Key , purposeGrant . Value . ToObject < bool > ( ) ) ;
63
91
}
64
92
65
93
unwrapped . grants [ vendorGrantWrapper . Key ] = new SpVendorGrant ( isGranted , purposeGrants ) ;
66
94
}
95
+
67
96
return new SpGdprConsent ( unwrapped ) ;
68
97
}
69
98
70
99
public static SpCustomConsentAndroid UnwrapSpCustomConsentAndroid ( string spConsentsJson )
71
100
{
72
- SpCustomConsentAndroid customConsent = JsonSerializer . Deserialize < SpCustomConsentAndroid > ( spConsentsJson ) ;
73
- return customConsent ;
101
+ try
102
+ {
103
+ SpCustomConsentAndroid customConsent ;
104
+
105
+ using ( StringReader stringReader = new StringReader ( spConsentsJson ) )
106
+ using ( NewtonsoftJson . JsonTextReader jsonReader = new NewtonsoftJson . JsonTextReader ( stringReader ) )
107
+ {
108
+ NewtonsoftJson . JsonSerializer serializer = new NewtonsoftJson . JsonSerializer ( ) ;
109
+ customConsent = serializer . Deserialize < SpCustomConsentAndroid > ( jsonReader ) ;
110
+
111
+ if ( customConsent == null )
112
+ {
113
+ throw new InvalidOperationException ( "Deserialized custom consent is null." ) ;
114
+ }
115
+ }
116
+
117
+ return customConsent ;
118
+ }
119
+ catch ( NewtonsoftJson . JsonException ex )
120
+ {
121
+ throw new ApplicationException ( "Error deserializing custom consent JSON." , ex ) ;
122
+ }
123
+ catch ( Exception ex )
124
+ {
125
+ throw new ApplicationException ( "An error occurred during custom consent JSON unwrapping." , ex ) ;
126
+ }
74
127
}
75
128
#endregion
76
129
0 commit comments