use of com.devonfw.cobigen.jsonplugin.merger.generic.GenericJSONMerger in project cobigen by devonfw.
the class JSONMerger method merge.
@Override
public String merge(File base, String patch, String targetCharset) throws MergeException {
String file = base.getAbsolutePath();
JsonObject objBase = null;
JsonObject objPatch = null;
try (InputStream in = Files.newInputStream(base.toPath());
InputStreamReader inSR = new InputStreamReader(in, Charset.forName(targetCharset));
JsonReader reader = new JsonReader(inSR)) {
JsonParser parser = new JsonParser();
JsonElement jsonBase = parser.parse(reader);
objBase = jsonBase.getAsJsonObject();
} catch (JsonIOException e) {
throw new MergeException(base, "Not JSON file", e);
} catch (JsonSyntaxException e) {
throw new MergeException(base, "JSON syntax error. ", e);
} catch (FileNotFoundException e) {
throw new MergeException(base, "File not found", e);
} catch (IOException e) {
throw new MergeException(base, "Could not read " + file, e);
}
try {
JsonParser parser = new JsonParser();
JsonElement jsonPatch = parser.parse(patch);
objPatch = jsonPatch.getAsJsonObject();
} catch (JsonIOException e) {
throw new MergeException(base, "Not JSON patch code", e);
} catch (JsonSyntaxException e) {
throw new MergeException(base, "JSON Patch syntax error. ", e);
}
JsonObject result = null;
// Override would be defined by patchOverrides at PluginActivator
if (this.type.contains(Constants.GENERIC_MERGE)) {
GenericJSONMerger ng2merge = new GenericJSONMerger(objBase, objPatch);
result = ng2merge.merge(this.patchOverrides);
} else {
throw new MergeException(base, "Merge strategy not yet supported!");
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(result);
}
Aggregations