【问题标题】:Get Gson deserialization to Pojo to fail if pojo does not contain field specified in json如果 pojo 不包含 json 中指定的字段,则获取对 Pojo 的 Gson 反序列化失败
【发布时间】:2016-07-19 04:01:03
【问题描述】:

给定以下 json 文件:

{
    "validField":"I'm here",
    "invalidField":"I'm not here :-(",
}

和波乔

public class Pojo {
    public String validField;
}

当我使用 gson 将 json 反序列化为 Pojo 时,我希望它在 invalidField 上失败,因为它在 Pojo 中不存在。

// This should fail but just ignores 'invalidField' property
Pojo pojo = new Gson().fromJson(json, Pojo.class);

有什么想法可以实现吗?

【问题讨论】:

    标签: gson


    【解决方案1】:

    由于缺乏替代方案,我想出了以下方法。我仍然很想知道是否有支持的解决方案。

    private static void validateAllDataLoaded(JsonElement element, Object returnType, String entryName) throws IOException {
    
        if (element.isJsonNull()) {
            return;
        }
    
        if (element.isJsonArray()) {
            checkFieldExists(entryName, returnType);
    
            for (Object arrayItem : (ArrayList<?>) returnType) {
                for (JsonElement item : element.getAsJsonArray()) {
                    validateAllDataLoaded(item, arrayItem, entryName);
                }
            }
        }
    
        if (element.isJsonObject()) {
            checkFieldExists(entryName, returnType);
    
            for (Map.Entry<String, JsonElement> entry : element.getAsJsonObject().entrySet()) {
                if (!entry.getValue().isJsonNull() && !entry.getValue().isJsonPrimitive()) {
                    validateAllDataLoaded(entry.getValue(), getField(entry.getKey(), returnType), "");
                } else {
                    validateAllDataLoaded(entry.getValue(), returnType, entry.getKey());
                }
            }
        }
    
        if (element.isJsonPrimitive()) {
            checkFieldExists(entryName, returnType);
        }
    }
    
    private static void checkFieldExists(String entryName, Object returnType) throws IOException {
        if (entryName.isEmpty()) {
            return;
        }
    
        Field[] fields = returnType.getClass().getDeclaredFields();
        boolean exists = Arrays.asList(fields).stream().filter(f -> f.getName().equals(entryName)).count() == 1;
    
        if (!exists) {
            throw new IOException("JSON element '" + entryName + "' is not a field in the destination class " + returnType.getClass().getName());
        }
    }
    
    private static Object getField(String entryName, Object returnType) throws IOException {
        if (entryName.isEmpty()) {
            return null;
        }
    
        Field field;
        try {
            field = returnType.getClass().getDeclaredField(entryName);
            field.setAccessible(true);
    
            return field.get(returnType);
        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
            throw new IOException(e);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      相关资源
      最近更新 更多