Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]v2.0.21 当从字符串中截取一部分作为格式化json时,对注释部分不能正确处理 #1073

Closed
wushilei111 opened this issue Jan 13, 2023 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@wushilei111
Copy link

问题描述

简要描述您碰到的问题。
当json字符串中存在注释时,如果使用JSON.parseObject(str)可以正确处理。
如果使用JSON.parseObject(str,offset,length)可能无法正确处理。

环境信息

请填写以下信息:

  • OS信息: [e.g.:Windows 4Core 3.10GHz 16 GB]
  • JDK信息: [e.g.:Openjdk 1.8.0_312]
  • 版本信息:[e.g.:Fastjson2 2.0.21]

重现步骤

如何操作可以重现该问题:

  1. 使用 xxx.xxx 方法
  2. 输入 ... 数据
  3. 出现 ... 错误
//可在此输入示例代码
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.junit.Test;

public class SimpleTest {

    @Test
    public void testJsonOffsetFail () {
        String s = "123456789123456789123456789{//一段注释\n\"name\": \"www\"}";
        JSONObject jsonObject = JSON.parseObject(s, 27, 22);
        System.out.println(jsonObject);
    }
    @Test
    public void testJsonOffsetSucccess () {
        String s = "{//一段注释\n\"name\": \"www\"}";
        JSONObject jsonObject = JSON.parseObject(s, 0, 22);
        System.out.println(jsonObject);
    }
}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。
当从字符串中截取一部分作为格式化json时,能够正确解析注释部分

相关日志输出

请复制并粘贴任何相关的日志输出。
//testJsonOffsetFail
com.alibaba.fastjson2.JSONException: not allow unquoted fieldName, offset 30, character �, line 1, column 31, fastjson-version 2.0.21 {//一段注释
"name": "www"}

at com.alibaba.fastjson2.JSONReader.read(JSONReader.java:1477)
at com.alibaba.fastjson2.JSON.parseObject(JSON.java:254)
at fileLoad.SimpleTest.testJsonOffsetFail(SimpleTest.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)

//testJsonOffsetSucccess
{"name":"www"}

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@wushilei111 wushilei111 added the bug Something isn't working label Jan 13, 2023
@wushilei111
Copy link
Author

image
问题应该出在这里

@wenshao wenshao added the fixed label Jan 14, 2023
@wenshao wenshao added this to the 2.0.23 milestone Jan 14, 2023
@wenshao
Copy link
Member

wenshao commented Jan 14, 2023

https://github.com/alibaba/fastjson2/releases/tag/2.0.23
问题已经修复,请用新版本

@wenshao wenshao closed this as completed Jan 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants