0

We have this function which takes care of special character. For example this function will convert He shouldn’t be allowed to He shouldn�t be allowed.

import io.circe._, io.circe.generic.auto._, 
io.circe.syntax._, io.circe.parser._, io.circe.optics.JsonPath._

private def bytesToJsonCirce(value: Array[Byte]): Json = {
    parse(encodeCharacters(value.map(_.toChar).mkString)) match {
      case Right(x: Json) => x
      case Left(err: ParsingFailure) =>
        logger.error(err.getLocalizedMessage)
        Json.Null
    }
  }  

private def encodeCharacters(x: String): String = {
        val encodeChar = '�'
        (x.toCharArray map {
          case y if y.isControl => encodeChar
          case y => y
        }).mkString
      }

Now we are trying to consume this API response at client side(python) getting error UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 11: ordinal not in range(128) . If we apply .encode("utf8") at client its giving back He shouldn�t be allowed but we are loosing the original form He shouldn’t be allowed.

Also this UnicodeEncodeError is not desired without .encode("utf8"). How it can be achieved at scala front .

curiousguy
  • 3,212
  • 8
  • 39
  • 71

1 Answers1

1

The error message says that \ufffd is not a valid ASCII character:

'ascii' codec can't encode character u'\ufffd'

\ufffd is the Unicode value of so the problem is that the replacement value encodeChar is not a valid ASCII value. Try changing encodeChar to a valid ASCII character.

Tim
  • 26,753
  • 2
  • 16
  • 29
  • I was more looking for instead of replacing by same encoder `�` , can I have some escape mechanism that can help to retain the actual value. – curiousguy Sep 02 '21 at 06:50