Skip to content

Commit cee5f06

Browse files
committed
Updated handling of additional context removal
With the root context no longer being at the start of the JSON, it needs to be handled differently. Because properties are now tied to inheritance and `Thing` has the `@Context` property, we can instead use `LastIndexOf` as the way to determine where the root context information is. We use this value as the length from the start to narrow the search-and-replace space.
1 parent 15a8c94 commit cee5f06

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Source/Schema.NET/Thing.Partial.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ static Thing()
6969
/// A <see cref="string" /> that represents the JSON-LD representation of this instance.
7070
/// </returns>
7171
public string ToString(JsonSerializerOptions serializerSettings) =>
72-
RemoveAllButFirstContext(JsonSerializer.Serialize(this, serializerSettings));
72+
RemoveAllButRootContext(JsonSerializer.Serialize(this, serializerSettings));
7373

74-
private static string RemoveAllButFirstContext(string json)
74+
private static string RemoveAllButRootContext(string json)
7575
{
7676
var stringBuilder = new StringBuilder(json);
77-
var startIndex = ContextPropertyJson.Length + 1; // We add the one to represent the opening curly brace.
78-
stringBuilder.Replace(ContextPropertyJson, string.Empty, startIndex, stringBuilder.Length - startIndex);
77+
var lastIndex = json.LastIndexOf(ContextPropertyJson, StringComparison.OrdinalIgnoreCase);
78+
stringBuilder.Replace(ContextPropertyJson, string.Empty, 0, lastIndex);
7979
return stringBuilder.ToString();
8080
}
8181
}

0 commit comments

Comments
 (0)