Skip to content

Commit 863857f

Browse files
MikeDobrzangnatogryzclient-engineering-bot
authored
Fix serializer settings issues (#73)
* fix: upgrade & add newtonsoft * fix: prevent setting from being overriden * fix: implement a raw SubscribeMessage * fix: add missing meta files * fix: updated ProjectSettings.asset * PubNub SDK v6.0.5 release. Co-authored-by: Michał Dobrzański <[email protected]> Co-authored-by: Client Engineering Bot <[email protected]>
1 parent 5832afc commit 863857f

27 files changed

+248
-102
lines changed

.pubnub.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
---
2-
version: v6.0.4
2+
version: v6.0.5
33
changelog:
4+
- date: 2022-09-02
5+
version: v6.0.5
6+
changes:
7+
- type: bug
8+
text: "Json.net stores its settings globally, which means any changes in the client application will affect the behaviour of the SDK. The SDK will now use the initial default settings."
49
- date: 2022-07-27
510
version: v6.0.4
611
changes:
@@ -648,7 +653,7 @@ sdks:
648653
distribution-type: package
649654
distribution-repository: git release
650655
package-name: PubNub.unitypackage
651-
location: https://github.com/pubnub/unity/releases/download/v6.0.4/PubNub.unitypackage
656+
location: https://github.com/pubnub/unity/releases/download/v6.0.5/PubNub.unitypackage
652657
requires:
653658
-
654659
name: "UnityEditor"
@@ -815,7 +820,7 @@ sdks:
815820
distribution-type: package
816821
distribution-repository: git release
817822
package-name: PubNub.unitypackage
818-
location: https://github.com/pubnub/unity/releases/download/v6.0.4/PubNub.unitypackage
823+
location: https://github.com/pubnub/unity/releases/download/v6.0.5/PubNub.unitypackage
819824
requires:
820825
-
821826
name: "UnityEditor"

PubNubUnity/Assets/PubNub.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PubNubUnity/Assets/PubNub/Models/Consumer/PubSub/SubscribeMessage.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,33 @@
22

33
namespace PubNubAPI
44
{
5-
public class SubscribeMessage
5+
public class SubscribeMessage : SubscribeMessage<object> {
6+
internal SubscribeMessage(string shard, string subscriptionMatch, string channel, object payload,
7+
string flags, string issuingClientId, string subscribeKey, long sequenceNumber,
8+
TimetokenMetadata originatingTimetoken,
9+
TimetokenMetadata publishMetadata, object userMetadata, int messageType) : base(shard, subscriptionMatch,
10+
channel, payload, flags, issuingClientId, subscribeKey, sequenceNumber, originatingTimetoken,
11+
publishMetadata, userMetadata, messageType) {
12+
}
13+
}
14+
15+
// Message class variant that does not deserialize the message
16+
public class SubscribeVerbatimMessage : SubscribeMessage<string> {
17+
internal SubscribeVerbatimMessage(string shard, string subscriptionMatch, string channel, string payload,
18+
string flags, string issuingClientId, string subscribeKey, long sequenceNumber,
19+
TimetokenMetadata originatingTimetoken,
20+
TimetokenMetadata publishMetadata, object userMetadata, int messageType) : base(shard, subscriptionMatch,
21+
channel, payload, flags, issuingClientId, subscribeKey, sequenceNumber, originatingTimetoken,
22+
publishMetadata, userMetadata, messageType) {
23+
}
24+
}
25+
26+
public class SubscribeMessage<T>
627
{
728
private string a { get; set;} //JSON shard;
829
private string b { get; set;} //JSON subscriptionMatch
930
private string c { get; set;} //JSON channel
10-
private object d { get; set;} //JSON payload
31+
private T d { get; set;} //JSON payload
1132
private int e { get; set;} //JSON Message Type (1: Signal, 2: MessageObjects, 3: MessageActions)
1233
private string f { get; set;} //JSON flags
1334
private string i { get; set;} //JSON issuingClientId
@@ -17,7 +38,7 @@ public class SubscribeMessage
1738
private TimetokenMetadata p { get; set;} //JSON publishMetadata
1839
private object u { get; set;} //JSON userMetadata
1940

20-
internal SubscribeMessage(string shard, string subscriptionMatch, string channel, object payload,
41+
internal SubscribeMessage(string shard, string subscriptionMatch, string channel, T payload,
2142
string flags, string issuingClientId, string subscribeKey, long sequenceNumber, TimetokenMetadata originatingTimetoken,
2243
TimetokenMetadata publishMetadata, object userMetadata, int messageType
2344
)
@@ -60,7 +81,7 @@ public int MessageType{
6081
}
6182
}
6283

63-
public object Payload{
84+
public T Payload{
6485
get{
6586
return d;
6687
}

PubNubUnity/Assets/PubNub/PubNubUnity/PubNubUnityBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace PubNubAPI
77
public class PubNubUnityBase
88
{
99
protected Counter publishMessageCounter;
10-
private const string build = "6.0.4";
10+
private const string build = "6.0.5";
1111
private string pnsdkVersion = string.Format ("PubNub-CSharp-Unity/{0}", build);
1212

1313
public string Version {

PubNubUnity/Assets/PubNub/Serialization/JSONSerializer.cs

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Reflection;
2121
using System.Text.RegularExpressions;
2222
using System.Globalization;
23+
using System.Runtime.Serialization;
2324
using System.Text;
2425

2526
namespace PubNubAPI
@@ -220,12 +221,38 @@ public Dictionary<string, object> DeserializeToDictionaryOfObject (string jsonSt
220221
}
221222
}
222223
#elif (USE_NEWTONSOFT_JSON)
223-
public class NewtonsoftJsonSerializer : IJsonLibrary
224-
{
225-
224+
public class NewtonsoftJsonSerializer : IJsonLibrary {
226225
readonly PubNubUnityBase pnUnityBase;
227-
public NewtonsoftJsonSerializer(PubNubUnityBase pnUnityBase){
226+
227+
private readonly JsonSerializerSettings defaultSettings;
228+
229+
public NewtonsoftJsonSerializer(PubNubUnityBase pnUnityBase) {
230+
228231
this.pnUnityBase = pnUnityBase;
232+
defaultSettings = new JsonSerializerSettings() {
233+
Context = new StreamingContext(),
234+
Culture = CultureInfo.InvariantCulture,
235+
ReferenceLoopHandling = ReferenceLoopHandling.Error,
236+
MissingMemberHandling = MissingMemberHandling.Ignore,
237+
NullValueHandling = NullValueHandling.Include,
238+
DefaultValueHandling = DefaultValueHandling.Include,
239+
ObjectCreationHandling = ObjectCreationHandling.Auto,
240+
PreserveReferencesHandling = PreserveReferencesHandling.None,
241+
ConstructorHandling = ConstructorHandling.Default,
242+
TypeNameHandling = TypeNameHandling.None,
243+
MetadataPropertyHandling = MetadataPropertyHandling.Default,
244+
Formatting = Formatting.None,
245+
DateFormatHandling = DateFormatHandling.IsoDateFormat,
246+
DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind,
247+
DateParseHandling = DateParseHandling.DateTime,
248+
FloatParseHandling = FloatParseHandling.Double,
249+
FloatFormatHandling = FloatFormatHandling.String,
250+
StringEscapeHandling = StringEscapeHandling.Default,
251+
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
252+
CheckAdditionalContent = false,
253+
DateFormatString = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK",
254+
MaxDepth = 64
255+
};
229256
}
230257

231258
public bool IsArrayCompatible (string jsonString)
@@ -240,7 +267,7 @@ public bool IsDictionaryCompatible (string jsonString)
240267

241268
public string SerializeToJsonString (object objectToSerialize)
242269
{
243-
string json = JsonConvert.SerializeObject (objectToSerialize);
270+
string json = JsonConvert.SerializeObject(objectToSerialize, defaultSettings);
244271
return EncodeNonAsciiCharacters(json);
245272
}
246273

@@ -250,8 +277,8 @@ public List<object> DeserializeToListOfObject (string jsonString)
250277
#if (ENABLE_PUBNUB_LOGGING)
251278
pnUnityBase.PNLog.WriteToLog (string.Format ("DeserializeToListOfObject: jsonString: {0}", jsonString), PNLoggingMethod.LevelInfo);
252279
#endif
253-
254-
var output = JsonConvert.DeserializeObject<List<object>> (jsonString);
280+
281+
var output = JsonConvert.DeserializeObject<List<object>>(jsonString, defaultSettings);
255282
return output;
256283
}
257284

@@ -261,7 +288,7 @@ public object DeserializeToObject (string jsonString)
261288
pnUnityBase.PNLog.WriteToLog (string.Format ("DeserializeToObject: jsonString: {0}", jsonString), PNLoggingMethod.LevelInfo);
262289
#endif
263290
jsonString = DecodeEncodedNonAsciiCharacters(jsonString);
264-
var output = JsonConvert.DeserializeObject<object> (jsonString);
291+
var output = JsonConvert.DeserializeObject<object> (jsonString, defaultSettings);
265292

266293
#if (ENABLE_PUBNUB_LOGGING)
267294
pnUnityBase.PNLog.WriteToLog (string.Format("DeserializeToObject: type {0} decoded jsonString: {1}", output.GetType(), jsonString), PNLoggingMethod.LevelInfo);
@@ -352,9 +379,9 @@ private object deserializeToDictionary(string jo, bool isArray=false)
352379
if (!isArray)
353380
{
354381

355-
var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(jo);
382+
var values = JsonConvert.DeserializeObject<Dictionary<string, object>>(jo, defaultSettings);
356383
#if (ENABLE_PUBNUB_LOGGING)
357-
pnUnityBase.PNLog.WriteToLog (string.Format("JsonConvert.SerializeObject(values) {0}", JsonConvert.SerializeObject(values)), PNLoggingMethod.LevelInfo);
384+
pnUnityBase.PNLog.WriteToLog (string.Format("JsonConvert.SerializeObject(values) {0}", JsonConvert.SerializeObject(values, defaultSettings)), PNLoggingMethod.LevelInfo);
358385
#endif
359386

360387
var values2 = new Dictionary<string, object>();
@@ -387,9 +414,9 @@ private object deserializeToDictionary(string jo, bool isArray=false)
387414
else
388415
{
389416

390-
var values = JsonConvert.DeserializeObject<List<object>>(jo);
417+
var values = JsonConvert.DeserializeObject<List<object>>(jo, defaultSettings);
391418
#if (ENABLE_PUBNUB_LOGGING)
392-
pnUnityBase.PNLog.WriteToLog (string.Format("2: JsonConvert.SerializeObject(values) {0}", JsonConvert.SerializeObject(values)), PNLoggingMethod.LevelInfo);
419+
pnUnityBase.PNLog.WriteToLog (string.Format("2: JsonConvert.SerializeObject(values) {0}", JsonConvert.SerializeObject(values, defaultSettings)), PNLoggingMethod.LevelInfo);
393420
#endif
394421

395422
Type whatType = typeof(object);
@@ -454,16 +481,16 @@ private object deserializeToDictionary(string jo, bool isArray=false)
454481

455482
public T Deserialize<T> (string jsonString)
456483
{
457-
var output = JsonConvert.DeserializeObject<T> (jsonString);
484+
var output = JsonConvert.DeserializeObject<T> (jsonString, defaultSettings);
458485
return output;
459486
}
460487

461-
public Dictionary<string, object> DeserializeToDictionaryOfObject (string jsonString)
462-
{
463-
var output = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
464-
488+
public Dictionary<string, object> DeserializeToDictionaryOfObject(string jsonString) {
489+
var output =
490+
JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString, defaultSettings);
491+
465492
return output;
466-
}
493+
}
467494
}
468495
#elif (USE_SimpleJSON)
469496
public class SimpleJSONSerializer : IJsonLibrary

PubNubUnity/Assets/PubNub/ThirdParty.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PubNubUnity/Assets/Resources.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PubNubUnity/Packages/manifest.json

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
{
22
"dependencies": {
3-
"com.unity.ads": "2.0.8",
4-
"com.unity.analytics": "3.2.3",
5-
"com.unity.collab-proxy": "1.2.15",
6-
"com.unity.package-manager-ui": "2.0.13",
7-
"com.unity.purchasing": "2.0.3",
8-
"com.unity.textmeshpro": "1.4.1",
3+
"com.unity.2d.sprite": "1.0.0",
4+
"com.unity.2d.tilemap": "1.0.0",
5+
"com.unity.ads": "3.7.5",
6+
"com.unity.analytics": "3.6.12",
7+
"com.unity.collab-proxy": "1.15.13",
8+
"com.unity.ide.rider": "2.0.7",
9+
"com.unity.ide.visualstudio": "2.0.14",
10+
"com.unity.ide.vscode": "1.2.5",
11+
"com.unity.purchasing": "4.1.3",
12+
"com.unity.test-framework": "1.1.31",
13+
"com.unity.textmeshpro": "3.0.6",
14+
"com.unity.timeline": "1.4.8",
15+
"com.unity.ugui": "1.0.0",
16+
"com.unity.xr.legacyinputhelpers": "2.1.9",
917
"com.unity.modules.ai": "1.0.0",
18+
"com.unity.modules.androidjni": "1.0.0",
1019
"com.unity.modules.animation": "1.0.0",
1120
"com.unity.modules.assetbundle": "1.0.0",
1221
"com.unity.modules.audio": "1.0.0",
@@ -35,6 +44,7 @@
3544
"com.unity.modules.video": "1.0.0",
3645
"com.unity.modules.vr": "1.0.0",
3746
"com.unity.modules.wind": "1.0.0",
38-
"com.unity.modules.xr": "1.0.0"
47+
"com.unity.modules.xr": "1.0.0",
48+
"com.unity.nuget.newtonsoft-json": "3.0.2"
3949
}
4050
}
-3.62 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)