0

我们有这个处理特殊字符的函数。例如,此函数将转换He shouldn’t be allowedHe 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
      }

现在我们正在尝试在客户端(python)使用这个 API 响应得到错误 UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 11: ordinal not in range(128)。如果我们.encode("utf8")在客户处申请回馈,He shouldn�t be allowed但我们会丢失原始表格He shouldn’t be allowed

如果UnicodeEncodeError没有.encode("utf8"). 如何在 scala front 实现它。

4

1 回答 1

1
于 2021-09-02T06:18:19.633 回答