{"id":2359,"date":"2014-10-08T19:11:01","date_gmt":"2014-10-08T15:11:01","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=2359"},"modified":"2014-10-08T19:34:30","modified_gmt":"2014-10-08T15:34:30","slug":"serializationmanager-java-serialization-using-generics","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=2359","title":{"rendered":"Android Java Serialization Using Generics"},"content":{"rendered":"<p>I have created a class &#8211; <strong>SerializationManager<\/strong>, it can serialize any objects and write them to files. It can be used to save and restore form states in <strong>Android<\/strong>. It is based on <strong>Java<\/strong> <strong>Generics<\/strong>. What is achieved by this is that you do not need to duplicate serialization code in every <strong>class<\/strong>. <\/p>\n<p><a href=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/images.jpeg\"><img decoding=\"async\" src=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/images.jpeg\" alt=\"images\" width=\"200\" class=\"aligncenter size-full wp-image-2364\" \/><\/a><\/p>\n<p>Object must implement <strong>Serialized<\/strong> interface. Here <strong>form<\/strong> is an object instance of a class. It can be named as just <strong>obj<\/strong>. <\/p>\n<pre>\r\npackage com.alwawee.util;\r\n\r\nimport android.content.Context;\r\n\r\nimport java.io.FileInputStream;\r\nimport java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.io.ObjectInputStream;\r\nimport java.io.ObjectOutputStream;\r\n\r\npublic class SerializationManager<T> {\r\n\r\n    \/\/================================================================================\r\n    \/\/ Constructor\r\n    \/\/================================================================================\r\n    public SerializationManager(){\r\n\r\n    }\r\n\r\n\r\n    \/\/================================================================================\r\n    \/\/ Other\r\n    \/\/================================================================================\r\n    \/\/ Reads saved object from a file. Object must implement Serialized interface.\r\n    public T readObjectFromFile(Context context, String className) {\r\n        T form = null;\r\n        try {\r\n            String fileName = className + \".ser\";\r\n            FileInputStream fileInputStream = context.openFileInput(fileName);\r\n            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);\r\n            form = (T) objectInputStream.readObject();\r\n            objectInputStream.close();\r\n            fileInputStream.close();\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        }\r\n        catch (ClassNotFoundException e) {\r\n            e.printStackTrace();\r\n        }\r\n        return form;\r\n    }\r\n\r\n\r\n    \/\/ Serializes object and saves it to a file. File name is created using object type (class name). \r\n    public void saveObjectToFile(Context context, T form) {\r\n        try {\r\n            String fileName = form.getClass().getSimpleName() + \".ser\";\r\n            FileOutputStream fileOutputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);\r\n            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);\r\n            objectOutputStream.writeObject(form);\r\n            objectOutputStream.close();\r\n            fileOutputStream.close();\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>This is how you can use it. First you declare a <strong>SerializationManager<\/strong> in your class and you tell it which class you are going to serialize: <\/p>\n<pre>\r\nprivate SerializationManager<CreateResumeForm> manager;\r\n<\/pre>\n<p>Then in <strong>onCreate<\/strong> method you create <strong>SerializationManager<\/strong> instance:<\/p>\n<pre>\r\n@Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        manager = new SerializationManager<CreateResumeForm>();\r\n<\/pre>\n<p>Writing object to a file:<\/p>\n<pre>\r\n\/\/ Saving to a file\r\nmanager.saveObjectToFile(this, form);\r\n<\/pre>\n<p>Reading objects from saved file: <\/p>\n<pre>\r\n\/\/ Reading form object from a file\r\nform = manager.readObjectFromFile(this, \"CreateResumeForm\");\r\n<\/pre>\n<p>A little joke for reading to the end:<br \/>\n<a href=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/java-generics.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/java-generics-300x300.jpg\" alt=\"java generics\" width=\"300\" height=\"300\" class=\"aligncenter size-medium wp-image-2368\" srcset=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/java-generics-300x300.jpg 300w, https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/java-generics-150x150.jpg 150w, https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/java-generics.jpg 320w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have created a class &#8211; SerializationManager, it can serialize any objects and write them to files. It can be used to save and restore form states in Android. It is based on Java Generics. What is achieved by this is that you do not need to duplicate serialization code in every class. Object must [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[188,143,140],"class_list":["post-2359","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-generics","tag-serialization"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Android Java Serialization Using Generics - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=2359\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android Java Serialization Using Generics - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"I have created a class &#8211; SerializationManager, it can serialize any objects and write them to files. It can be used to save and restore form states in Android. It is based on Java Generics. What is achieved by this is that you do not need to duplicate serialization code in every class. Object must [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=2359\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2014-10-08T15:11:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-10-08T15:34:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/images.jpeg\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"Android Java Serialization Using Generics\",\"datePublished\":\"2014-10-08T15:11:01+00:00\",\"dateModified\":\"2014-10-08T15:34:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359\"},\"wordCount\":131,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/10\\\/images.jpeg\",\"keywords\":[\"Android\",\"Generics\",\"serialization\"],\"articleSection\":[\"Android\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359\",\"name\":\"Android Java Serialization Using Generics - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/10\\\/images.jpeg\",\"datePublished\":\"2014-10-08T15:11:01+00:00\",\"dateModified\":\"2014-10-08T15:34:30+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359#primaryimage\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/10\\\/images.jpeg\",\"contentUrl\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/10\\\/images.jpeg\",\"width\":217,\"height\":232},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2359#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android Java Serialization Using Generics\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android Java Serialization Using Generics - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=2359","og_locale":"en_US","og_type":"article","og_title":"Android Java Serialization Using Generics - Denis Kutlubaev","og_description":"I have created a class &#8211; SerializationManager, it can serialize any objects and write them to files. It can be used to save and restore form states in Android. It is based on Java Generics. What is achieved by this is that you do not need to duplicate serialization code in every class. Object must [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=2359","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2014-10-08T15:11:01+00:00","article_modified_time":"2014-10-08T15:34:30+00:00","og_image":[{"url":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/images.jpeg","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=2359#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=2359"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"Android Java Serialization Using Generics","datePublished":"2014-10-08T15:11:01+00:00","dateModified":"2014-10-08T15:34:30+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=2359"},"wordCount":131,"commentCount":0,"image":{"@id":"https:\/\/blog.alwawee.ru\/?p=2359#primaryimage"},"thumbnailUrl":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/images.jpeg","keywords":["Android","Generics","serialization"],"articleSection":["Android"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=2359#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=2359","url":"https:\/\/blog.alwawee.ru\/?p=2359","name":"Android Java Serialization Using Generics - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=2359#primaryimage"},"image":{"@id":"https:\/\/blog.alwawee.ru\/?p=2359#primaryimage"},"thumbnailUrl":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/images.jpeg","datePublished":"2014-10-08T15:11:01+00:00","dateModified":"2014-10-08T15:34:30+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=2359#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=2359"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.alwawee.ru\/?p=2359#primaryimage","url":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/images.jpeg","contentUrl":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/10\/images.jpeg","width":217,"height":232},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=2359#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"Android Java Serialization Using Generics"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2359"}],"version-history":[{"count":0,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2359\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}