{"id":741,"date":"2019-09-10T13:35:25","date_gmt":"2019-09-10T16:35:25","guid":{"rendered":"https:\/\/benjaminray.com\/codebase\/?p=741"},"modified":"2019-09-10T14:25:17","modified_gmt":"2019-09-10T17:25:17","slug":"serializing-objects-in-asp-net-webforms","status":"publish","type":"post","link":"https:\/\/benjaminray.com\/codebase\/serializing-objects-in-asp-net-webforms\/","title":{"rendered":"Serializing Objects in ASP.Net WebForms"},"content":{"rendered":"<p>This is a very incomplete post with some quick &amp; dirty notes about serializing objects in ASP.Net WebForms. All included code is VB.Net.<\/p>\n<h2>Default ASP.Net serialization<\/h2>\n<ul>\n<li>Classes must have annotation <code>&lt;Serializable&gt;<\/code> to be serialized by the default serializer<\/li>\n<li>Use annotation <code>&lt;NonSerialized&gt;<\/code> on properties to be left out of serialization<\/li>\n<li>Navigation Properties (Entity Framework) can't be marked as <code>&lt;NonSerialized&gt;<\/code>, so you may end up with circular references when the serialization attempts to follow all relationships (e.g. child has reference to a parent, and parent has reference to all children)<\/li>\n<li>Any private variable\/property will by default be left out of the serialization<\/li>\n<\/ul>\n<h2>Newtonsoft Json.Net Serialization<\/h2>\n<ul>\n<li>No need for <code>&lt;Serializable&gt;<\/code> annotation; any object\/class can be serialized<\/li>\n<li>Use annotation <code>&lt;JsonIgnore&gt;<\/code> on any properties to be left out of the serialization<\/li>\n<li>Navigation Properties (Entity Framework) can have annotation <code>&lt;JsonIgnore&gt;<\/code> to be left out of the serialization, preventing circular references and unnecessary data from being included<\/li>\n<li>I haven't tested if private variables are included in the serialization, but it seems likely<\/li>\n<\/ul>\n<h2>Serialization and ViewState<\/h2>\n<p>Anything stored in ViewState is serialized. When you reference <code>ViewState(\"SomeKey\")<\/code> directly (or through a property), any objects not already serialized will be automatically serialized using built-in serialization. Also, when you access <code>ViewState(\"SomeKey\")<\/code>, you are getting a reference to the value in the ViewState, and any changes made will affect the actual values behind the reference. For example:<\/p>\n<pre><code>CType(ViewState(\"SomeKey\"), List(Of String)).Add(\"test\")\n<\/code><\/pre>\n<p>After running this code, <code>ViewState(\"SomeKey\")<\/code> will contain the new entry. This is because ViewState(\"SomeKey\") is giving you the reference to the actual value, not a copy of the value.<\/p>\n<p>On the other hand, if you were to use Json.Net serializer to serialize an object before adding it to ViewState, when you deserialize it you will be getting a copy of the value (not a reference to it), and direct updates won't affect the value in ViewState. For example:<\/p>\n<pre><code>JsonConvert.DeserializeObject(ViewState(\"SomeKey\"), GetType(List(Of String))).Add(\"New Entry\")\n<\/code><\/pre>\n<p>After running this code, <code>ViewState(\"SomeKey\")<\/code> will not contain the new entry, as the function DeserializeObject returns a deserialized copy of the value in ViewState, not a reference to the value in ViewState.<\/p>\n<p>To use Json.Net and ViewState, you will have to retrieve the value from ViewState, deserialize it to a local variable, make your changes, reserialize it, then save it back to ViewSate.<\/p>\n<pre><code>Dim TestList as List(Of String)\nTestList = JsonConvert.DeserializeObject(ViewState(\"SomeKey\"), GetType(List(Of String)))\nlstTest.Add(\"New Entry\")\nViewState(\"SomeKey\") = JsonConvert.SerializeObject(TestList)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a very incomplete post with some quick &amp; dirty notes about serializing objects in ASP.Net WebForms. All included code is VB.Net. Default ASP.Net serialization Classes must have annotation  [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[19,24],"tags":[],"class_list":["post-741","post","type-post","status-publish","format-standard","hentry","category-entity-framework","category-webforms"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9GNjN-bX","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/comments?post=741"}],"version-history":[{"count":7,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/741\/revisions"}],"predecessor-version":[{"id":748,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/741\/revisions\/748"}],"wp:attachment":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/media?parent=741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/categories?post=741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/tags?post=741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}