public void SerializeAndDeserialize_CheckIfSerializationWorks_Works()
        {
            ItemList list = new ItemList("at0001");
            list.Items.Add(new DvText("at0002", new CString("#Test")));
            list.Items.Add(new DvCount("at0003", new CCount(25)));

            string json = JsonConvert.SerializeObject(list);

            JObject obj = (JObject)JsonConvert.DeserializeObject(json);

            ItemList deserializedList = new ItemList();
            deserializedList.ReadJson(obj);

            Assert.AreEqual(list.Items[0].TypeName, deserializedList.Items[0].TypeName);
            Assert.AreEqual(list.Items[1].TypeName, deserializedList.Items[1].TypeName);
            Assert.AreEqual(((DvText)list.Items[0]).Value.Value, ((DvText)deserializedList.Items[0]).Value.Value);
            Assert.AreEqual(((DvCount)list.Items[1]).Value.Magnitude, ((DvCount)deserializedList.Items[1]).Value.Magnitude);

            deserializedList.Dispose();
            list.Dispose();
        }
Beispiel #2
0
        public static void GenerateRandomObjects(int count, string file)
        {
            Random rnd = new Random();

            using (StreamWriter outfile = new StreamWriter(file, false, Encoding.UTF8))
            {
                for (int i = 0; i < count; i++)
                {
                    ItemList list = new ItemList("someName " + rnd.Next().ToString());
                    list.Rev = null;
                    list.Id = Guid.NewGuid().ToString();

                    int itemCount = rnd.Next(100);

                    for (int j = 0; j < itemCount; j++)
                    {
                        OMBase item = null;
                        if (j % 2 == 0)
                        {
                            item = new DvText("textNode_id_" + rnd.Next().ToString(), new CString(rnd.Next().ToString() + " test string " + rnd.Next().ToString()));
                        }
                        else
                        {
                            item = new DvCount("countNode_id_" + rnd.Next().ToString(), new CCount(rnd.Next()));
                        }

                        item.Rev = null;
                        item.Id = Guid.NewGuid().ToString();

                        list.Items.Add(item);
                    }

                    outfile.WriteLine(JsonConvert.SerializeObject(list, Formatting.None));

                    list.Dispose();
                    list = null;
                }
            }
        }