Quantcast
Channel: DevNet Questions
Viewing all articles
Browse latest Browse all 8901

Kentico: Deserialize object (TreeNode) from Json

$
0
0
My question is pretty simple. I'm working on an API and I need to update an entity. For this purpose, I need to convert the JObject coming from body to a TreeNode (News) object of the Kentico Model. Reading the Kentico documentation there is an object called JSonSerializer which seems to be right one:Kentico 9 JsonSerializerMy API looks like this:[HttpPut] [Route("")] public IHttpActionResult UpdateById([FromBody]JObject updated) { CMS.DataCom.JsonSerializer serializer = new CMS.DataCom.JsonSerializer(); var result = serializer.Unserialize@CMS.DocumentEngine.Types.News@(updated.ToString()); DocumentHelper.UpdateDocument(result); return Ok("record updated."); } My "updated" object is having the correct JSON. However, when I try to Unserialize I get the following error: An exception of type 'System.Runtime.Serialization.InvalidDataContractException' occurred in System.Runtime.Serialization.dll but was not handled in user code Additional information: Type 'CMS.DocumentEngine.Types.News' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.My autogenerated CMS.DocumentEngine.Types.News class is defined this way://-------------------------------------------------------------------------------------------------- // @auto-generated@ // // This code was generated by code generator tool. // // To customize the code use your own partial class. For more info about how to use and customize // the generated code see the documentation at http://docs.kentico.com. // // @/auto-generated@ //-------------------------------------------------------------------------------------------------- using System; using System.Collections.Generic; using CMS; using CMS.Helpers; using CMS.DataEngine; using CMS.DocumentEngine.Types; using CMS.DocumentEngine; using System.Runtime.Serialization; [assembly: RegisterDocumentType(News.CLASS_NAME, typeof(News))] namespace CMS.DocumentEngine.Types { /// @summary@ /// Represents a content item of type News. /// @/summary@ public partial class News : TreeNode { #region "Constants and variables" /// @summary@ /// The name of the data class. /// @/summary@ public const string CLASS_NAME = "AonAffinity.News"; /// @summary@ /// The instance of the class that provides extended API for working with News fields. /// @/summary@ private readonly NewsFields mFields; #endregion #region "Properties" /// @summary@ /// /// @/summary@ [DatabaseIDField] public int NewsID { get { return ValidationHelper.GetInteger(GetValue("NewsID"), 0); } set { SetValue("NewsID", value); } } /// @summary@ /// Hero Image (1650x660). /// @/summary@ [DatabaseField] public string HeroImage { get { return ValidationHelper.GetString(GetValue("HeroImage"), ""); } set { SetValue("HeroImage", value); } } /// @summary@ /// Heading Text. /// @/summary@ [DatabaseField] public string HeadingText { get { return ValidationHelper.GetString(GetValue("HeadingText"), ""); } set { SetValue("HeadingText", value); } } /// @summary@ /// Main Content. /// @/summary@ [DatabaseField] public string MainContent { get { return ValidationHelper.GetString(GetValue("MainContent"), ""); } set { SetValue("MainContent", value); } } /// @summary@ /// News Title. /// @/summary@ [DatabaseField] public string NewsTitle { get { return ValidationHelper.GetString(GetValue("NewsTitle"), ""); } set { SetValue("NewsTitle", value); } } /// @summary@ /// Release Date. /// @/summary@ [DatabaseField] public DateTime NewsReleaseDate { get { return ValidationHelper.GetDateTime(GetValue("NewsReleaseDate"), DateTimeHelper.ZERO_TIME); } set { SetValue("NewsReleaseDate", value); } } /// @summary@ /// News Summary. /// @/summary@ [DatabaseField] public string NewsSummary { get { return ValidationHelper.GetString(GetValue("NewsSummary"), ""); } set { SetValue("NewsSummary", value); } } /// @summary@ /// News Text. /// @/summary@ [DatabaseField] public string NewsText { get { return ValidationHelper.GetString(GetValue("NewsText"), ""); } set { SetValue("NewsText", value); } } /// @summary@ /// Teaser Image (370x230). /// @/summary@ [DatabaseField] public string NewsTeaser { get { return ValidationHelper.GetString(GetValue("NewsTeaser"), ""); } set { SetValue("NewsTeaser", value); } } /// @summary@ /// This text will show up on the search results page. It has a max length of . /// @/summary@ [DatabaseField] public string SearchDescriptionText { get { return ValidationHelper.GetString(GetValue("SearchDescriptionText"), ""); } set { SetValue("SearchDescriptionText", value); } } /// @summary@ /// Page Container Css Class Override. /// @/summary@ [DatabaseField] public string PageContainerCssClassOverride { get { return ValidationHelper.GetString(GetValue("PageContainerCssClassOverride"), ""); } set { SetValue("PageContainerCssClassOverride", value); } } /// @summary@ /// Gets an object that provides extended API for working with News fields. /// @/summary@ public NewsFields Fields { get { return mFields; } } /// @summary@ /// Provides extended API for working with News fields. /// @/summary@ public partial class NewsFields { /// @summary@ /// The content item of type News that is a target of the extended API. /// @/summary@ private readonly News mInstance; /// @summary@ /// Initializes a new instance of the @see cref="NewsFields" /@ class with the specified content item of type News. /// @/summary@ /// @param name="instance"@The content item of type News that is a target of the extended API.@/param@ public NewsFields(News instance) { mInstance = instance; } /// @summary@ /// /// @/summary@ public int ID { get { return mInstance.NewsID; } set { mInstance.NewsID = value; } } /// @summary@ /// Hero Image (1650x660). /// @/summary@ public string HeroImage { get { return mInstance.HeroImage; } set { mInstance.HeroImage = value; } } /// @summary@ /// Heading Text. /// @/summary@ public string HeadingText { get { return mInstance.HeadingText; } set { mInstance.HeadingText = value; } } /// @summary@ /// Main Content. /// @/summary@ public string MainContent { get { return mInstance.MainContent; } set { mInstance.MainContent = value; } } /// @summary@ /// News Title. /// @/summary@ public string Title { get { return mInstance.NewsTitle; } set { mInstance.NewsTitle = value; } } /// @summary@ /// Release Date. /// @/summary@ public DateTime ReleaseDate { get { return mInstance.NewsReleaseDate; } set { mInstance.NewsReleaseDate = value; } } /// @summary@ /// News Summary. /// @/summary@ public string Summary { get { return mInstance.NewsSummary; } set { mInstance.NewsSummary = value; } } /// @summary@ /// News Text. /// @/summary@ public string Text { get { return mInstance.NewsText; } set { mInstance.NewsText = value; } } /// @summary@ /// Teaser Image (370x230). /// @/summary@ public string Teaser { get { return mInstance.NewsTeaser; } set { mInstance.NewsTeaser = value; } } /// @summary@ /// This text will show up on the search results page. It has a max length of . /// @/summary@ public string SearchDescriptionText { get { return mInstance.SearchDescriptionText; } set { mInstance.SearchDescriptionText = value; } } /// @summary@ /// Page Container Css Class Override. /// @/summary@ public string PageContainerCssClassOverride { get { return mInstance.PageContainerCssClassOverride; } set { mInstance.PageContainerCssClassOverride = value; } } } #endregion #region "Constructors" /// @summary@ /// Initializes a new instance of the @see cref="News" /@ class. /// @/summary@ public News() : base(CLASS_NAME) { mFields = new NewsFields(this); } #endregion } } It's also important to mention that I already tried to deserialize by using System.Web.Script.Serialization.JavaScriptSerializer and Newtonsoft.Json.Linq.JToken.ToObject, and also by putting directly the class on the parameter of the API, but didn't work either.Any thoughts?

Viewing all articles
Browse latest Browse all 8901

Trending Articles