예제 #1
0
    public void PopulateStore()
    {
      Store s = new Store();
      s.Color = StoreColor.Red;
      s.product = new List<Product>
        {
          new Product
            {
              ExpiryDate = new DateTime(2000, 12, 3, 0, 0, 0, DateTimeKind.Utc),
              Name = "ProductName!",
              Price = 9.9m
            }
        };
      s.Width = 99.99d;
      s.Mottos = new List<string> { "Can do!", "We deliver!" };

      string json = @"{
  ""Color"": 2,
  ""Establised"": ""\/Date(1264122061000+0000)\/"",
  ""Width"": 99.99,
  ""Employees"": 999,
  ""RoomsPerFloor"": [
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9
  ],
  ""Open"": false,
  ""Symbol"": ""@"",
  ""Mottos"": [
    ""Fail whale""
  ],
  ""Cost"": 100980.1,
  ""Escape"": ""\r\n\t\f\b?{\\r\\n\""'"",
  ""product"": [
    {
      ""Name"": ""ProductName!"",
      ""ExpiryDate"": ""\/Date(975801600000)\/"",
      ""Price"": 9.9,
      ""Sizes"": null
    }
  ]
}";

      JsonConvert.PopulateObject(json, s, new JsonSerializerSettings
        {
          ObjectCreationHandling = ObjectCreationHandling.Replace
        });

      Assert.AreEqual(1, s.Mottos.Count);
      Assert.AreEqual("Fail whale", s.Mottos[0]);
      Assert.AreEqual(1, s.product.Count);

      //Assert.AreEqual("James", p.Name);
    }
예제 #2
0
    public void WriteSerializedStore()
    {
      MemoryStream ms = new MemoryStream();
      BsonWriter writer = new BsonWriter(ms);

      Store s1 = new Store();
      s1.Color = StoreColor.White;
      s1.Cost = 999.59m;
      s1.Employees = int.MaxValue - 1;
      s1.Open = true;
      s1.product.Add(new Product
                       {
                         ExpiryDate = new DateTime(2000, 9, 28, 3, 59, 58, DateTimeKind.Local),
                         Name = "BSON!",
                         Price = -0.1m,
                         Sizes = new [] { "First", "Second" }
                       });
      s1.Establised = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local);

      JsonSerializer serializer = new JsonSerializer();
      serializer.Serialize(writer, s1);

      ms.Seek(0, SeekOrigin.Begin);
      BsonReader reader = new BsonReader(ms);
      Store s2 = (Store)serializer.Deserialize(reader, typeof (Store));

      Assert.AreNotEqual(s1, s2);
      Assert.AreEqual(s1.Color, s2.Color);
      Assert.AreEqual(s1.Cost, s2.Cost);
      Assert.AreEqual(s1.Employees, s2.Employees);
      Assert.AreEqual(s1.Escape, s2.Escape);
      Assert.AreEqual(s1.Establised, s2.Establised);
      Assert.AreEqual(s1.Mottos.Count, s2.Mottos.Count);
      Assert.AreEqual(s1.Mottos.First(), s2.Mottos.First());
      Assert.AreEqual(s1.Mottos.Last(), s2.Mottos.Last());
      Assert.AreEqual(s1.Open, s2.Open);
      Assert.AreEqual(s1.product.Count, s2.product.Count);
      Assert.AreEqual(s1.RoomsPerFloor.Length, s2.RoomsPerFloor.Length);
      Assert.AreEqual(s1.Symbol, s2.Symbol);
      Assert.AreEqual(s1.Width, s2.Width);

      MemoryStream ms1 = new MemoryStream();
      BsonWriter writer1 = new BsonWriter(ms1);

      serializer.Serialize(writer1, s1);

      CollectionAssert.AreEquivalent(ms.ToArray(), ms1.ToArray());

      string s = JsonConvert.SerializeObject(s1);
      byte[] textData = Encoding.UTF8.GetBytes(s);

      int l1 = textData.Length;
      int l2 = ms.ToArray().Length;

      Console.WriteLine(l1);
      Console.WriteLine(l2);
    }