public override void WriteToStream(Type type, object value, System.IO.Stream writeStream, System.Net.Http.HttpContent content) { using (var writer = new StreamWriter(writeStream)) { var underlyingType = GetUnderlyingType(type); var collectionJson = new CollectionJsonDocument(underlyingType, HttpContext.Current.Request.Path); collectionJson.Initialize(); if (value is CollectionJsonDocument) { collectionJson = (CollectionJsonDocument)value; } else if (value is IEnumerable) { foreach (var entity in (IEnumerable)value) { collectionJson.AddItem(entity); } } else { collectionJson.AddItem(value); } var json = JsonConvert.SerializeObject( value: collectionJson, formatting: Formatting.Indented, settings: Configuration.SerializerSettings ); writer.WriteLine(json); } writeStream.Close(); }
public CollectionJsonDocument Get() { var collectionJsonDocument = new CollectionJsonDocument(typeof(FluidFriend), "/friends"); collectionJsonDocument.Initialize(); collectionJsonDocument.Collection.Href = "/manualfriends"; collectionJsonDocument.Collection.Links = new List<LinkProperty> { new LinkProperty { Href = "/friends/rss", Rel = "feed" } }; var items = new List<ItemProperty>(); foreach (var friend in SampleData.FluidFriends) { var data = new List<DataProperty>() { new DataProperty{Name = "email", Value = friend.Email, Prompt = "Email"}, new DataProperty{Name = "full-name", Value = friend.FullName, Prompt = "Full Name"}, new DataProperty{Name = "short-name", Value = friend.ShortName, Prompt = "Short Name"} }; var links = new List<LinkProperty> { new LinkProperty{Href = string.Format("/friends/blogs/{0}", friend.ShortName), Rel = "blog", Prompt = "Blog"}, new LinkProperty{Href = string.Format("/friends/images/{0}", friend.ShortName), Rel = "avatar", Prompt = "Avatar"} }; items.Add(new ItemProperty { Href = string.Format("/friends/{0}", friend.ShortName), Data = data, Links = links }); } collectionJsonDocument.Collection.Items = items; collectionJsonDocument.Collection.Queries = new List<QueryProperty> { new QueryProperty { Href = "/friends/search", Rel = "search" } }; collectionJsonDocument.Collection.Template = new TemplateProperty { Data = new List<TemplateDataProperty> { new TemplateDataProperty { Name = "email", Prompt = "Email", Value = string.Empty }, new TemplateDataProperty { Name = "full-name", Prompt = "Full Name", Value = string.Empty, Required = "true", Regexp = "^[a-zA-z]+$" } } }; return collectionJsonDocument; }
public void Initialize() { this.testModel = new TestMe(); this.collectionJson = new CollectionJsonDocument(testModel.GetType(), this.href); }