Ejemplo n.º 1
0
        /// <summary>
        /// Just fetch Json Post as per supplied criteria
        /// </summary>
        /// <param name="param"></param>
        private void GetSelectedPost(object param)
        {
            if (_service != null && _selectedJsonPlaceHolder != null)
            {
                JsonPostDetailModel jsonPlaceHolderModel = _service.GetPost(_selectedJsonPlaceHolder.Id);

                if (jsonPlaceHolderModel != null)
                {
                    //Just populate fetched object after service call
                    SelectedJsonPlaceHolder = ModelToViewObjectConverter.Convert(jsonPlaceHolderModel);
                    NotifyPropertyChanged("CopyModeEnabled");
                    RenderPostContentFormat();
                }
            }
        }
Ejemplo n.º 2
0
        public void PostContentProperty_Test()
        {
            if (_jsonPostsViewModel != null)
            {
                JsonPostDetailModel jsonPostDetailModel = ViewObjectToModelConverter.Convert(_jsonPostsViewModel.JsonPosts.FirstOrDefault());

                //During initial loading _jsonPostsViewModel.PostContent was set, verify its validity
                Assert.AreEqual(_jsonPostsViewModel.PostContent, jsonPostDetailModel.ToString());

                //Intentionally set the value
                _jsonPostsViewModel.PostContent = string.Empty;
                _jsonPostsViewModel.PostContent = jsonPostDetailModel.ToString();

                Assert.AreEqual(_jsonPostsViewModel.PostContent, jsonPostDetailModel.ToString());
            }
        }
        /// <summary>
        /// Fetches the relavant JSON Post from http://jsonplaceholder.typicode.com/posts/ and deserializes it to a Model object
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonPostDetailModel GetPost(int id)
        {
            string         url     = Constants.POSTSURL + "/" + id.ToString();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            JsonPostDetailModel jsonPlaceHolderModel = null;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string result = reader.ReadToEnd();
                    //This deserializes the data from http://jsonplaceholder.typicode.com/posts/1 to JsonPlaceHolderData
                    JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
                    jsonPlaceHolderModel = new JsonPostDetailModel();
                    jsonPlaceHolderModel = jsonSerializer.Deserialize <JsonPostDetailModel>(result);
                }
            }

            return(jsonPlaceHolderModel);
        }
        /// <summary>
        /// Method that parses the Model object into plain texe, Json, Html
        /// </summary>
        /// <param name="model"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public string ParseModelData(JsonPostDetailModel model, CopyMode mode)
        {
            string data = string.Empty;

            switch (mode)
            {
            case CopyMode.JSON:
                var jsSerializer = new JavaScriptSerializer();
                data = jsSerializer.Serialize(model);
                break;

            case CopyMode.HTML:
                var htmlSerializer = new XmlSerializer(typeof(JsonPostDetailModel));
                using (var srtWriter = new StringWriter())
                {
                    using (var writer = XmlWriter.Create(srtWriter))
                    {
                        htmlSerializer.Serialize(writer, model);
                        data = srtWriter.ToString();

                        //Just Convert the object to Html string representation
                        XmlDocument document = new XmlDocument();
                        document.LoadXml(data);
                        XmlNode node = document.SelectSingleNode(Constants.JSONPOSTDETAILMODEL);

                        if (node != null)
                        {
                            data = node.InnerXml.ToString();
                        }
                    }
                }
                break;

            default:
                return(model.ToString());
            }

            return(data);
        }
 /// <summary>
 /// Returns JsonPostViewObject from a JsonPostDetailModel object
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 private static JsonPostViewObject GetJsonPostViewObject(JsonPostDetailModel model)
 {
     return(new JsonPostViewObject {
         Id = model.id, UserId = model.userId, Title = model.title, Body = model.body
     });
 }
 /// <summary>
 /// Create a ViewObject used for rendering from the model object
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public static JsonPostViewObject Convert(JsonPostDetailModel model)
 {
     return(GetJsonPostViewObject(model));
 }