2

我开始使用Moshi编写 Java 库Brockman来解析类似于此示例的 JSON 响应。但是,该格式需要对对象进行一些概括。stream

视频流摘录:

{
    "slug": "hd-native",
    "display": "Saal 1 FullHD Video",
    "type": "video",
    "isTranslated": false,
    "videoSize": [
        1920,
        1080
    ],
    "urls": {
        "webm": {
            "display": "WebM",
            "tech": "1920x1080, VP8+Vorbis in WebM, 2.8 MBit/s",
            "url": "http://example.com/s1_native_hd.webm"
        },
        "hls": {
            "display": "HLS",
            "tech": "1920x1080, h264+AAC im MPEG-TS-Container via HTTP",
            "url": "http://example.com/hls/s1_native_hd.m3u8"
        }
    }
}

音频流摘录:

{
    "slug": "audio-native",
    "display": "Saal 1 Audio",
    "type": "audio",
    "isTranslated": false,
    "videoSize": null,
    "urls": {
        "mp3": {
            "display": "MP3",
            "tech": "MP3-Audio, 96 kBit/s",
            "url": "http://example.com/s1_native.mp3"
        },
        "opus": {
            "display": "Opus",
            "tech": "Opus-Audio, 64 kBit/s",
            "url": "http://example.com/s1_native.opus"
        }
    }
}

对象的内容会urls = {}有所不同,具体取决于是否stream type存在videoaudio从上面的示例中可以看到。

目前,只有模型Mp3Opus它们的属性相同。我想用一个Format类来代替它们,它也可以作为缺失WebmHls类的替代品。我如何才能将Urls对象的不同字段实际映射到Format类中?

public class Format {

    public final String display;
    public final String tech;
    public final String url;

    public Format(String display, String tech, String url) {
        this.display = display;
        this.tech = tech;
        this.url = url;
    }
}

我可以想象这个Stream看起来像这样:

public class Stream {

    public final String display;
    public final boolean isTranslated;
    public final String slug;
    public final String type;
    public final VideoSize videoSize;
    public final List<Format> urls; // How to map into List<Format>?

// ...
4

1 回答 1

1

我想出了以下内容:

public class StreamAdapter {

    @ToJson
    public String toJson(Stream stream) throws Exception {
        throw new UnsupportedOperationException("Not yet implemented.");
    }

    @FromJson
    public Stream fromJson(StreamJson streamJson) throws Exception {
        String slug = streamJson.slug;
        String display = streamJson.display;
        boolean isTranslated = streamJson.isTranslated;
        Stream.TYPE type = streamJson.type;
        VideoSize videoSize = streamJson.videoSize;
        Map<String, Object> urlsJson = streamJson.urls;
        List<Url> urls = getUrls(urlsJson);
        return new Stream(display, isTranslated, slug, type, videoSize, urls);
    }

    private List<Url> getUrls(Map<String, Object> urlsJson) {
        Set<String> urlTypes = urlsJson.keySet();
        List<Url> urls = new ArrayList<Url>(urlTypes.size());
        for (String urlType : urlTypes) {
            @SuppressWarnings("unchecked")
            Map<String, String> urlProperties = (Map<String, String>) urlsJson.get(urlType);
            urls.add(getUrl(urlType, urlProperties));
        }
        return urls;
    }

    private Url getUrl(String urlType, Map<String, String> properties) {
        Url.TYPE type = new UrlTypeAdapter().fromJson(urlType);
        String display = properties.get("display");
        String tech = properties.get("tech");
        String url = properties.get("url");
        return new Url(type, display, tech, url);
    }

    private static final class StreamJson {
        String slug;
        String display;
        boolean isTranslated;
        Stream.TYPE type;
        VideoSize videoSize;
        Map<String, Object> urls;
    }

}

请注意,我Url现在使用一个类而不是Format问题中提到的类。请在此处找到更多详细信息。如果您对如何改进有任何想法,欢迎在此处发表评论或在存储库上创建问题/拉取请求

于 2015-12-03T16:30:02.913 回答