Skip to content

Commit 205a90e

Browse files
authored
Merge pull request #20 from SourcePointUSA/DIA-2808-parsing-json-with-newtonsoft
DIA-2808 parsing json with Newtonsoft for Android
2 parents 3699ed4 + 39d6d84 commit 205a90e

File tree

7 files changed

+101
-35
lines changed

7 files changed

+101
-35
lines changed

Assets/ConsentManagementProvider/Scripts/json/JsonUnwrapper.cs

Lines changed: 84 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Text.Json;
4+
using System.IO;
5+
using NewtonsoftJson = Newtonsoft.Json;
6+
using Newtonsoft.Json.Linq;
37

48
namespace ConsentManagementProviderLib.Json
59
{
@@ -8,69 +12,118 @@ internal static class JsonUnwrapper
812
#region Android
913
public static SpConsents UnwrapSpConsentsAndroid(string json)
1014
{
11-
SpConsentsWrapperAndroid wrapped = JsonSerializer.Deserialize<SpConsentsWrapperAndroid>(json);
12-
SpGdprConsent unwrappedGdpr = null;
13-
SpCcpaConsent unwrappedCcpa = null;
14-
if (wrapped.gdpr!= null)
15+
try
1516
{
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);
1730
}
18-
if (wrapped.ccpa != null)
31+
catch (NewtonsoftJson.JsonException ex)
1932
{
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);
2138
}
22-
return new SpConsents(unwrappedGdpr, unwrappedCcpa);
2339
}
2440

2541
private static SpCcpaConsent UnwrapSpCcpaConsentAndroid(CcpaConsentWrapper wrappedCcpa)
2642
{
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+
3756
return new SpCcpaConsent(unwrapped);
3857
}
3958

4059
public static SpGdprConsent UnwrapSpGdprConsentAndroid(SpGdprConsentWrapperAndroid wrappedGdpr)
4160
{
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+
4267
GdprConsent unwrapped = new GdprConsent
4368
{
4469
uuid = wrappedGdpr.uuid,
4570
euconsent = wrappedGdpr.euconsent,
46-
TCData = wrappedGdpr.tcData,
71+
TCData = wrappedGdpr.tcData,
4772
grants = new Dictionary<string, SpVendorGrant>()
4873
};
49-
foreach (KeyValuePair<string, Dictionary<string, object>> vendorGrantWrapper in wrappedGdpr.grants)
74+
75+
foreach (var vendorGrantWrapper in wrappedGdpr.grants)
5076
{
51-
Dictionary<string, bool> purposeGrants = new Dictionary<string, bool>();
77+
var purposeGrants = new Dictionary<string, bool>();
5278
bool isGranted = false;
5379

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)
5786
{
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>());
6391
}
6492

6593
unwrapped.grants[vendorGrantWrapper.Key] = new SpVendorGrant(isGranted, purposeGrants);
6694
}
95+
6796
return new SpGdprConsent(unwrapped);
6897
}
6998

7099
public static SpCustomConsentAndroid UnwrapSpCustomConsentAndroid(string spConsentsJson)
71100
{
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+
}
74127
}
75128
#endregion
76129

Assets/ConsentManagementProvider/Scripts/json/wrappers/android/SpConsentsWrapperAndroid.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace ConsentManagementProviderLib.Json
44
{
55
internal class SpConsentsWrapperAndroid
66
{
7-
[JsonInclude] public CcpaConsentWrapper ccpa;
8-
[JsonInclude] public SpGdprConsentWrapperAndroid gdpr;
7+
[JsonInclude] public CcpaConsentWrapper? ccpa;
8+
[JsonInclude] public SpGdprConsentWrapperAndroid? gdpr;
99
}
1010
}

Assets/ConsentManagementProvider/Scripts/wrapper/Android/SpClientProxy.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ AndroidJavaObject onAction(AndroidJavaObject view, AndroidJavaObject actionType)
5858
void onConsentReady(string spConsents)
5959
{
6060
CmpDebugUtil.Log("I've reached the C# onConsentReady with json string: " + spConsents);
61+
try
62+
{
63+
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
64+
_spConsents = consents;
65+
ConsentMessenger.Broadcast<IOnConsentReady>(consents);
66+
}
67+
catch (Exception e)
68+
{
69+
ConsentMessenger.Broadcast<IOnConsentError>(e);
70+
}
6171
}
6272

6373
/**
@@ -72,7 +82,6 @@ void onSpFinished(string spConsents)
7282
{
7383
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
7484
_spConsents = consents;
75-
ConsentMessenger.Broadcast<IOnConsentReady>(consents);
7685
ConsentMessenger.Broadcast<IOnConsentSpFinished>(consents);
7786
}
7887
catch (Exception e)

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 2.1.4
2+
* [DIA-2808](https://sourcepoint.atlassian.net/browse/DIA-2808) DIA-2808 Implemented JSON parsing with Newtonsoft for Android [#20](https://github.com/SourcePointUSA/unity-sdk/pull/20)
3+
14
# 2.1.3
25
* Fixed `onConsentReady` - `onSpFinished` race condition for android
36

Packages/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"com.unity.ide.visualstudio": "2.0.16",
1212
"com.unity.ide.vscode": "1.2.5",
1313
"com.unity.mobile.android-logcat": "1.3.2",
14+
"com.unity.nuget.newtonsoft-json": "3.2.1",
1415
"com.unity.test-framework": "1.1.31",
1516
"com.unity.textmeshpro": "3.0.6",
1617
"com.unity.timeline": "1.6.4",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.sourcepoint.unitycmp",
33
"displayName": "Sourcepoint Consent Message Plugin",
4-
"version": "2.1.3",
4+
"version": "2.1.4",
55
"unity": "2021.3",
66
"description": "Native UI Privacy Manager for both GDPR and CCPA legislations.",
77
"author": {
397 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)