期望 GSON 中的字符串时如何处理布尔值? [英] How to handle a Boolean when expecting a String in GSON?

查看:20
本文介绍了期望 GSON 中的字符串时如何处理布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用第 3 方 REST API 进行开发.我正在为 API 返回的 JSON 对象创建对象.我正在关注他们的文档,其中说某些变量将是字符串.有时当没有String时,它会返回False.有时我期待一个 URL 并且我得到 False.

I am currently developing with a 3rd party REST API. I am creating objects for their JSON objects being returned by the API. I am following their documentation where it says certain variables will be Strings. Sometimes when there is no String, it will return False. Sometimes I'm expecting a URL and I get False.

我该如何处理?

这是有问题的 API https://developers.cannabisreports.com/docs/

This is the API in question https://developers.cannabisreports.com/docs/

应变对象示例:https://ghostbin.com/paste/kgeau在应变对象中,执行搜索时出现异常.一些搜索结果在注释掉的代码位中有布尔值而不是字符串/url.

Strain Object EXAMPLE: https://ghostbin.com/paste/kgeau In the Strain Object, I get an exception when performing a search. Some of the results of the search have booleans instead of strings/url in the bits of code that are commented out.

有时我会得到这个

"genetics": {
            "names": false,
            "ucpc": false,
            "link": false
        }

有时我可以得到这个

"genetics": {
            "names": "Blueberry x Haze",
            "ucpc": "W74AFGH22Z000000000000000 x 9XVU7WJQCD000000000000000",
            "link": "https:\/\/www.cannabisreports.com\/api\/v1.0\/strains\/9XVU7PTG2P000000000000000\/genetics"
        }

推荐答案

当您遇到设计不完善的 API 时遇到麻烦时,Gson 在反序列化由此类 API 生成的 JSON 文档时为您提供了一些灵活性.尽管您有多种方法来解决这个问题(例如 JsonDeserializer 等),但您最喜欢面临的是 false-as-null我似乎已经在这里看到的问题.您可以通过标记错误"字段来帮助 Gson:

When you're in trouble with not a very well designed API, Gson gives you some flexibility while deserializing JSON documents produced by such an API. Despite you have numerous ways to work around this issue (like JsonDeserializer, etc), you mostly like are faced with the false-as-null issue I seem to have seen here already. You can help Gson by marking "wrong" fields:

final class Envelope {

    final Genetics genetics = null;

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("genetics", genetics)
                .toString();
    }

}

final class Genetics {

    @JsonAdapter(FalseAsNullTypeAdapterFactory.class)
    final String names = null;

    @JsonAdapter(FalseAsNullTypeAdapterFactory.class)
    final String ucpc = null;

    @JsonAdapter(FalseAsNullTypeAdapterFactory.class)
    final URL link = null;

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("names", names)
                .add("ucpc", ucpc)
                .add("link", link)
                .toString();
    }

}

这就是类型适配器工厂的实现方式:

And this is how the type adapter factory implemented:

final class FalseAsNullTypeAdapterFactory
        implements TypeAdapterFactory {

    // No worries, Gson will instantiate it itself
    private FalseAsNullTypeAdapterFactory() {
    }

    @Override
    public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) {
        final TypeAdapter<T> delegateAdapter = gson.getAdapter(typeToken);
        return new TypeAdapter<T>() {
            @Override
            public void write(final JsonWriter out, final T value)
                    throws IOException {
                delegateAdapter.write(out, value);
            }

            @Override
            public T read(final JsonReader in)
                    throws IOException {
                // If the next token is a boolean
                if ( in.peek() == JsonToken.BOOLEAN ) {
                    // and it's true
                    if ( in.nextBoolean() ) {
                        // then just report an unexpected `true` literal
                        throw new MalformedJsonException("Expected a null value indicator as `false`. " + in);
                    }
                    // and it's false, then we assume it's a null
                    return null;
                }
                // Otherwise read the whole value as a usual
                return delegateAdapter.read(in);
            }
        };
    }

}

一旦您反序列化您在问题中提供的 JSON 文档,toString 映射可能会产生如下内容:

Once you deserialize the JSON documents you provided in the question, a toString-ed mapping might produce something like this:

Envelope{genetics=Genetics{names=null, ucpc=null, link=null}}  
Envelope{genetics=Genetics{names=Blueberry x Haze, ucpc=W74AFGH22Z000000000000000 x 9XVU7WJQCD000000000000000, link=https://www.cannabisreports.com/api/v1.0/strains/9XVU7PTG2P000000000000000/genetics}}  

这篇关于期望 GSON 中的字符串时如何处理布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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