private static WPPostPageModel MapWPPost(dynamic jsonObj) { //object declaration var wpPostCollectionModel = new WPPostPageModel(); var wpPostCollectionModelList = new List <WPPostPageModel>(); try { //for a collection POST content request, but only returns a single post wpPostCollectionModelList = JsonConvert.DeserializeObject(jsonObj.ToString(), typeof(List <WPPostPageModel>)); if (wpPostCollectionModelList != null && wpPostCollectionModelList.Any()) { wpPostCollectionModel = wpPostCollectionModelList.First(); } } catch (Exception ex) { //for a single POST content request wpPostCollectionModel = JsonConvert.DeserializeObject(jsonObj.ToString(), typeof(WPPostPageModel)); } //ensuring that app dependent values are set in the page model before returning if (wpPostCollectionModelList != null && wpPostCollectionModelList.Any()) { wpPostCollectionModelList.ForEach(EnsurePostPageValues); } else { EnsurePostPageValues(wpPostCollectionModel); } return(wpPostCollectionModel); }
/// <summary> /// Method is used to ensure that newly declared Post/Page objects have necessary property values when returned /// </summary> /// <param name="page"></param> private static void EnsurePostPageValues(WPPostPageModel page) { page.slug = page.slug ?? page.id.ToString(); page.type = page.type ?? "post"; //Place default values into the Post/Page Model to ensure a successful delivery if (page.title == null) { page.title = new WPPostPageModel.Title(); page.title.rendered = @"404 - Not Found"; } if (page.content == null) { page.content = new WPPostPageModel.Content(); page.content.rendered = @"<h2>404 - Not Found</h2>"; } }