Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion Test_Engine/Compute/TryToJsonAndFromJsonAndCheckIfEqual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public static Output<List<string>, List<object>, List<string>, List<object>, Lis

if (isEqual)
{
successfulJson.Add(json);
// Added here, after the checks, so they run on the json as serialised.
successfulJson.Add(AddDeclaringAssembly(json, obj));
successfulJsonObjects.Add(obj);
}
else
Expand All @@ -137,6 +138,50 @@ public static Output<List<string>, List<object>, List<string>, List<object>, Lis
Item8 = notEqualObjects
};
}

/***************************************************/
/**** Private Methods ****/
/***************************************************/

[Description("Records the assembly declaring the object's type on the record, as an `_asm` field.")]
[Input("json", "Serialised record to add the field to.")]
[Input("obj", "Object the record was serialised from.")]
[Output("json", "The record, with the declaring assembly recorded on it.")]
private static string AddDeclaringAssembly(string json, object obj)
{
if (string.IsNullOrEmpty(json) || obj == null)
return json;

// A method record already carries its declaring assembly, because its declaring type is
// serialised assembly-qualified. Its top-level type is MethodBase, so recording the
// assembly of that would be untrue.
if (obj is System.Reflection.MethodBase)
return json;

string assembly;
try
{
assembly = obj.GetType().Assembly.GetName().Name;
}
catch
{
// Left as it was. A guessed value would be indistinguishable from a real one.
return json;
}

if (string.IsNullOrWhiteSpace(assembly))
return json;

// Before `_bhomVersion`, which Versioning_Engine appends last. Helpers.DescriptionFromJson
// reads fixed quote-delimited indices, so the field has to sit past them.
int at = json.LastIndexOf("\"_bhomVersion\"", StringComparison.Ordinal);
if (at < 0)
return json;

return json.Substring(0, at) + $"\"_asm\" : \"{assembly}\", " + json.Substring(at);
}

/***************************************************/
}
}

Expand Down
Loading