非法字符(CTRL-CHAR,代码0)- 解析异常

huangapple go评论87阅读模式
英文:

Illegal character (CTRL-CHAR, code 0) - Parsing Exception

问题

我目前遇到了以下问题:

```json
JsonParseException: 非法字符 ((CTRL-CHAR, code 0)):在标记之间只允许普通空白字符 (\r, \n, \t)
 at [Source: (String)"    ){"BusinessDate"

这是因为在"BusinessDate"之前有一个')'字符,并且在它之前有四个NUL字符,但是它们在这里不显示出来,因为它们是虚构的字符。这是我正在尝试映射到对象的JSON字符串的开头。我只是发送一个以Avro序列化的ByteArray,并尝试使用ByteArray Deserializer进行反序列化(我认为这是问题所在)。我如何摆脱这些虚构的字符,以便它正确地映射到我的对象,或者如何更改我的SCS(Spring Cloud Stream)消费者配置,以便在仅针对该消费者使用Avro反序列化。

// 外部主题监听器
@StreamListener(ChannelsScheduler.SCHEDULER_IN_FROM_EXTERNAL_EVENT)
public void consumeMessage(@Payload GenericMessage<String> message) throws IOException
{
    logger.info("从外部主题消费消息: {}", message);

我尝试过以下方法:

GPTMStatus gptmStatus = mapper.readValue(message.getPayload(), GPTMStatus.class);
GPTMStatus gptmStatus = mapper.readValue(message.getPayload().trim(), GPTMStatus.class);
GPTMStatus gptmStatus = mapper.readValue(message.getPayload().replace(")", ""), GPTMStatus.class);

String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));
GPTMStatus gptmStatus = mapper.readValue(json, GPTMStatus.class);

所有三种方法都收到了与上述相同的错误。

完整的载荷:

"    ){"BusinessDate":"2020-03-05","ContentUri":"20180712_EOB/1583443159984/0.xml","DFReference":"80712_EOB","DFRevision":0,"DFVersion":1583443159984}"

这是我发送消息的方式:

kafka
    .send(
        destination,
        formatter.transform(df.getCanonicalPayload()).getBytes(StandardCharsets.UTF_8))
    .get();

<details>
<summary>英文:</summary>

I am currently running into

JsonParseException: Illegal character ((CTRL-CHAR, code 0)): only regular white space (\r, \n, \t) is allowed between tokens
at [Source: (String)" ){"BusinessDate"

It is because of the &#39;)&#39; before business date and there is four NUL before that, but they don&#39;t show up here as they are imaginary characters. That is the beginning of my json string I&#39;m trying to map to my object. I&#39;m just sending a ByteArray serialized as Avro, and trying to Deserialize using a ByteArray Deserializer (which I&#39;m assuming is the problem). How do I get rid of those imaginary characters so it maps correctly to my object, or change my SCS consumer config to use Avro deserialization, only on that one consumer.

// External topic listener
@StreamListener(ChannelsScheduler.SCHEDULER_IN_FROM_EXTERNAL_EVENT)
public void consumeMessage(@Payload GenericMessage<String> message) throws IOException
{
logger.info("Consumed message from external topic: {}", message);


I have tried
    GPTMStatus gptmStatus = mapper.readValue(message.getPayload(), GPTMStatus.class);
    GPTMStatus gptmStatus = mapper.readValue(message.getPayload().trim(), GPTMStatus.class);
    GPTMStatus gptmStatus = mapper.readValue(message.getPayload().replace(&quot;)&quot;, &quot;&quot;), GPTMStatus.class);

String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));

    GPTMStatus gptmStatus = mapper.readValue(json, GPTMStatus.class);

All three are receiving the same error as above.

Full Payload: 

" ){"BusinessDate":"2020-03-05","ContentUri":"20180712_EOB/1583443159984/0.xml","DFReference":"80712_EOB","DFRevision":0,"DFVersion":1583443159984}"


This is how I&#39;m sending the message:
      kafka
          .send(
              destination,
              formatter.transform(df.getCanonicalPayload()).getBytes(StandardCharsets.UTF_8))
          .get();

</details>


# 答案1
**得分**: 1

这一行略显误导(至少对我来说)

```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));```

JSON<b>可以</b>包含`NULL`控制字符,但仅当它被转义并且位于字符串字段内。我建议从一些简单的东西开始,比如`json.replace("

</details>
# 答案1
**得分**: 1
这一行略显误导(至少对我来说)
```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));```
JSON<b>可以</b>包含`NULL`控制字符,但仅当它被转义并且位于字符串字段内。我建议从一些简单的东西开始,比如`json.replace("\0", "");`,然后检查一下你的代码是否可以继续运行。
<details>
<summary>英文:</summary>
This line is slightly misleading (at least to me)
```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));```
JSON &lt;b&gt;CAN&lt;/b&gt; contain the `NULL` control character but only if it&#39;s escaped and within a string field. I&#39;d suggest starting with something simple, `json.replace(&quot;\0&quot;, &quot;&quot;);` and checking if your code gets any further.
</details>
", "");`,然后检查一下你的代码是否可以继续运行。 <details> <summary>英文:</summary> This line is slightly misleading (at least to me) ```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));``` JSON &lt;b&gt;CAN&lt;/b&gt; contain the `NULL` control character but only if it&#39;s escaped and within a string field. I&#39;d suggest starting with something simple, `json.replace(&quot;

</details>
# 答案1
**得分**: 1
这一行略显误导(至少对我来说)
```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));```
JSON<b>可以</b>包含`NULL`控制字符,但仅当它被转义并且位于字符串字段内。我建议从一些简单的东西开始,比如`json.replace("\0", "");`,然后检查一下你的代码是否可以继续运行。
<details>
<summary>英文:</summary>
This line is slightly misleading (at least to me)
```String json = StringUtils.newStringUtf8(message.getPayload().getBytes(StandardCharsets.UTF_8));```
JSON &lt;b&gt;CAN&lt;/b&gt; contain the `NULL` control character but only if it&#39;s escaped and within a string field. I&#39;d suggest starting with something simple, `json.replace(&quot;\0&quot;, &quot;&quot;);` and checking if your code gets any further.
</details>
&quot;, &quot;&quot;);` and checking if your code gets any further. </details>

huangapple
  • 本文由 发表于 2020年10月15日 02:51:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64359760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定