java - 将我的 writeJsonFile 函数从 GSON 更改为 Jackson

标签 java json jackson gson

我有一些代码,它接受描述符列表并使用 GSON 库将它们写入不同的 JSON 文件。我现在正尝试将该库更改为 jackson 。我不是 jackson 专家,所以我正在寻求一些帮助。这是我使用 GSON 时的代码:

描述符类:

public class Descriptor {
    @SerializedName("BatchName")
    private String batchName;

    @SerializedName("Metadata")
    private Metadata metadata;

    @SerializedName("SampleInfo")
    private SampleInfoJsonModel sampleInfo;

    @SerializedName("Files")
    private List<String> files;

    @SerializedName("ClientData")
    private ClientData clientData;

    @SerializedName("CaseName")
    private String caseName;

    public Descriptor() {
        this.metadata = new Metadata();
        this.sampleInfo = new SampleInfoJsonModel();
        this.files = new ArrayList<String>();
        this.clientData = new ClientData();
    }

    public String getBatchName() {
        return batchName;
    }

    public void setBatchName(String batchName) {
        this.batchName = batchName;
    }

    public Metadata getMetadata() {
        return metadata;
    }

    public void setMetadata(Metadata metadata) {
        this.metadata = metadata;
    }

    public SampleInfoJsonModel getSampleInfo() {
        return sampleInfo;
    }

    public void setSampleInfo(SampleInfoJsonModel sampleInfo) {
        this.sampleInfo = sampleInfo;
    }

    public List<String> getFiles() {
        return files;
    }

    public void setFiles(List<String> files) {
        this.files = files;
    }

    public ClientData getClientData() {
        return clientData;
    }

    public void setClientData(ClientData clientData) {
        this.clientData = clientData;
    }

    public String getCaseName() {
        return caseName;
    }

    public void setCaseName(String caseName) {
        this.caseName = caseName;
    }

    public ClientData getClientDataNoCountryCodes() {
        // TODO Auto-generated method stub
        return null ;
    }

}

我的写入JSON文件函数:

public static void writeJsonFile(List<Descriptor> descriptors) {

        try {
            for(Descriptor descriptor : descriptors) {
                BufferedWriter buffWrite = new BufferedWriter(new FileWriter("descriptor_"+descriptor.getCaseName()+".json"));
                Gson gson = new GsonBuilder().setPrettyPrinting().create();

                buffWrite.write(gson.toJson(descriptor));

                buffWrite.close();
            }
        }

        catch (IOException ioe) {
            System.err.println("Error while writing to json file in writeJsonFile: ");
            ioe.printStackTrace();
        }
    }

这是我在 jackson 写的内容:

 BufferedWriter buffWrite = new BufferedWriter(new FileWriter("descriptor_"+descriptor.getCaseName()+".json"));
                ObjectMapper mapper = new ObjectMapper();
                mapper.enable(SerializationFeature.INDENT_OUTPUT);
                buffWrite.write(mapper.writeValueAsString(descriptor));

这与 GSON 中的以下代码等效吗?

BufferedWriter buffWrite = new BufferedWriter(new FileWriter("descriptor_"+descriptor.getCaseName()+".json"));
                Gson gson = new GsonBuilder().setPrettyPrinting().create();
                buffWrite.write(gson.toJson(descriptor));
                buffWrite.close();

最佳答案

我认为您正在寻找为您的对象生成漂亮的 JSON 输出并尝试将其写入文件。

您必须确保在对象属性上使用来自 jackson 的 @SerializedName 等效注释,即 @JsonProperty

您还可以使用以下方法使用 jackson ObjectMapper 来美化 JSON

mapper.writerWithDefaultPrettyPrinter().writeValueAsString(descriptorObj)

请注意,设置 SerializationFeature.INDENT_OUTPUT 也将有助于执行您已经想到的相同操作。

还有Files API 对于文件相关操作非常有用。

希望这会有所帮助!

关于java - 将我的 writeJsonFile 函数从 GSON 更改为 Jackson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51828666/

相关文章:

java - Spring Boot/Java 应用程序中的实体错误

java - 用jrebel-maven-plugin在jar模块根目录生成rebel.xml

c# - Web API 2 - 实现补丁

android - com.fasterxml.jackson.databind.exc.MismatchedInputException : Unexpected token (START_OBJECT), 预期 START_ARRAY:

java - 从 RecyclerView 中的 ViewHolder 中删除小部件

java - 如何在 java 中使用 JRSwapFileVirtualizer 进行碧 Jade 报告

ios - UIPickerView 无法显示 JSON 数组数据

javascript - 如何获取 Backbone.Collection 的子集?

java - 我需要在我的 pojo (java) 类中分配一个变量名 "package",以便可以通过对象映射器类绑定(bind)它,但它显示错误

java - 使用 Jackson 在 Java 中定义的 POJO 将 JSON 摄取到 Spark RDD 中