org.kohsuke.github中是否有内置机制将GHRepository对象序列化为JSON? [英] Is there a built in mechanism within org.kohsuke.github to serialize GHRepository objects to JSON?

查看:117
本文介绍了org.kohsuke.github中是否有内置机制将GHRepository对象序列化为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用org.kohsuke.github中的Java Github API来检索GHRepository对象.我想将此对象序列化为JSON. org.kohsuke.github客户端中是否提供提供此功能的方法?可能使用Jackson?

I'm using the Java Github API from org.kohsuke.github to retrieve a GHRepository object. I'd like to serialize this object to JSON. Is there a provided way of doing this within the org.kohsuke.github client? Possibly using Jackson?

当前,在使用杰克逊时,我不得不创建一个自定义pojo,以避免杰克逊遇到空的Map键和值.我怀疑这个github库已经有执行此操作的代码,因为我可以看到它在GitHub.java中序列化为JSON,但是我没有看到利用此功能的公开访问的方法.下面的代码遍历我所有组织中的所有存储库,以获取存储库的JSON表示形式,然后将其存储在数据库中.

Currently, when using jackson, I've had to create a custom pojo to avoid Jackson encountering null Map key's and values. I suspect this github library already has code to do this as I can see it serializing to JSON in GitHub.java but I'm not seeing a publicly accessible means of leveraging this. The code below is iterating over all my repos in all my orgs to get the JSON representation of the repo which will then be stored in a database.

// Iterate over all orgs that I can see on enterprise
github.listOrganizations().withPageSize(100).forEach( org -> {

    // Get all the repositories in an org
    Map<String, GHRepository> ghRepositoryMap = org.getRepositories();

    // Iterate over each repo object and serialize
    for (Map.Entry<String, GHRepository> entry :ghRepositoryMap.entrySet()  ) {

        // Serialize to JSON
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); //Handles null fields ok
        String jsonStr = objectMapper.writeValueAsString(entry.getValue()); // <-- this fails due to null key

    }
}

最后一行是:

com.fasterxml.jackson.databind.JsonMappingException:的空键 JSON中不允许映射(使用转换的NullKeySerializer吗?)(通过 参考链: org.kohsuke.github.GHRepository ["responseHeaderFields"]-> java.util.Collections $ UnmodifiableMap ["null"])

com.fasterxml.jackson.databind.JsonMappingException: Null key for a Map not allowed in JSON (use a converting NullKeySerializer?) (through reference chain: org.kohsuke.github.GHRepository["responseHeaderFields"]->java.util.Collections$UnmodifiableMap["null"])

根据检查,我相信这不是唯一的null键.我可能可以通过添加mixins来处理这些问题,从而实现自定义效果,但是我正在寻找org.kohsuke.github库中的内置方法,该方法使此操作更容易,因为它似乎可以在内部执行此操作.

From inspection I believe this isn't the only null key. I could probably customize the heck out of this by adding mixins to handle them but I'm looking for a built in way within the org.kohsuke.github library that makes this easier since it appears to be capable of doing this internally.

推荐答案

您应实现并注册NullKeySerializer.参见以下示例:

You should implement and register NullKeySerializer. See below example:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        Map<String, List<String>> map = new HashMap<>();
        map.put(null, Arrays.asList("A", "B", "C"));
        map.put("regular-key", Arrays.asList("X", "Y"));

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer());

        System.out.println(mapper.writeValueAsString(map));
    }
}

class NullKeySerializer extends JsonSerializer<Object> {
    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeFieldName("null");
    }
}

上面的代码显示:

{
  "null" : [ "A", "B", "C" ],
  "regular-key" : [ "X", "Y" ]
}

此外,您不应为每个条目创建ObjectMapper实例.您可以创建一次并将其用于所有元素.

Also, you should not create ObjectMapper instance for each entry. You can create it once and use for all elements.

这篇关于org.kohsuke.github中是否有内置机制将GHRepository对象序列化为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆