20
20
using System . Reflection ;
21
21
using System . Text . RegularExpressions ;
22
22
using System . Globalization ;
23
+ using System . Runtime . Serialization ;
23
24
using System . Text ;
24
25
25
26
namespace PubNubAPI
@@ -220,12 +221,38 @@ public Dictionary<string, object> DeserializeToDictionaryOfObject (string jsonSt
220
221
}
221
222
}
222
223
#elif ( USE_NEWTONSOFT_JSON )
223
- public class NewtonsoftJsonSerializer : IJsonLibrary
224
- {
225
-
224
+ public class NewtonsoftJsonSerializer : IJsonLibrary {
226
225
readonly PubNubUnityBase pnUnityBase ;
227
- public NewtonsoftJsonSerializer ( PubNubUnityBase pnUnityBase ) {
226
+
227
+ private readonly JsonSerializerSettings defaultSettings ;
228
+
229
+ public NewtonsoftJsonSerializer ( PubNubUnityBase pnUnityBase ) {
230
+
228
231
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
+ } ;
229
256
}
230
257
231
258
public bool IsArrayCompatible ( string jsonString )
@@ -240,7 +267,7 @@ public bool IsDictionaryCompatible (string jsonString)
240
267
241
268
public string SerializeToJsonString ( object objectToSerialize )
242
269
{
243
- string json = JsonConvert . SerializeObject ( objectToSerialize ) ;
270
+ string json = JsonConvert . SerializeObject ( objectToSerialize , defaultSettings ) ;
244
271
return EncodeNonAsciiCharacters ( json ) ;
245
272
}
246
273
@@ -250,8 +277,8 @@ public List<object> DeserializeToListOfObject (string jsonString)
250
277
#if ( ENABLE_PUBNUB_LOGGING )
251
278
pnUnityBase . PNLog . WriteToLog ( string . Format ( "DeserializeToListOfObject: jsonString: {0}" , jsonString ) , PNLoggingMethod . LevelInfo ) ;
252
279
#endif
253
-
254
- var output = JsonConvert . DeserializeObject < List < object > > ( jsonString ) ;
280
+
281
+ var output = JsonConvert . DeserializeObject < List < object > > ( jsonString , defaultSettings ) ;
255
282
return output ;
256
283
}
257
284
@@ -261,7 +288,7 @@ public object DeserializeToObject (string jsonString)
261
288
pnUnityBase . PNLog . WriteToLog ( string . Format ( "DeserializeToObject: jsonString: {0}" , jsonString ) , PNLoggingMethod . LevelInfo ) ;
262
289
#endif
263
290
jsonString = DecodeEncodedNonAsciiCharacters ( jsonString ) ;
264
- var output = JsonConvert . DeserializeObject < object > ( jsonString ) ;
291
+ var output = JsonConvert . DeserializeObject < object > ( jsonString , defaultSettings ) ;
265
292
266
293
#if ( ENABLE_PUBNUB_LOGGING )
267
294
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)
352
379
if ( ! isArray )
353
380
{
354
381
355
- var values = JsonConvert . DeserializeObject < Dictionary < string , object > > ( jo ) ;
382
+ var values = JsonConvert . DeserializeObject < Dictionary < string , object > > ( jo , defaultSettings ) ;
356
383
#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 ) ;
358
385
#endif
359
386
360
387
var values2 = new Dictionary < string , object > ( ) ;
@@ -387,9 +414,9 @@ private object deserializeToDictionary(string jo, bool isArray=false)
387
414
else
388
415
{
389
416
390
- var values = JsonConvert . DeserializeObject < List < object > > ( jo ) ;
417
+ var values = JsonConvert . DeserializeObject < List < object > > ( jo , defaultSettings ) ;
391
418
#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 ) ;
393
420
#endif
394
421
395
422
Type whatType = typeof ( object ) ;
@@ -454,16 +481,16 @@ private object deserializeToDictionary(string jo, bool isArray=false)
454
481
455
482
public T Deserialize < T > ( string jsonString )
456
483
{
457
- var output = JsonConvert . DeserializeObject < T > ( jsonString ) ;
484
+ var output = JsonConvert . DeserializeObject < T > ( jsonString , defaultSettings ) ;
458
485
return output ;
459
486
}
460
487
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
+
465
492
return output ;
466
- }
493
+ }
467
494
}
468
495
#elif ( USE_SimpleJSON )
469
496
public class SimpleJSONSerializer : IJsonLibrary
0 commit comments