devinus / poison

An incredibly fast, pure Elixir JSON library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question regarding encoding Map to JSON

mjvcallibrity opened this issue · comments

Hello,

I currently am working on encoding an elixir map to JSON and noticed some things that I was confused by.

First I noticed that where I put True in my original Map gets encoded to Elixir.True. Why is that and how do I change that?

Second, the Content part of my map gets encoded to JSON with triple escapes. Is this expected behavior and is there a way to change that?

Any help you can offer would be greatly appreciated.

(Sorry if you read this before I made my edits. My original post came off sounding terse and without context. I had outlined what I wanted to ask and accidentally hit submit)

Here is the original Elixir Map I'm encoding:

@content_beginning ~s(<root available-locales="en_US" default-locale="en_US">  <dynamic-element name="content" type="text_area" index-type="keyword" instance-id="mwab">    <dynamic-content language-id="en_US"><![CDATA[)
@content_ending ~s(]]></dynamic-content>  </dynamic-element> </root>)

    body = Poison.encode!(%{
      "groupId" => 20143,
      "folderId" => 32641,
      "classNameId" => 0,
      "classPK" => 0,
      "autoArticleId" => True,
      "titleMap" => %{"en_US" => title},
      "descriptionMap" => %{"en_US" => "Default description"},
      "Content" => @content_beginning <> content <> @content_ending,
      "ddmStructureKey" => "BASIC-WEB-CONTENT",
      "ddmTemplateKey" => "BASIC-WEB-CONTENT",
      "displayDateMonth" => 1,
      "displayDateDay" => 1,
      "displayDateYear" => 2017,
      "displayDateHour" => 1,
      "displayDateMinute" => 1,
      "expirationDateMonth" => 1,
      "expirationDateDay" => 1,
      "expirationDateYear" => 2111,
      "expirationDateHour" => 1,
      "expirationDateMinute" => 1,
      "neverExpire" => True,
      "reviewDateMonth" => 1,
      "reviewDateDay" => 1,
      "reviewDateYear" => 2111,
      "reviewDateHour" => 1,
      "reviewDateMinute" => 1,
      "neverReview" => True,
      "Indexable" => True,
      "articleURL" => "my-url"
    })

And here is the resulting JSON:

"{\"titleMap\":{\"en_US\":\"invalidPhone\"},
\"reviewDateYear\":2111,
\"reviewDateMonth\":1,
\"reviewDateMinute\":1,
\"reviewDateHour\":1,
\"reviewDateDay\":1,
\"neverReview\":\"Elixir.True\",
\"neverExpire\":\"Elixir.True\",
\"groupId\":20143,
\"folderId\":32641,
\"expirationDateYear\":2111,
\"expirationDateMonth\":1,
\"expirationDateMinute\":1,
\"expirationDateHour\":1,
\"expirationDateDay\":1,
\"displayDateYear\":2017,
\"displayDateMonth\":1,
\"displayDateMinute\":1,
\"displayDateHour\":1,
\"displayDateDay\":1,
\"descriptionMap\":{\"en_US\":\"Default description\"},
\"ddmTemplateKey\":\"BASIC-WEB-CONTENT\",
\"ddmStructureKey\":\"BASIC-WEB-CONTENT\",
\"classPK\":0,
\"classNameId\":0,
\"autoArticleId\":\"Elixir.True\",
\"articleURL\":\"my-url\",
\"Indexable\":\"Elixir.True\",
\"Content\":\"<root available-locales=\\\"en_US\\\" default-locale=\\\"en_US\\\">  <dynamic-element name=\\\"content\\\" type=\\\"text_area\\\" index-type=\\\"keyword\\\" instance-id=\\\"mwab\\\">    <dynamic-content language-id=\\\"en_US\\\"><![CDATA[<p>Phone number entered does not appear to be valid.</p>]]></dynamic-content>  </dynamic-element> </root>\"}"

True in Elixir is an alias, or a module name in other words, when they module names are printed as atoms they get Elixir. prepended. You were probably meaning to use the lower case true instead, which is the boolean true value.

When elixir strings are inspected " gets escaped, " inside strings in json are also escaped. If you print your JSON string with IO.puts(json) instead of IO.inspect(json) it may look as you expect.