Java 使用Jackson读取带有嵌入式JS注释的JSON

Java 使用Jackson读取带有嵌入式JS注释的JSON,java,json,parsing,jackson,comments,Java,Json,Parsing,Jackson,Comments,我正在寻找一种处理JSON的方法,其中包括JS注释。我知道,注释对于JSON是不合法的,但不幸的是,我需要读/写带有注释的JSON文件 我找到了一种用Jackson写评论的方法。此代码 JsonGenerator gen = factory.createGenerator(System.out); gen.writeStartObject(); gen.writeStringField("a", "b"); gen.writeRaw("\n/*-

我正在寻找一种处理JSON的方法,其中包括JS注释。我知道,注释对于JSON是不合法的,但不幸的是,我需要读/写带有注释的JSON文件

我找到了一种用Jackson写评论的方法。此代码

JsonGenerator gen = factory.createGenerator(System.out);
gen.writeStartObject();
gen.writeStringField("a", "b");
gen.writeRaw("\n/*------------*/\n");
gen.writeStringField("c", "d");
gen.writeEndObject();
gen.close();
生成以下JSON:

{"a":"b"
/*------------*/
,"c":"d"}
如果我开始解析这个JSON

factory.enable(JsonParser.Feature.ALLOW_COMMENTS);
JsonParser parser = factory.createParser("{\"a\":\"b\"/*------------*/,\"c\":\"d\"}");
while (parser.nextToken() != JsonToken.END_OBJECT) {
    System.out.println(parser.currentToken() + ":" + parser.getText());
}
注释和所有格式都被跳过。甚至没有“RAW”或“COMMENT”类型的JsonToken


有没有一种方法可以使用Jackson(或其他Java库)解析包含原始数据的JSON?

不知道为什么需要解析JSON中的数据。如果您想从json中看到command,为什么不将其作为字符串读取并解析出来呢。下面是一个正则表达式示例,用于阅读

final Pattern pattern = Pattern.compile("/\\*.+\\*/", Pattern.DOTALL);
final Matcher matcher = pattern.matcher("{\"a\":\"b\"/*------------*/,\"c\":\"d\"}");
matcher.find();
System.out.println(matcher.group(0));
NodeJs方式 您可以通过节点包来实现这一点-

基本上,这个库是专门为保持注释的原样而设计的,还允许美化JSON的输出

这是使用
npm I-g comment json安装包后可以做的事情:

$ node
> const { parse, stringify } = require('comment-json') 
> const parsed = parse(`{"a":"b"/*------------*/,"c":"d"}`)
> prased
{ a: 'b', c: 'd' }
> parsed['a'] = 'df'
> parsed
{ a: 'df', c: 'd' }
> stringify(parsed, null, 2)
'{\n  "a": "df" /*------------*/,\n  "c": "d"\n}'
现在,我知道这是一个节点包。我们可以通过node使用它,或者如果确实需要通过Java使用它的话

Java变通方法 安装NPM和软件包: 使用,您可以安装node/npm以及包。因此,您的pom.xml可以如下所示:

<plugin>
    ...
    <executions>
        <execution>
            <!-- optional: you dont really need execution ids -->
            <id>install node and npm</id>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
            <!-- optional: default phase is "generate-resources" -->
            <phase>generate-resources</phase>

            <configuration>
                <nodeVersion>v6.9.1</nodeVersion>
            </configuration>
        </execution>
        <execution>
            <id>npm install</id>
            <goals>
               <goal>npm</goal>
            </goals>

            <!-- optional: default phase is "generate-resources" -->
            <phase>generate-resources</phase>

            <configuration>
            <!-- Install the comment-json-->
            <arguments>install -g comment-json</arguments>
            </configuration>
         </execution>
    </executions>
</plugin>
因此,如果我以如下方式执行它:
node app.js“{\“a\”:\“b\”/*------------------------*/,“c\”:“d\”}“a”“df”
,它将在控制台上打印:

{
  "a": "df" /*------------*/,
  "c": "d"
}
使用
ProcessBuilder
,执行脚本并从console.log捕获输出 也使用

List commands=newlinkedlist();
添加(“节点”);
commands.add(“app.js”);//您需要实际的/path/to/app.js
add(“{\'a\':\'b\'/*--------------*/,\'c\':\'d\'”);
命令。添加(“a”);
命令。添加(“df”);
ProcessBuilder ProcessBuilder=新的ProcessBuilder(命令);
字符串输出=IOUtils.toString(processBuilder.start().getInputStream(),StandardCharsets.UTF_8);

您的JSON是否没有被解析,或者您正在寻找从JSON中提取注释?我需要操作JSON保持不变。@30thh应该是一个纯Java库?或者一个房间来介绍除java以外的任何东西?@Nagaraj一切有用的东西都欢迎:-)我对评论不感兴趣。我需要修改JSON以保留注释(如果可能,还需要格式化)。我需要在JSON中再注入一个字段,保持文档的其余部分不变。Diff工具应该只指向新领域的界线。我不太可能实现如此复杂的集成,但感谢您的出色调查。
{
  "a": "df" /*------------*/,
  "c": "d"
}
List<String> commands = new LinkedList<String>();
commands.add("node");
commands.add("app.js"); // You need the actual /path/to/app.js
commands.add("{\"a\":\"b\"/*------------*/,\"c\":\"d\"}");
commands.add("a");
commands.add("df");

ProcessBuilder processBuilder = new ProcessBuilder(commands);
String output = IOUtils.toString(processBuilder.start().getInputStream(), StandardCharsets.UTF_8);