栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java中使用Gson合并/扩展JSON对象

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java中使用Gson合并/扩展JSON对象

这是我第一次尝试编写自己的静态合并方法。随意戳孔。

import com.google.gson.JsonArray;import com.google.gson.JsonElement;import com.google.gson.JsonObject;import java.util.Map;public class GsonTools {    public static enum ConflictStrategy {        THROW_EXCEPTION, PREFER_FIRST_OBJ, PREFER_SECOND_OBJ, PREFER_NON_NULL;    }    public static class JsonObjectExtensionConflictException extends Exception {        public JsonObjectExtensionConflictException(String message) { super(message);        }    }    public static void extendJsonObject(JsonObject destinationObject, ConflictStrategy conflictResolutionStrategy, JsonObject ... objs)  throws JsonObjectExtensionConflictException {        for (JsonObject obj : objs) { extendJsonObject(destinationObject, obj, conflictResolutionStrategy);        }    }    private static void extendJsonObject(JsonObject leftObj, JsonObject rightObj, ConflictStrategy conflictStrategy)  throws JsonObjectExtensionConflictException {        for (Map.Entry<String, JsonElement> rightEntry : rightObj.entrySet()) { String rightKey = rightEntry.getKey(); JsonElement rightVal = rightEntry.getValue(); if (leftObj.has(rightKey)) {     //conflict          JsonElement leftVal = leftObj.get(rightKey);     if (leftVal.isJsonArray() && rightVal.isJsonArray()) {         JsonArray leftArr = leftVal.getAsJsonArray();         JsonArray rightArr = rightVal.getAsJsonArray();         //concat the arrays -- there cannot be a conflict in an array, it's just a collection of stuff         for (int i = 0; i < rightArr.size(); i++) {  leftArr.add(rightArr.get(i));         }     } else if (leftVal.isJsonObject() && rightVal.isJsonObject()) {         //recursive merging         extendJsonObject(leftVal.getAsJsonObject(), rightVal.getAsJsonObject(), conflictStrategy);     } else {//not both arrays or objects, normal merge with conflict resolution         handleMergeConflict(rightKey, leftObj, leftVal, rightVal, conflictStrategy);     } } else {//no conflict, add to the object     leftObj.add(rightKey, rightVal); }        }    }    private static void handleMergeConflict(String key, JsonObject leftObj, JsonElement leftVal, JsonElement rightVal, ConflictStrategy conflictStrategy)  throws JsonObjectExtensionConflictException {        { switch (conflictStrategy) {     case PREFER_FIRST_OBJ:         break;//do nothing, the right val gets thrown out     case PREFER_SECOND_OBJ:         leftObj.add(key, rightVal);//right side auto-wins, replace left val with its val         break;     case PREFER_NON_NULL:         //check if right side is not null, and left side is null, in which case we use the right val         if (leftVal.isJsonNull() && !rightVal.isJsonNull()) {  leftObj.add(key, rightVal);         }//else do nothing since either the left value is non-null or the right value is null         break;     case THROW_EXCEPTION:         throw new JsonObjectExtensionConflictException("Key " + key + " exists in both objects and the conflict resolution strategy is " + conflictStrategy);     default:         throw new UnsupportedOperationException("The conflict strategy " + conflictStrategy + " is unknown and cannot be processed"); }        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569111.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号