Exemple #1
0
        public void CanConvertInterfaceWithDeserializerBinding()
        {
            GlobalSettings.DeserializationSettings.TypeBindings.Remove <ITestObject>();

            var deserializerSettings = new DeserializerSettings();

            deserializerSettings.TypeBindings.Add <ITestObject, TestObject>();

            var str         = "{\"Integer\":12}";
            var strWithType = "{\"$type\":\"SFJsonTests.ITestObject, SFJsonTests\",\"Integer\":12}";

            Console.WriteLine(str);
            Console.WriteLine(strWithType);

            var strDeserialized = _deserializer.Deserialize <ITestObject>(str, deserializerSettings);

            Assert.NotNull(strDeserialized);
            Assert.IsInstanceOf <TestObject>(strDeserialized);
            Assert.AreEqual(12, strDeserialized.Integer);

            var strWithTypeDeserialized = _deserializer.Deserialize <ITestObject>(strWithType, deserializerSettings);

            Console.WriteLine(_serializer.Serialize(strWithTypeDeserialized));
            Assert.NotNull(strWithTypeDeserialized);
            Assert.IsInstanceOf <TestObject>(strWithTypeDeserialized);
            Assert.AreEqual(12, strWithTypeDeserialized.Integer);
        }
 private void Tokenize(string stringToSerialize, DeserializerSettings deserializerSettings = null)
 {
     _stringToDeserialize = stringToSerialize;
     _lastTokenization    = _tokenizer.Tokenize(_stringToDeserialize, new SettingsManager {
         DeserializationSettings = deserializerSettings
     });
 }
 /// <summary>
 /// Converts a JSON string to an instance of type <c>T</c>.
 /// </summary>
 /// <param name="instance"></param>
 /// <param name="stringToSerialize"></param>
 /// <param name="deserializerSettings"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns>
 /// A deserialized instance of type <c>T</c>
 /// </returns>
 /// <exception cref="DeserializationException">
 /// Deserialization errors will contain the root <see cref="JsonToken"/> from
 /// the tokenization phase.
 /// </exception>
 public T Deserialize <T>(object instance, string stringToSerialize, DeserializerSettings deserializerSettings = null)
 {
     Tokenize(stringToSerialize, deserializerSettings);
     try
     {
         var obj = _lastTokenization.GetValue <T>(instance);
         return(obj);
     }
     catch (DeserializationException e)
     {
         e.Token = _lastTokenization;
         throw;
     }
     catch (Exception e)
     {
         throw new DeserializationException($"An error occured during deserialization. {_lastTokenization}.{_lastTokenization.Name}", _lastTokenization, e);
     }
 }
        public void SkipNullKeyInDictionaryWithSettings()
        {
            var str                  = "{\"Dictionary\":{null:2,\"3\":4,\"5\":6}}";
            var strWithType          = "{\"$type\":\"SFJsonTests.ObjectWithDictionary, SFJsonTests\",\"Dictionary\":{\"$type\":\"System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[System.Int32, System.Private.CoreLib]], System.Private.CoreLib\",null:2,\"3\":4,\"5\":6}}";
            var deserializerSettings = new DeserializerSettings()
            {
                SkipNullKeysInKeyValuedCollections = true
            };

            var strDeserialized = _deserializer.Deserialize <ObjectWithDictionary>(str, deserializerSettings);

            Assert.NotNull(strDeserialized);
            Assert.NotNull(strDeserialized.Dictionary);
//            Assert.AreEqual(2, strDeserialized.Dictionary.Count);
            var strWithTypeDeserialized = _deserializer.Deserialize <ObjectWithDictionary>(strWithType, deserializerSettings);

            Assert.NotNull(strWithTypeDeserialized);
            Assert.NotNull(strWithTypeDeserialized.Dictionary);
//            Assert.AreEqual(2, strWithTypeDeserialized.Dictionary.Count);
        }
 static GlobalSettings()
 {
     DeserializationSettings = new DeserializerSettings();
     SerializationSettings   = new SerializerSettings();
 }
 /// <summary>
 /// Converts a JSON string to an instance of <pararef name="type"/>.
 /// </summary>
 /// <param name="type"></param>
 /// <param name="instance"></param>
 /// <param name="stringToSerialize"></param>
 /// <param name="deserializerSettings"></param>
 /// <returns>
 /// A deserialized <c>object</c> instance and may be converted
 /// to <paramref name="type"/>.
 /// </returns>
 /// <exception cref="DeserializationException">
 /// Deserialization errors will contain the root <see cref="JsonToken"/> from
 /// the tokenization phase.
 /// </exception>
 public object Deserialize(Type type, object instance, string stringToSerialize, DeserializerSettings deserializerSettings = null)
 {
     Tokenize(stringToSerialize, deserializerSettings);
     try
     {
         var obj = _lastTokenization.GetValue(type, instance);
         return(obj);
     }
     catch (DeserializationException e)
     {
         e.Token = _lastTokenization;
         throw;
     }
     catch (Exception e)
     {
         throw new DeserializationException("An error occured during deserialization.", _lastTokenization, e);
     }
 }
 public object Deserialize(Type type, string stringToSerialize, DeserializerSettings deserializerSettings = null)
 {
     return(this.Deserialize(type, null, stringToSerialize, deserializerSettings));
 }
 public T Deserialize <T>(string stringToSerialize, DeserializerSettings deserializerSettings = null)
 {
     return(Deserialize <T>(null, stringToSerialize, deserializerSettings));
 }
Exemple #9
0
 /// <see cref="Deserializer.Deserialize"/>
 public static object DeserializeOnToInstance(Type type, object instance, string stringToDeserialize, DeserializerSettings deserializerSettings = null)
 {
     return(_deserializer.Deserialize(type, stringToDeserialize, deserializerSettings));
 }
Exemple #10
0
 /// <see cref="Deserializer.Deserialize"/>
 public static T DeserializeOnToInstance <T>(object instance, string stringToDeserialize, DeserializerSettings deserializerSettings = null)
 {
     return(_deserializer.Deserialize <T>(stringToDeserialize, deserializerSettings));
 }
Exemple #11
0
 /// <see cref="Deserializer.Deserialize"/>
 public static T Deserialize <T>(string stringToDeserialize, DeserializerSettings deserializerSettings = null)
 {
     return(_deserializer.Deserialize <T>(stringToDeserialize, deserializerSettings));
 }