title: Getting Gson MalformedJsonException: Unterminated array
body: <p>I am creating a custom keyboard app. In the <code>onCreateInputView</code> method I have the following code inside of a coroutine that parses a file (using <code>JsonParser</code> from the Gson library) from <a href="https://github.com/vedantroy/image-test/raw/master/index.json" rel="nofollow noreferrer">https://github.com/vedantroy/image-test/raw/master/index.json</a>.</p>
  
  <pre><code>Log.d("VED-APP","Parsing File From Github")
  JsonParser().parse(fetchString("https://github.com/vedantroy/image-test/raw/master/index.json"))
  </code></pre>
  
  <p>where <code>fetchString()</code> is the following method:</p>
  
  <pre><code>private suspend fun fetchString(url: String): String = suspendCoroutine { cont -&gt;
      url.httpGet().header(Pair("pragma", "no-cache"), Pair("cache-control", "no-cache")).responseString { _, _, result -&gt;
          //Log.d("VED-APP", r.toString())
  
          when (result) {
              is Result.Failure -&gt; {
                  cont.resumeWithException(result.getException())
              }
              is Result.Success -&gt; {
                  cont.resume(result.value)
              }
          }
  
      }
  }
  </code></pre>
  
  <p>The above code works fine without any errors. I can successfully parse the file located at the URL using Gson.  </p>
  
  <p>However, I get an exception when I try to parse the file (with the same exact contents) from a local source, instead of github, with the following code.</p>
  
  <pre><code>val layoutFileName = "index.json"
  val layoutFile =  File(filesDir, layoutFileName)
  
  
          //If layout file does not exist, create it and write default layout to file.
          if(!layoutFile.exists()) {
              Log.d("VED-APP","FILE DOES NOT EXIST")
              layoutFile.writeText(resources.getString(R.string.default_keyboard_layout))
          } else {
              Log.d("VED-APP","FILE EXISTS")
  
          }
  
          Log.d("VED-APP","Parsing Local File")
         JsonParser().parse(FileReader(layoutFile))
  </code></pre>
  
  <p>The exception is:</p>
  
  <blockquote>
    <p>com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated array at line 1 column 18 path $.Excited<a href="https://i.stack.imgur.com/hpr3c.png" rel="nofollow noreferrer">1</a></p>
  </blockquote>
  
  <p>Here is the contents of the entry <code>default_keyboard_layout</code> in <code>strings.xml</code>:
  <a href="https://i.stack.imgur.com/hpr3c.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hpr3c.png" alt="Screenshot_Of_Strings.xml"></a></p>
  
  <p>This is very odd because the contents of the <code>default_keyboard_layout</code> entry in <code>strings.xml</code> is the exact same as the contents of the index.json file on github.</p>
  
  <p>I did some debugging and I think I found the source of the error.
  I added this into my code in order to view the contents of <code>index.json</code> before it gets parsed</p>
  
  <pre><code>layoutFile.forEachLine {
              Log.d("VED-APP",it)
          }
  </code></pre>
  
  <p>and I got the following result:</p>
  
  <pre><code> { Excited:[https://github.com/vedantroy/image-test/raw/master/Happy/excited1.gif, https://github.com/vedantroy/image-test/raw/master/Happy/excited2.gif, https://github.com/vedantroy/image-test/raw/master/Happy/excited3.gif], Sad:[https://github.com/vedantroy/image-test/raw/master/Sad/sad1.gif, https://github.com/vedantroy/image-test/raw/master/Sad/sad2.gif, https://github.com/vedantroy/image-test/raw/master/Sad/sad3.gif, https://github.com/vedantroy/image-test/raw/master/Sad/sad4.gif] }
  </code></pre>
  
  <p>Two things are odd about this output. </p>
  
  <p>1) Everything is on one line, although the entry in <code>strings.xml</code> and the file on github are both multiline</p>
  
  <p>2) The file has no quotation marks although the entry in <code>strings.xml</code> and the file on github do have quotation marks.</p>
  
  <p>I suspect these two issues are the source of the problem, but I'm not sure.</p>
  
  <p>Why am I getting this exception and how do I fix it?</p>
  
  <p>Update 1 - </p>
  
  <p>I tried Tynn's solution, by changing my <code>strings.xml</code> too</p>
  
  <pre><code>&lt;resources&gt;
      &lt;string name="app_name"&gt;Anime Face Keyboard&lt;/string&gt;
      &lt;string name="subtype_en_US"&gt;English (US)&lt;/string&gt;
      &lt;string name="default_keyboard_layout"&gt;&lt;![CDATA[{
      "Excited":["https://github.com/vedantroy/image-test/raw/master/Happy/excited1.gif",
                  "https://github.com/vedantroy/image-test/raw/master/Happy/excited2.gif",
                  "https://github.com/vedantroy/image-test/raw/master/Happy/excited3.gif"],
  
      "Sad":["https://github.com/vedantroy/image-test/raw/master/Sad/sad1.gif",
              "https://github.com/vedantroy/image-test/raw/master/Sad/sad2.gif",
              "https://github.com/vedantroy/image-test/raw/master/Sad/sad3.gif",
              "https://github.com/vedantroy/image-test/raw/master/Sad/sad4.gif"]
  }]]&gt;&lt;/string&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>but that did nothing.</p>
  
  <p>I printed the value of <code>resources.getString(R.string.default_keyboard_layout)</code> and it was still missing quotes and newlines.  </p>