コード例 #1
0
        public static DocumentRoot <List <Article> > GenerateDocumentRoot(int listSize)
        {
            var fixture = new Fixture();

            fixture.Customize <Article>(composer => composer.Without(x => x.Type).Without(x => x.Links));
            fixture.Customize <Comment>(composer => composer.Without(x => x.Type).Without(x => x.Links));
            fixture.Customize <Person>(composer => composer.Without(x => x.Type).Without(x => x.Links));
            fixture.Customize <Person>(composer => new ElementsBuilder <Person>(fixture.CreateMany <Person>(Math.Max(1, listSize / 10)).ToList()));


            return(DocumentRoot.Create(fixture.CreateMany <Article>(listSize).ToList()));
        }
コード例 #2
0
        public void When_id_null_should_serialize_as_string()
        {
            var root = DocumentRoot.Create(new
            {
                Id     = (int?)null,
                Type   = "articles",
                Title  = "My title",
                Author = ResourceIdentifier.Create(new
                {
                    Id   = 7357, //int relationship
                    Type = "person",
                    Name = "Scribble McPen"
                })
            });

            var json = JsonConvert.SerializeObject(root, new JsonApiSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Include,
                Formatting        = Formatting.Indented
            });
            var expectedjson = @"{
              ""data"": {
                ""id"": null,
                ""type"": ""articles"",
                ""attributes"": {
                  ""title"": ""My title""
                },
                ""relationships"": {
                  ""author"": {
                    ""data"": {
                      ""id"": ""7357"",
                      ""type"": ""person""
                    }
                  }
                }
              },
              ""included"": [
                {
                  ""id"": ""7357"",
                  ""type"": ""person"",
                  ""attributes"": {
                    ""name"": ""Scribble McPen""
                  }
                }
              ]
            }";

            Assert.Equal(expectedjson, json, JsonStringEqualityComparer.Instance);
        }
コード例 #3
0
        public void When_id_int_in_explicit_identifier_should_serialize_as_string()
        {
            var root = DocumentRoot.Create(new
            {
                Id     = 7357, //int id
                Type   = "articles",
                Title  = "My title",
                Author = ResourceIdentifier.Create(new
                {
                    Id   = 7357, //int relationship
                    Type = "person",
                    Name = "Scribble McPen"
                })
            });

            var json         = JsonConvert.SerializeObject(root, settings);
            var expectedjson = @"{
              ""data"": {
                ""id"": ""7357"",
                ""type"": ""articles"",
                ""attributes"": {
                  ""title"": ""My title""
                },
                ""relationships"": {
                  ""author"": {
                    ""data"": {
                      ""id"": ""7357"",
                      ""type"": ""person""
                    }
                  }
                }
              },
              ""included"": [
                {
                  ""id"": ""7357"",
                  ""type"": ""person"",
                  ""attributes"": {
                    ""name"": ""Scribble McPen""
                  }
                }
              ]
            }";

            Assert.Equal(expectedjson, json, JsonStringEqualityComparer.Instance);
        }
コード例 #4
0
        public void When_relationshp_with_resource_identifier_should_serialize()
        {
            var root = DocumentRoot.Create(new
            {
                Id     = "1234",
                Type   = "articles",
                Title  = "My Article",
                Author = new Relationship <ResourceIdentifier <Person> >()
                {
                    Data = new ResourceIdentifier <Person>()
                    {
                        Meta = new Meta {
                            { "primary-author", true }
                        },
                        Value = new Person
                        {
                            Id        = "333",
                            Type      = "people",
                            FirstName = "John",
                            LastName  = "Smith",
                            Twitter   = "jsmi",
                            Meta      = new Meta()
                            {
                                { "best-sellers", 4 }
                            }
                        }
                    },
                    Meta = new Meta {
                        { "self-published", true }
                    }
                },
                Comments = Relationship.Create(new List <ResourceIdentifier <Comment> >
                {
                    ResourceIdentifier.Create(new Comment()
                    {
                        Id   = "57",
                        Type = "comments",
                        Body = "I give it a 5 out of 7"
                    })
                })
            });

            var json         = JsonConvert.SerializeObject(root, settings);
            var expectedjson = @"{
                ""data"": {
                    ""id"": ""1234"",
                    ""type"": ""articles"",
                    ""attributes"": {
                        ""title"": ""My Article""
                    },
                    ""relationships"": {
                        ""author"": {
                            ""data"": { 
                                ""id"":""333"", 
                                ""type"":""people"",
                                ""meta"":{ ""primary-author"": true}
                            },
                            ""meta"": {""self-published"": true},
                        },
                        ""comments"": {
                            ""data"": [{ 
                                ""id"":""57"", 
                                ""type"":""comments"",
                            }]
                        }
                    }
                },
                ""included"" : [
                    {
                        ""id"": ""333"",
                        ""type"": ""people"",
                        ""attributes"":{
                            ""first-name"": ""John"",
                            ""last-name"": ""Smith"",
                            ""twitter"": ""jsmi""
                        },
                        ""meta"":{ ""best-sellers"": 4}

                    },
                    {
                        ""id"": ""57"",
                        ""type"": ""comments"",
                        ""attributes"":{
                            ""body"": ""I give it a 5 out of 7""
                        }
                    }
                ]
            }";

            Assert.Equal(expectedjson, json, JsonStringEqualityComparer.Instance);
        }
コード例 #5
0
        public void When_relationship_not_identifable_from_property_type_should_serialize_as_relationship()
        {
            var article = new
            {
                id     = "1234",
                Title  = "My Article",
                type   = "articles",
                author = (object)new //declared object but actaul type is relationship
                {
                    Id        = "333",
                    type      = "people",
                    FirstName = "John",
                    LastName  = "Smith",
                    Twitter   = "jsmi"
                },
                timestamps = (object)new //declared object but actaul type is attribute
                {
                    published = new DateTime(2018, 10, 28),
                    edited    = new DateTime(2018, 10, 31),
                }
            };
            var root = DocumentRoot.Create((object)article);

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

            var expectedjson = @"{
                ""data"": {
                    ""id"": ""1234"",
                    ""type"": ""articles"",
                    ""attributes"": {
                        ""title"": ""My Article"",
                        ""timestamps"": {
                            ""published"": ""2018-10-28T00:00:00"",
                            ""edited"": ""2018-10-31T00:00:00""
                        }
                    },
                    ""relationships"": {
                        ""author"": {
                            ""data"": { 
                                ""id"":""333"", 
                                ""type"":""people""
                            }
                        }
                    }
                },
                ""included"" : [
                    {
                        ""id"": ""333"",
                        ""type"": ""people"",
                        ""attributes"":{
                            ""firstName"": ""John"",
                            ""lastName"": ""Smith"",
                            ""twitter"": ""jsmi""
                        }

                    }
                ]
            }";

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