public static void TryMergeJsonInto(string jsonIn, ref string jsonOut) { var jsonHash = jsonIn.GetHashCode(); var jsonCopy = jsonOut; if (!JsonHashToId.ContainsKey(jsonHash)) { return; } var id = JsonHashToId[jsonHash]; if (!JsonMerges.ContainsKey(id)) { return; } try { var ontoJObj = JObject.Parse(jsonCopy); foreach (var jsonMerge in JsonMerges[id]) { var inJObj = JObject.Parse(jsonMerge); ontoJObj.Merge(inJObj); } jsonOut = ontoJObj.ToString(); } catch (JsonReaderException e) { Log($"Error merging JSON ${e}"); } }
internal static void TryMergeIntoInterceptedJson(string jsonIn, ref string jsonOut) { var jsonHash = jsonIn.GetHashCode(); var jsonCopy = jsonOut; if (!JsonHashToId.ContainsKey(jsonHash)) { return; } var id = JsonHashToId[jsonHash]; if (!JsonMerges.ContainsKey(id)) { return; } LogWithDate($"Merging json into ID: {id}"); JObject ontoJObj; try { ontoJObj = JObject.Parse(jsonCopy); } catch (Exception e) { try { Log("\tParent JSON has an JSON parse error, attempting to fix missing commas with regex"); jsonCopy = FixMissingCommas(jsonCopy); ontoJObj = JObject.Parse(jsonCopy); } catch (Exception e2) { Log("\tParent JSON has an error preventing merges that couldn't be fixed with missing comma regex"); Log($"\t\t Exception before regex: {e.Message}"); Log($"\t\t Exception after regex: {e2.Message}"); return; } Log("\tFixed missing commas in parent JSON."); } foreach (var jsonMerge in JsonMerges[id]) { try { var inJObj = JObject.Parse(jsonMerge); ontoJObj.Merge(inJObj, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Replace }); } catch (Exception e) { Log($"\tError merging particular JSON merge onto {id}, skipping just this single merge"); Log($"\t\t{e.Message}"); } } jsonOut = ontoJObj.ToString(); }