示例#1
0
        public void When_deserializing_Then_works()
        {
            var result = new DataContractJsonSerializerHelper().Deserialize <TestClass>(@"{""Value"":""Wot""}");

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Value, Is.EqualTo("Wot"));
        }
示例#2
0
        public void When_serializing_Then_works()
        {
            string result = new DataContractJsonSerializerHelper().Serialize(new TestClass()
            {
                Value = "Wot"
            });

            Assert.That(result, Is.EqualTo(@"{""Value"":""Wot""}"));
        }
示例#3
0
 public object GetDeserializedObject(object obj, Type targetType)
 {
     if (obj is ListDictionaryArray)
     {
         var array = (ListDictionaryArray)obj;
         var dict  = array.SelectMany(pair => pair).ToDictionary(pair => pair.Key, pair => pair.Value);
         var json  = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(dict);
         return(DataContractJsonSerializerHelper.GetObject <T>(json, new DataContractJsonSerializerSettings {
             UseSimpleDictionaryFormat = true
         }));
     }
     return(obj);
 }
示例#4
0
 public object GetObjectToSerialize(object obj, Type targetType)
 {
     if (obj.GetType() == typeof(T))
     {
         var json = DataContractJsonSerializerHelper.GetJson((T)obj, new DataContractJsonSerializerSettings {
             UseSimpleDictionaryFormat = true
         });
         var dict  = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize <Dictionary <string, string> >(json);
         var array = new ListDictionaryArray();
         array.AddRange(dict.Select(pair => new[] { pair }.ToDictionary(p => p.Key, p => p.Value)));
         return(array);
     }
     return(obj);
 }
示例#5
0
    public static void Test()
    {
        var a1 = new A()
        {
            s1 = "A"
        };
        var b1 = new B()
        {
            s2 = "B"
        };
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IObject));
        var jsona1 = DataContractJsonSerializerHelper.GetJson(a1, serializer);
        var jsonb1 = DataContractJsonSerializerHelper.GetJson(b1, serializer);

        Debug.WriteLine(jsona1);
        Debug.WriteLine(jsonb1);
        var newa1 = DataContractJsonSerializerHelper.GetObject <IObject>(jsona1, serializer);

        Debug.Assert(newa1.GetType() == a1.GetType());     // No assert
    }
 /// <summary>
 /// Overrides the default data contract json serializer.
 /// </summary>
 /// <param name="serializer">The data contract json serializer helper.</param>
 public static void Override(DataContractJsonSerializerHelper serializer)
 {
     Validate.Is.Not.Null(dataContractJsonSerializerHelper, "dataContractJsonSerializerHelper");
     Serializers.dataContractJsonSerializerHelper = serializer;
 }