-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use Concurrent Dictionaries and ignore case on them to avoid ToLower * remove single array allocation * allocate GetClosures differently * Use JsonReader for closures * add comment * use isdefined * more readonly with less allocations * sorts * fmt * use element type when making an array
- Loading branch information
1 parent
b14c8db
commit 42882cb
Showing
4 changed files
with
134 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/Speckle.Sdk/Serialisation/SerializationUtilities/ClosureParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using Speckle.Newtonsoft.Json; | ||
using Speckle.Sdk.Common; | ||
|
||
namespace Speckle.Sdk.Serialisation.SerializationUtilities; | ||
|
||
public static class ClosureParser | ||
{ | ||
public static IReadOnlyList<(string, int)> GetClosures(string rootObjectJson) | ||
{ | ||
try | ||
{ | ||
using JsonTextReader reader = new(new StringReader(rootObjectJson)); | ||
reader.Read(); | ||
while (reader.TokenType != JsonToken.EndObject) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonToken.StartObject: | ||
{ | ||
var closureList = ReadObject(reader); | ||
if (closureList?.Any() ?? false) | ||
{ | ||
closureList.Sort((a, b) => b.Item2.CompareTo(a.Item2)); | ||
return closureList; | ||
} | ||
return Array.Empty<(string, int)>(); | ||
} | ||
default: | ||
reader.Read(); | ||
reader.Skip(); | ||
break; | ||
} | ||
} | ||
} | ||
catch (Exception ex) when (!ex.IsFatal()) { } | ||
return Array.Empty<(string, int)>(); | ||
} | ||
|
||
private static List<(string, int)>? ReadObject(JsonTextReader reader) | ||
{ | ||
reader.Read(); | ||
while (reader.TokenType != JsonToken.EndObject) | ||
{ | ||
switch (reader.TokenType) | ||
{ | ||
case JsonToken.PropertyName: | ||
{ | ||
if (reader.Value as string == "__closure") | ||
{ | ||
reader.Read(); //goes to prop vale | ||
var closureList = ReadClosureList(reader); | ||
return closureList; | ||
} | ||
reader.Read(); //goes to prop vale | ||
reader.Skip(); | ||
reader.Read(); //goes to next | ||
} | ||
break; | ||
default: | ||
reader.Read(); | ||
reader.Skip(); | ||
reader.Read(); | ||
break; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private static List<(string, int)> ReadClosureList(JsonTextReader reader) | ||
{ | ||
List<(string, int)> closureList = new(); | ||
reader.Read(); //startobject | ||
while (reader.TokenType != JsonToken.EndObject) | ||
{ | ||
var childId = (reader.Value as string).NotNull(); // propertyName | ||
int childMinDepth = reader.ReadAsInt32().NotNull(); //propertyValue | ||
reader.Read(); | ||
closureList.Add((childId, childMinDepth)); | ||
} | ||
|
||
return closureList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters