{"id":152,"date":"2012-09-04T14:36:48","date_gmt":"2012-09-04T20:36:48","guid":{"rendered":"http:\/\/sha.nnoncarey.com\/blog\/?p=152"},"modified":"2012-11-16T18:07:53","modified_gmt":"2012-11-17T00:07:53","slug":"152","status":"publish","type":"post","link":"https:\/\/sha.nnoncarey.com\/blog\/archives\/152","title":{"rendered":"ActiveModel JSON serialization"},"content":{"rendered":"<p>If you have code:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">&lt;%= JSON.pretty_generate(JSON.parse(@myobject.as_json)) %&gt;<\/pre>\n<p>that results in <code>\"can't convert Hash into String\"<\/code><br \/>\nOr<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">&lt;%= JSON.pretty_generate(@myobject.as_json) %&gt;<\/pre>\n<p>That results in: <code>\"undefined method `key?' for #<JSON::Ext::Generator::State:0x007f80e0abcf30>\"<\/code><\/p>\n<p>Try:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">&lt;%= JSON.pretty_generate(JSON.parse(@page.to_json)) %&gt;<\/pre>\n<p>And if as_json or to_json is not obeying your &#8220;only:&#8221;, &#8220;include:&#8221;, or &#8220;except:&#8221; directives, make sure you include ActiveModel&#8217;s JSON if your object is not an ActiveRecord object (or other object that inherits it already).<\/p>\n<p>Along with that, you&#8217;ll need to implement an attributes method. As mentioned in ActiveModel::Serialization, &#8220;You need to declare some sort of attributes hash which contains the attributes you want to serialize and their current value.&#8221; The weird thing is that it doesn&#8217;t matter what the values returned in the hash are. It only obeys the keys, calling .send(key) on your object to get the value for each key.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">\r\nclass MyThing\r\n  include ActiveModel::Serializers::JSON\r\n  ...\r\n  # Needed for ActiveModel::Serialization and things that use it (such as JSON)\r\n  def attributes\r\n    {\r\n        &#039;myattr&#039; =&gt; nil,\r\n        &#039;anotherattr&#039; =&gt; nil\r\n    }\r\n  end\r\nend\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you have code: &lt;%= JSON.pretty_generate(JSON.parse(@myobject.as_json)) %&gt; that results in &#8220;can&#8217;t convert Hash into String&#8221; Or &lt;%= JSON.pretty_generate(@myobject.as_json) %&gt; That results in: &#8220;undefined method `key?&#8217; for #&#8221; Try: &lt;%= JSON.pretty_generate(JSON.parse(@page.to_json)) %&gt; And if as_json or to_json is not obeying your &#8220;only:&#8221;, &#8220;include:&#8221;, or &#8220;except:&#8221; directives, make sure you include ActiveModel&#8217;s JSON if your object is &hellip; <a href=\"https:\/\/sha.nnoncarey.com\/blog\/archives\/152\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;ActiveModel JSON serialization&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-152","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/comments?post=152"}],"version-history":[{"count":18,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/152\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}