public void When_nested_structure_should_output_correclty()
        {
            var root = new LocationWithId()
            {
                Id      = "Willesdon Green",
                Parents = new ILocationWithId[]
                {
                    new LocationWithId()
                    {
                        Id      = "Brent",
                        Parents = new ILocationWithId[]
                        {
                            new LocationWithId()
                            {
                                Id = "London"
                            }
                        }
                    }
                }
            };


            var json = JsonConvert.SerializeObject(root, settings);

            Assert.Equal(@"{
            ""data"": {
                ""type"": ""locationwithid"",
                ""id"": ""Willesdon Green"",
                ""relationships"": {
                    ""parents"": {
                        ""data"": [
                        {
                            ""id"": ""Brent"",
                            ""type"": ""locationwithid""
                        }
                        ]
                    }
                }
            },
            ""included"": [
            {
                ""type"": ""locationwithid"",
                ""id"": ""Brent"",
                ""relationships"": {
                    ""parents"": {
                        ""data"": [
                        {
                            ""id"": ""London"",
                            ""type"": ""locationwithid""
                        }
                        ]
                    }
                }
            }
            ]
        }", json, JsonStringEqualityComparer.Instance);
        }
        public void When_linqed_list_should_serialize()
        {
            int selectCount = 0;
            var root        = new LocationWithId()
            {
                Id      = "Willesdon Green",
                Parents = Enumerable.Range(0, 3).Select(i => new LocationWithId()
                {
                    Id = $"London_{i}_{selectCount++}"
                })
            };

            var json = JsonConvert.SerializeObject(root, settings);

            Assert.Equal(3, selectCount); //should evaluate IEnumerable only once
            Assert.Equal(@"{
            ""data"": {
                ""type"": ""locationwithid"",
                ""id"": ""Willesdon Green"",
                ""relationships"": {
                    ""parents"": {
                        ""data"": [
                            {
                                ""id"": ""London_0_0"",
                                ""type"": ""locationwithid""
                            },
                            {
                                ""id"": ""London_1_1"",
                                ""type"": ""locationwithid""
                            },
                            {
                                ""id"": ""London_2_2"",
                                ""type"": ""locationwithid""
                            }
                        ]
                    }
                }
            }
        }", json, JsonStringEqualityComparer.Instance);
        }
Ejemplo n.º 3
0
        public void When_object_reference_relationships_are_in_data_should_not_be_in_includes()
        {
            var london = new LocationWithId {
                Id = "London", Description = "Capital"
            };
            var kingsCross = new LocationWithId {
                Id = "Kings-Cross", Parents = new[] { london }
            };
            var farringdon = new LocationWithId {
                Id = "Farringdon", Parents = new[] { london }
            };

            var root = new[]
            {
                london,
                kingsCross,
                farringdon
            };

            var json         = JsonConvert.SerializeObject(root, settings);
            var expectedjson = @"{
  ""data"": [
    {
      ""type"": ""locationwithid"",
      ""id"": ""London"",
      ""attributes"": {
        ""description"": ""Capital""
      }
    },
    {
      ""type"": ""locationwithid"",
      ""id"": ""Kings-Cross"",
      ""relationships"": {
        ""parents"": {
          ""data"": [
            {
              ""id"": ""London"",
              ""type"": ""locationwithid""
            }
          ]
        }
      }
    },
    {
      ""type"": ""locationwithid"",
      ""id"": ""Farringdon"",
      ""relationships"": {
        ""parents"": {
          ""data"": [
            {
              ""id"": ""London"",
              ""type"": ""locationwithid""
            }
          ]
        }
      }
    }
  ]
}";

            Assert.Equal(expectedjson, json, JsonStringEqualityComparer.Instance);
        }