コード例 #1
0
        public void When_data_list_explicit_null_should_serialize_empty_list()
        {
            var root = new ArticleWithRelationship
            {
                Id       = "1234",
                Title    = "My Article",
                Comments = new Relationship <List <Comment> >
                {
                    Data  = null,
                    Links = new Links
                    {
                        { "related", new Link {
                              Href = "http://example.com/articles/1/comments"
                          } }
                    }
                }
            };

            var json         = JsonConvert.SerializeObject(root, settings);
            var expectedjson = @"{
                ""data"": {
                    ""id"": ""1234"",
                    ""type"": ""articles"",
                    ""attributes"": {
                        ""title"": ""My Article""
                    },
                    ""relationships"": {
                        ""comments"": {
                            ""data"" : [],
                            ""links"": {
                                ""related"": ""http://example.com/articles/1/comments""
                            }
                        }
                    }
                }
            }";

            Assert.Equal(expectedjson, json, JsonStringEqualityComparer.Instance);
        }
コード例 #2
0
        public void When_relationship_include_null_should_serialize_relationships()
        {
            var root = new ArticleWithRelationship
            {
                Id       = "1234",
                Title    = "My Article",
                Comments = null,
                Author   = null
            };
            var includeNull = new JsonApiSerializerSettings()
            {
                Formatting        = Formatting.Indented,
                NullValueHandling = NullValueHandling.Include
            };
            var json         = JsonConvert.SerializeObject(root, includeNull);
            var expectedjson = @"{
                ""data"": {
                    ""id"": ""1234"",
                    ""type"": ""articles"",
                    ""links"": null,
                    ""attributes"": {
                        ""title"": ""My Article""
                    },
                    ""relationships"": {
                        ""comments"": {
                            ""data"" : []
                        },
                        ""author"": {
                            ""data"" : null
                        }
                    }
                }
            }";

            Assert.Equal(expectedjson, json, JsonStringEqualityComparer.Instance);
        }
コード例 #3
0
        public void When_explicit_relationship_should_output_metaData()
        {
            var root = new ArticleWithRelationship
            {
                Id     = "1234",
                Title  = "My Article",
                Author = new Relationship <Person>
                {
                    Data = new Person
                    {
                        Id        = "333",
                        FirstName = "John",
                        LastName  = "Smith",
                        Twitter   = "jsmi"
                    },
                    Links = new Links
                    {
                        { "self", new Link {
                              Href = "http://example.com/articles/1/relationships/author"
                          } },
                        { "related", new Link {
                              Href = "http://example.com/articles/1/author"
                          } }
                    }
                }
            };

            var json         = JsonConvert.SerializeObject(root, settings);
            var expectedjson = @"{
                ""data"": {
                    ""id"": ""1234"",
                    ""type"": ""articles"",
                    ""attributes"": {
                        ""title"": ""My Article""
                    },
                    ""relationships"": {
                        ""author"": {
                            ""data"": { 
                                ""id"":""333"", 
                                ""type"":""people""
                            },
                            ""links"": {
                                ""self"": ""http://example.com/articles/1/relationships/author"",
                                ""related"": ""http://example.com/articles/1/author""
                            }
                        }
                    }
                },
                ""included"" : [
                    {
                        ""id"": ""333"",
                        ""type"": ""people"",
                        ""attributes"":{
                            ""first-name"": ""John"",
                            ""last-name"": ""Smith"",
                            ""twitter"": ""jsmi""
                        }

                    }
                ]
            }";

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