question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SimpleArrayUtils.fromString results JsonParseException when string contains new line escape

See original GitHub issue

The following code results JsonParseException: Unrecognized character escape ‘w’

val s = """{"{\"title\": \"hello\\nworld\"}"}"""
utils.SimpleArrayUtils.fromString[JsValue](Json.parse)(s).orNull

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
alexandervanheckecommented, Jul 24, 2019

for future reference, the problem seems to be in the PgTokenHelper.getString method. I first tried manipulating the Tokens returned by PgTokenHelper.Tokenizer.tokenize myself, quickly realised I needed the PgTokenHelper.groupingas well and ended up with this :

    import com.github.tminglei.slickpg.utils.PgTokenHelper._

    def tokenToString(token: Seq[Token]): String =
      token.collect {
        case GroupToken(members) => tokenToString(members)
        case t => t.value
      }.mkString("")

    def fromString[T](convert: String => T)(arrString: String): Option[Seq[T]] =
      grouping(Tokenizer.tokenize(arrString)) match {
        case Null => None
        case root => Some(getChildren(root).map {
          case GroupToken(members) => convert(StringEscapeUtils.unescapeJson(tokenToString(members.drop(1).dropRight(1))))
          case _  => null.asInstanceOf[T]
        })
      }

    implicit val playJsonArrayTypeMapper =
      new AdvancedArrayJdbcType[JsValue](pgjson,
        s => fromString[JsValue](Json.parse)(s).orNull,
        v => utils.SimpleArrayUtils.mkString[JsValue](_.toString())(v)).to(_.toList)

the actual unescaping is done using commons-text StringEscapeUtils. My fromString is basically a copy paste of the SimpleArrayUtils.fromStringreplacing the getString call with an unescapeJson call. This seems to works ok for all kinds of escaped json text inside JSON values.

1reaction
zgrannancommented, Sep 14, 2018

I encountered this error myself. Here is a problematic json value:

"hello \\n world"

This represents hello + a literal backslash + n world, NOT a newline.

When stored in a jsonb column this is no problem, but if it is an element of a jsonb[] array it is incorrectly parsed as:

"hello \\ world"

I have included a failing test case on my branch here: https://github.com/zgrannan/slick-pg/commit/4543de1b9a7c63836a048a59598255e66a0d8405

FYI as a workaround, one can change their db’s jsonb[] column to a json column and convert the data using array_to_json

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to escape a JSON string containing newline characters ...
I tried escaping the new line character \n to \\n. It is working fine. But i am looking for any JS library which...
Read more >
Adding a Newline Character To a String In Java - Xperti
Find out how to add a new line to a string in Java. ... See the examples below, here escape sequences are used...
Read more >
How to erase newline character from string? - MATLAB Answers
Hello,. I am reading an xml file that contains new line (enter) character. How do I detect and erase it?
Read more >
Remove Newline from Character String in R - GeeksforGeeks
In this article, we are going to see how to remove the new line from a character string in R Programming Language. Example:...
Read more >
Python: Insert New Line into String - STechies
Mentioning the newline character using n will bring the cursor to the consecutive line. Example 1 print('Hello n STechies'). Output: Hello STechies. We...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found