コード例 #1
0
        public void CanSerializeIncludedWithTypedDocument()
        {
            var article = new ArticleWithAuthor
            {
                Id     = "1",
                Type   = "article",
                Title  = "Cereal-eyes",
                Author = new Author
                {
                    Id      = "9",
                    Type    = "people",
                    Name    = "Joe",
                    Twitter = "joe"
                }
            };

            var document = JsonApiDocument.Create(new[] { article });

            var json = document.Serialize();

            Assert.Equal(@"
                {
                  'data': [
                    {
                      'id': '1',
                      'type': 'article',
                      'attributes': {
                        'title': 'Cereal-eyes'
                      },
                      'relationships': {
                        'author': {
                          'data': {
                            'id': '9',
                            'type': 'people'
                          }
                        }
                      }
                    }
                  ],
                  'included': [
                    {
                      'id': '9',
                      'type': 'people',
                      'attributes': {
                        'name': 'Joe',
                        'twitter': 'joe'
                      }
                    }
                  ]
                }".Format(), json, JsonStringEqualityComparer.Default);
        }
コード例 #2
0
        public void CanSerializeSimpleCompoundDocument()
        {
            var model = new ArticleWithAuthor
            {
                Id     = "1",
                Type   = "articles",
                Title  = "Jsonapi",
                Author = new Author
                {
                    Id      = "9",
                    Type    = "people",
                    Name    = "Dan Gebhardt",
                    Twitter = "dgeb"
                }
            };

            var json = model.Serialize();

            Assert.Equal(@"
                {
                  'data': {
                    'type': 'articles',
                    'id': '1',
                    'attributes': {
                      'title': 'Jsonapi'
                    },
                    'relationships': {
                      'author': {
                        'data': {
                          'type': 'people',
                          'id': '9'
                        }
                      }
                    }
                  },
                  'included': [
                    {
                      'type': 'people',
                      'id': '9',
                      'attributes': {
                        'name': 'Dan Gebhardt',
                        'twitter': 'dgeb'
                      }
                    }
                  ]
                }".Format(), json, JsonStringEqualityComparer.Default);
        }
コード例 #3
0
        public void CanSerializeIncludedAndRelationshipsWithTypedDocument()
        {
            var author = new Author
            {
                Id      = "9",
                Type    = "people",
                Name    = "Joe",
                Twitter = "joe"
            };

            var article = new ArticleWithAuthor
            {
                Id       = "1",
                Type     = "articles",
                Title    = "Highway or My Way",
                Author   = author,
                Comments = new[]
                {
                    new Comment
                    {
                        Id     = "5",
                        Type   = "comments",
                        Body   = "Hi!",
                        Author = author
                    }
                }
            };

            var document = new JsonApiDocument <ArticleWithAuthor[]>
            {
                Data = new[] { article }
            };

            var json = document.Serialize();

            Assert.Equal(@"
                {
                  'data': [
                    {
                      'id': '1',
                      'type': 'articles',
                      'attributes': {
                        'title': 'Highway or My Way'
                      },
                      'relationships': {
                        'author': {
                          'data': {
                            'id': '9',
                            'type': 'people'
                          }
                        },
                        'comments': {
                          'data': [
                            {
                              'id': '5',
                              'type': 'comments'
                            }
                          ]
                        }
                      }
                    }
                  ],
                  'included': [
                    {
                      'id': '9',
                      'type': 'people',
                      'attributes': {
                        'name': 'Joe',
                        'twitter': 'joe'
                      }
                    },
                    {
                      'id': '5',
                      'type': 'comments',
                      'attributes': {
                        'body': 'Hi!'
                      },
                      'relationships': {
                        'author': {
                          'data': {
                            'id': '9',
                            'type': 'people'
                          }
                        }
                      }
                    }
                  ]
                }".Format(), json, JsonStringEqualityComparer.Default);
        }
コード例 #4
0
        public void CanSerializeRelationshipCollection()
        {
            var model = new ArticleWithAuthor
            {
                Id       = "1",
                Type     = "articles",
                Title    = "Jsonapi",
                Comments = new []
                {
                    new Comment
                    {
                        Id   = "5",
                        Type = "comments",
                        Body = "first"
                    },
                    new Comment
                    {
                        Id   = "12",
                        Type = "comments",
                        Body = "second"
                    }
                }
            };

            var json = model.Serialize();

            Assert.Equal(@"
                {
                  'data': {
                    'type': 'articles',
                    'id': '1',
                    'attributes': {
                      'title': 'Jsonapi'
                    },
                    'relationships': {
                      'comments': {
                        'data': [
                          {
                            'type': 'comments',
                            'id': '5'
                          },
                          {
                            'type': 'comments',
                            'id': '12'
                          }
                        ]
                      }
                    }
                  },
                  'included': [
                    {
                      'type': 'comments',
                      'id': '5',
                      'attributes': {
                        'body': 'first'
                      }
                    },
                    {
                      'type': 'comments',
                      'id': '12',
                      'attributes': {
                        'body': 'second'
                      }
                    }
                  ]
                }".Format(), json, JsonStringEqualityComparer.Default);
        }