Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question regarding encoding Map to JSON #141

Open
mjvcallibrity opened this issue Jul 28, 2017 · 1 comment
Open

Question regarding encoding Map to JSON #141

mjvcallibrity opened this issue Jul 28, 2017 · 1 comment

Comments

@mjvcallibrity
Copy link

mjvcallibrity commented Jul 28, 2017

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>\"}"
@ericmj
Copy link

ericmj commented Jul 28, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants