Пример #1
0
        public void SerializePersonToJsonStringTest()
        {
            var person = new Person
            {
                Name = "Jon Doe",
                Birth = new Birth { Date = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) },
                Email = new Email { Address = "*****@*****.**" },
                Gender = Gender.Male,
                Url = new Uri("http://www.facebook.com/jon.doe"),
            };

            var doc = JsonNode.FromObject(person);

            Assert.Equal(
@"{
  ""Name"": ""Jon Doe"",
  ""SSN"": null,
  ""Birth"": {
    ""Date"": ""1970-01-01Z""
  },
  ""Email"": {
    ""Address"": ""*****@*****.**""
  },
  ""Gender"": ""Male"",
  ""Connections"": null,
  ""Url"": ""http:\/\/www.facebook.com\/jon.doe""
}", doc.ToString());
        }
Пример #2
0
        public void SelfReferencingTest()
        {
            // Not sure how we'll determine what depth to serialize.

            var person1 = new Person
            {
                Name = "Jason"
            };

            var person2 = new Person
            {
                Name = "Joe"
            };

            var connection1 = new Connection { Source = person1, Target = person2 };
            var connection2 = new Connection { Source = person2, Target = person1 };

            person1.Connections = new[] { connection1, connection2 };
            person2.Connections = new[] { connection2, connection1 };

            var post = new Post
            {
                Title = "Hello world",
                Body = "Not much to say here.",
                Created = new DateTime(2008, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc),
                Creator = person1,
                Tags = new[] { "Monsters", "Blue" }
            };

            var doc = new JsonSerializer().Serialize(post);

            Assert.Equal(@"{
  ""Title"": ""Hello world"",
  ""Body"": ""Not much to say here."",
  ""Creator"": {
    ""Name"": ""Jason"",
    ""SSN"": null,
    ""Birth"": null,
    ""Email"": null,
    ""Gender"": ""Male"",
    ""Connections"": [
      {
        ""Source"": {
          ""Name"": ""Jason"",
          ""SSN"": null,
          ""Birth"": null,
          ""Email"": null,
          ""Gender"": ""Male"",
          ""Connections"": [
            {
              ""Source"": {
              },
              ""Target"": {
              }
            },
            {
              ""Source"": {
              },
              ""Target"": {
              }
            }
          ],
          ""Url"": null
        },
        ""Target"": {
          ""Name"": ""Joe"",
          ""SSN"": null,
          ""Birth"": null,
          ""Email"": null,
          ""Gender"": ""Male"",
          ""Connections"": [
            {
              ""Source"": {
              },
              ""Target"": {
              }
            },
            {
              ""Source"": {
              },
              ""Target"": {
              }
            }
          ],
          ""Url"": null
        }
      },
      {
        ""Source"": {
          ""Name"": ""Joe"",
          ""SSN"": null,
          ""Birth"": null,
          ""Email"": null,
          ""Gender"": ""Male"",
          ""Connections"": [
            {
              ""Source"": {
              },
              ""Target"": {
              }
            },
            {
              ""Source"": {
              },
              ""Target"": {
              }
            }
          ],
          ""Url"": null
        },
        ""Target"": {
          ""Name"": ""Jason"",
          ""SSN"": null,
          ""Birth"": null,
          ""Email"": null,
          ""Gender"": ""Male"",
          ""Connections"": [
            {
              ""Source"": {
              },
              ""Target"": {
              }
            },
            {
              ""Source"": {
              },
              ""Target"": {
              }
            }
          ],
          ""Url"": null
        }
      }
    ],
    ""Url"": null
  },
  ""Media"": [ ],
  ""Tags"": [ ""Monsters"", ""Blue"" ],
  ""Issued"": null,
  ""Created"": ""2008-01-01Z""
}", doc.ToString());

            //Console.WriteLine(doc.ToString());
        }