Beispiel #1
0
        public void CanSerializeAnonymousObjectsComingFromInterface()
        {
            var factory     = new AnonymousModelFactory();
            var transformer = factory.GetTransformer <Article>();

            var articles = new[]
            {
                new Article
                {
                    Id    = "1",
                    Type  = "articles",
                    Title = "Wifi"
                },
                new Article
                {
                    Id    = "2",
                    Type  = "articles",
                    Title = "Home Theater"
                }
            };

            var models = articles.Select(x => transformer.GetModel(x));

            var json = JsonApiDocument.Create(models).Serialize();

            Assert.Equal(@"
                {
                  'data': [
                    {
                      'id': '1',
                      'type': 'articles',
                      'attributes': {
                        'title': 'Wifi'
                      }
                    },
                    {
                      'id': '2',
                      'type': 'articles',
                      'attributes': {
                        'title': 'Home Theater'
                      }
                    }
                  ]
                }".Format(), json, JsonStringEqualityComparer.Default);
        }
Beispiel #2
0
        public void CanSerializeAnonymousObjectWithRelationshipFromInterface()
        {
            var factory     = new AnonymousModelFactory();
            var transformer = factory.GetTransformer <ArticleWithAuthor>();

            var author = new Author
            {
                Id   = "4",
                Type = "authors",
                Name = "Bob"
            };

            var articles = new[]
            {
                new ArticleWithAuthor
                {
                    Id     = "1",
                    Type   = "articles",
                    Title  = "Wifi",
                    Author = author
                },
                new ArticleWithAuthor
                {
                    Id     = "2",
                    Type   = "articles",
                    Title  = "Home Theater",
                    Author = author
                }
            };

            var models = articles.Select(x => transformer.GetModel(x));

            var json = JsonApiDocument.Create(models).Serialize();

            Assert.Equal(@"
                {
                  'data': [
                    {
                      'id': '1',
                      'type': 'articles',
                      'attributes': {
                        'title': 'Wifi'
                      },
                      'relationships': {
                        'author': {
                          'data': {
                            'id': '4',
                            'type': 'authors'
                          }
                        }
                      }
                    },
                    {
                      'id': '2',
                      'type': 'articles',
                      'attributes': {
                        'title': 'Home Theater'
                      },
                      'relationships': {
                        'author': {
                          'data': {
                            'id': '4',
                            'type': 'authors'
                          }
                        }
                      }
                    }
                  ],
                  'included': [
                    {
                      'id': '4',
                      'type': 'authors',
                      'attributes': {
                        'name': 'Bob'
                      }
                    }
                  ]
                }".Format(), json, JsonStringEqualityComparer.Default);
        }