英文:
How to compare the keys and the content of a yaml-file in Java?
问题
我想找一个能够比较两个YAML文件内容的框架。
我找到了一个链接,里面介绍了如何在Ruby中实现这个功能。
https://stackoverflow.com/questions/6274126/how-to-compare-keys-in-yaml-files
但我正在寻找一个Java框架来实现这个功能。
如果结果能够像“git diff”命令一样显示,那就太好了。
谢谢。
英文:
I want to find a framework,
which is able to compare the content of two yaml-files.
I found a link, how they describe it in ruby.
https://stackoverflow.com/questions/6274126/how-to-compare-keys-in-yaml-files
But I am searching for a Java-Framework for it.
It would be nice, if the result can be shown like a "git diff" command.
Thanks
答案1
得分: 3
你可以使用[zjsonpatch][1]和jackson-yaml进行比较...
添加以下依赖:
implementation 'com.flipkart.zjsonpatch:zjsonpatch:0.4.10'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.8'
尝试以下代码:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.flipkart.zjsonpatch.JsonDiff;
import java.io.File;
public class Main {
public static void main(String[] args) throws Exception{
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
JsonNode file1 = objectMapper.readTree(new File("/your/file/path/file1.yml"));
JsonNode file2 = objectMapper.readTree(new File("/your/file/path/file2.yml"));
JsonNode patch = JsonDiff.asJson(file1, file2);
String diffs = patch.toString();
System.out.println(diffs);
}
}
[1]: https://github.com/flipkart-incubator/zjsonpatch
英文:
You can compare by using zjsonpatch and jackson-yaml...
add below dependency:
implementation 'com.flipkart.zjsonpatch:zjsonpatch:0.4.10'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.8'
Try with this:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.flipkart.zjsonpatch.JsonDiff;
import java.io.File;
public class Main {
public static void main(String[] args) throws Exception{
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
JsonNode file1 = objectMapper.readTree(new File("/your/file/path/file1.yml"));
JsonNode file2 = objectMapper.readTree(new File("/your/file/path/file2.yml"));
JsonNode patch = JsonDiff.asJson(file1, file2);
String diffs = patch.toString();
System.out.println(diffs);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论