{"id":236,"date":"2022-04-17T19:41:30","date_gmt":"2022-04-17T19:41:30","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/scala\/?p=236"},"modified":"2022-04-24T12:35:37","modified_gmt":"2022-04-24T12:35:37","slug":"scala-build-json-encoder-with-circe","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/scala\/scala-build-json-encoder-with-circe\/","title":{"rendered":"Scala \u2013 Build Json Encoder With Circe"},"content":{"rendered":"<p>In the preceding tutorial, we explained <a href=\"https:\/\/www.kindsonthegenius.com\/scala\/scala-json-encoding-with-circe\/\" target=\"_blank\" rel=\"noopener\">how Json Decoding with Circe works in Scala<\/a>. We also learnt how to create and manipulated Json values including nested fields. In this tutorial, we would then build the actual encoders.<\/p>\n<ol>\n<li><a href=\"#t1\">Create the Types and Instances<\/a><\/li>\n<li><a href=\"#t2\">Build Your Encoders<\/a><\/li>\n<li><a href=\"#t3\">Using Your Encoders<\/a><\/li>\n<li><a href=\"#t4\">Bonus Encoders!<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Create the Types and Instances<\/strong><\/h4>\n<p>We would be bulding encoders for two custom data types: Article and Author.<\/p>\n<p>The Author data type is given below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/The Author data type<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Author<\/span><span style=\"color: #333333;\">(<\/span>\r\n                   name<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">,<\/span> \r\n                   bio<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Option<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">]<\/span>\r\n                 <span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The Article data type is give below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/The Article data type<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Article<\/span><span style=\"color: #333333;\">(<\/span>\r\n                    id<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">UUID<\/span><span style=\"color: #333333;\">,<\/span>\r\n                    title<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">,<\/span>\r\n                    content<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">String<\/span><span style=\"color: #333333;\">,<\/span>\r\n                    author<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Author<\/span>\r\n                  <span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>Note from the above type definition that the Author type is part of the Article type.<\/p>\n<p>These classes by the way are <em><strong>case classes<\/strong><\/em>. These classes provide the apply method which takes care of object construction. So you don&#8217;t have to use the <strong><em>new<\/em><\/strong> keyword for creating new objects. They are normally used to model immutable types. <a href=\"https:\/\/docs.scala-lang.org\/tour\/case-classes.html\" target=\"_blank\" rel=\"noopener\">More on case classes here<\/a>.<\/p>\n<p>Let&#8217;s now create a few instances of Author and Article that would use to test the encoder that we will build.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Create two Article instances<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">val<\/span> article1<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Article<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Article<\/span> <span style=\"color: #333333;\">(<\/span>\r\n    <span style=\"color: #bb0066; font-weight: bold;\">UUID<\/span><span style=\"color: #333333;\">.<\/span>randomUUID<span style=\"color: #333333;\">(),<\/span>\r\n    <span style=\"background-color: #fff0f0;\">\"All That Glitters is Not Gold\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n    <span style=\"background-color: #fff0f0;\">\"Take it or Leave it\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n    author1    \r\n<span style=\"color: #333333;\">)<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">val<\/span> article2<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Article<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Article<\/span> <span style=\"color: #333333;\">(<\/span>\r\n    <span style=\"color: #bb0066; font-weight: bold;\">UUID<\/span><span style=\"color: #333333;\">.<\/span>randomUUID<span style=\"color: #333333;\">(),<\/span>\r\n    <span style=\"background-color: #fff0f0;\">\"You Dance to Purple Rain\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n    <span style=\"background-color: #fff0f0;\">\"Like We Used To\"<\/span><span style=\"color: #333333;\">,<\/span>\r\n    author2\r\n<span style=\"color: #333333;\">)<\/span>    \r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Build the Encoders<\/strong><\/h4>\n<p>Now we would go ahead to build the encoder that could convert from an instance of Author to Json values. Same for Article as well.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ An Encoder to encoder an an Author type into Json Value<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">implicit<\/span> <span style=\"color: #008800; font-weight: bold;\">val<\/span> authorEncoder<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Encoder<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">Author<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> author <span style=\"color: #008800; font-weight: bold;\">=&gt;<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Json<\/span><span style=\"color: #333333;\">.<\/span>obj<span style=\"color: #333333;\">(<\/span>\r\n    <span style=\"background-color: #fff0f0;\">\"name\"<\/span> <span style=\"color: #333333;\">-&gt;<\/span> author<span style=\"color: #333333;\">.<\/span>name<span style=\"color: #333333;\">.<\/span>asJson<span style=\"color: #333333;\">,<\/span>\r\n    <span style=\"background-color: #fff0f0;\">\"bio\"<\/span> <span style=\"color: #333333;\">-&gt;<\/span> author<span style=\"color: #333333;\">.<\/span>bio<span style=\"color: #333333;\">.<\/span>asJson\r\n<span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>The encoder for Article type is shown below, I call it articleEncoder<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/An Encoder to encoder an article type into Json value<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">implicit<\/span> <span style=\"color: #008800; font-weight: bold;\">val<\/span> articleEncoder<span style=\"color: #008800; font-weight: bold;\">:<\/span> <span style=\"color: #333399; font-weight: bold;\">Encoder<\/span><span style=\"color: #333333;\">[<\/span><span style=\"color: #333399; font-weight: bold;\">Article<\/span><span style=\"color: #333333;\">]<\/span> <span style=\"color: #008800; font-weight: bold;\">=<\/span> article <span style=\"color: #008800; font-weight: bold;\">=&gt;<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Json<\/span><span style=\"color: #333333;\">.<\/span>obj<span style=\"color: #333333;\">(<\/span>\r\n    <span style=\"background-color: #fff0f0;\">\"id\"<\/span> <span style=\"color: #333333;\">-&gt;<\/span> article<span style=\"color: #333333;\">.<\/span>id<span style=\"color: #333333;\">.<\/span>asJson<span style=\"color: #333333;\">,<\/span>\r\n    <span style=\"background-color: #fff0f0;\">\"title\"<\/span> <span style=\"color: #333333;\">-&gt;<\/span> article<span style=\"color: #333333;\">.<\/span>title<span style=\"color: #333333;\">.<\/span>asJson<span style=\"color: #333333;\">,<\/span>\r\n    <span style=\"background-color: #fff0f0;\">\"content\"<\/span> <span style=\"color: #333333;\">-&gt;<\/span> article<span style=\"color: #333333;\">.<\/span>content<span style=\"color: #333333;\">.<\/span>asJson<span style=\"color: #333333;\">,<\/span>\r\n    <span style=\"background-color: #fff0f0;\">\"author\"<\/span> <span style=\"color: #333333;\">-&gt;<\/span> article<span style=\"color: #333333;\">.<\/span>author<span style=\"color: #333333;\">.<\/span>asJson\r\n<span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Using your Encoder<\/strong><\/h4>\n<p>How then do you use your encoder to encoder instances?<\/p>\n<p>To use the encoder to encode an instance, you simply call the asJson function on the instance. The code below encodes the instances of author1 and article1 into Json values and prints them out to the console<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Perform Encoding using your encoder<\/span>\r\nprintln<span style=\"color: #333333;\">(<\/span>article1<span style=\"color: #333333;\">.<\/span>asJson<span style=\"color: #333333;\">)<\/span>\r\n    \r\nprintln<span style=\"color: #333333;\">(<\/span>author1<span style=\"color: #333333;\">.<\/span>asJson<span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Bonus Encoders!<\/strong><\/h4>\n<p>Once you write an encoder for a given type T, then circe will give you the following encoders for T. Therefore, the<strong> Encoder[Author]<\/strong>, we would have the following encoders for free:<\/p>\n<ul>\n<li>Encoder[Option[Author]]<\/li>\n<li>Encoder[List[Author]]<\/li>\n<li>Encoder[Array[Author]]<\/li>\n<li>Encoder[Set[Author]]<\/li>\n<li>Encoder[Map[Author]]<\/li>\n<li>Encoder[Vector[Author]]<\/li>\n<li>Encoder[NonEmptyList[Author]]<\/li>\n<\/ul>\n<p>The code below creates a list of authors and uses the free Encoder[List[Author]] to encodes the list of authors and print them out<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Using the free encoders<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">val<\/span> authorList <span style=\"color: #008800; font-weight: bold;\">=<\/span> <span style=\"color: #bb0066; font-weight: bold;\">List<\/span><span style=\"color: #333333;\">(<\/span>author1<span style=\"color: #333333;\">,<\/span> author2<span style=\"color: #333333;\">)<\/span>\r\nprintln<span style=\"color: #333333;\">(<\/span>authorList<span style=\"color: #333333;\">.<\/span>asJson<span style=\"color: #333333;\">.<\/span>spaces2<span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Well, so far so good! Now we would talk about Decoders in the next tutorial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the preceding tutorial, we explained how Json Decoding with Circe works in Scala. We also learnt how to create and manipulated Json values including &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,2],"tags":[41,44,43,42],"class_list":["post-236","post","type-post","status-publish","format-standard","hentry","category-circe","category-scala-programming","tag-circe","tag-decoder","tag-encoder","tag-json"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/comments?post=236"}],"version-history":[{"count":4,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/236\/revisions"}],"predecessor-version":[{"id":266,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/posts\/236\/revisions\/266"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/media?parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/categories?post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/scala\/wp-json\/wp\/v2\/tags?post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}