Skip to content

Commit

Permalink
Reduce complexity in serializing to JSON
Browse files Browse the repository at this point in the history
Use the new mainstream library to serialize to JSON.
  • Loading branch information
Viir committed Apr 3, 2022
1 parent 769beef commit efa14f5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 32 deletions.
85 changes: 54 additions & 31 deletions implement/read-memory-64-bit/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace read_memory_64_bit;

class Program
{
static string AppVersionId => "2022-04-01";
static string AppVersionId => "2022-04-03";

static int Main(string[] args)
{
Expand Down Expand Up @@ -187,11 +187,7 @@ IImmutableList<UITreeNode> ReadUITrees() =>
uiTreePreparedForFile = uiTreePreparedForFile.WithOtherDictEntriesRemoved();
}

var uiTreeAsJson = Newtonsoft.Json.JsonConvert.SerializeObject(
uiTreePreparedForFile,
// Support popular JSON parsers: Wrap large integers in a string to work around limitations there. (https://discourse.elm-lang.org/t/how-to-parse-a-json-object/4977)
new IntegersToStringJsonConverter()
);
var uiTreeAsJson = SerializeMemoryReadingNodeToJson(uiTreePreparedForFile);

var fileContent = System.Text.Encoding.UTF8.GetBytes(uiTreeAsJson);

Expand Down Expand Up @@ -363,6 +359,19 @@ static ulong ParseULong(string asString)

return ulong.Parse(asString);
}

static public string SerializeMemoryReadingNodeToJson(object obj) =>
System.Text.Json.JsonSerializer.Serialize(
obj,
new System.Text.Json.JsonSerializerOptions
{
Converters =
{
// Support common JSON parsers: Wrap large integers in a string to work around limitations there. (https://discourse.elm-lang.org/t/how-to-parse-a-json-object/4977)
new Int64JsonConverter(),
new UInt64JsonConverter()
}
});
}

public class EveOnline64
Expand Down Expand Up @@ -798,9 +807,11 @@ struct LocalMemoryReadingTools
}

var entriesOfInterestJObject =
new Newtonsoft.Json.Linq.JObject(
new System.Text.Json.Nodes.JsonObject(
entriesOfInterest.Select(dictEntry =>
new Newtonsoft.Json.Linq.JProperty(dictEntry.key, Newtonsoft.Json.Linq.JToken.FromObject(dictEntry.value))));
new KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>
(dictEntry.key,
System.Text.Json.Nodes.JsonNode.Parse(Program.SerializeMemoryReadingNodeToJson(dictEntry.value)))));

return new UITreeNode.Bunch
{
Expand Down Expand Up @@ -1048,7 +1059,7 @@ object GetDictEntryValueRepresentation(ulong valueOjectAddress)
}

{
var _displayDictEntry = dictEntriesOfInterest.FirstOrDefault(c => c.key == "_display");
var _displayDictEntry = dictEntriesOfInterest.FirstOrDefault(entry => entry.key == "_display");

if (_displayDictEntry != null && (_displayDictEntry.value is bool displayAsBool))
if (!displayAsBool)
Expand All @@ -1062,7 +1073,7 @@ UITreeNode[] ReadChildren()

// https://github.com/Arcitectus/Sanderling/blob/b07769fb4283e401836d050870121780f5f37910/guide/image/2015-01.eve-online-python-ui-tree-structure.png

var childrenDictEntry = dictEntriesOfInterest.FirstOrDefault(c => c.key == "children");
var childrenDictEntry = dictEntriesOfInterest.FirstOrDefault(entry => entry.key == "children");

if (childrenDictEntry == null)
return null;
Expand Down Expand Up @@ -1136,13 +1147,15 @@ UITreeNode[] ReadChildren()

var dictEntriesOfInterestLessNoneType =
dictEntriesOfInterest
.Where(c => !(((object)c.value as UITreeNode.DictEntryValueGenericRepresentation)?.pythonObjectTypeName == "NoneType"))
.Where(entry => !((entry.value as UITreeNode.DictEntryValueGenericRepresentation)?.pythonObjectTypeName == "NoneType"))
.ToArray();

var dictEntriesOfInterestJObject =
new Newtonsoft.Json.Linq.JObject(
new System.Text.Json.Nodes.JsonObject(
dictEntriesOfInterestLessNoneType.Select(dictEntry =>
new Newtonsoft.Json.Linq.JProperty(dictEntry.key, Newtonsoft.Json.Linq.JToken.FromObject(dictEntry.value))));
new KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>
(dictEntry.key,
System.Text.Json.Nodes.JsonNode.Parse(Program.SerializeMemoryReadingNodeToJson(dictEntry.value)))));

return new UITreeNode
{
Expand Down Expand Up @@ -1412,7 +1425,7 @@ public class UITreeNode

public string pythonObjectTypeName { set; get; }

public Newtonsoft.Json.Linq.JObject dictEntriesOfInterest { set; get; }
public System.Text.Json.Nodes.JsonObject dictEntriesOfInterest { set; get; }

public string[] otherDictEntriesKeys { set; get; }

Expand All @@ -1433,7 +1446,7 @@ public class DictEntry

public class Bunch
{
public Newtonsoft.Json.Linq.JObject entriesOfInterest { set; get; }
public System.Text.Json.Nodes.JsonObject entriesOfInterest { set; get; }
}

public IEnumerable<UITreeNode> EnumerateSelfAndDescendants() =>
Expand Down Expand Up @@ -1528,22 +1541,32 @@ static public (IImmutableList<(ulong baseAddress, byte[] content)> memoryRegions
}
}

public class IntegersToStringJsonConverter : Newtonsoft.Json.JsonConverter
public class Int64JsonConverter : System.Text.Json.Serialization.JsonConverter<long>
{
public override bool CanRead => false;
public override bool CanWrite => true;
public override bool CanConvert(Type type) =>
type == typeof(int) || type == typeof(long) || type == typeof(uint) || type == typeof(ulong);

public override void WriteJson(
Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
writer.WriteValue(value.ToString());
}
public override long Read(
ref System.Text.Json.Utf8JsonReader reader,
Type typeToConvert,
System.Text.Json.JsonSerializerOptions options) =>
long.Parse(reader.GetString()!);

public override void Write(
System.Text.Json.Utf8JsonWriter writer,
long integer,
System.Text.Json.JsonSerializerOptions options) =>
writer.WriteStringValue(integer.ToString());
}

public override object ReadJson(
Newtonsoft.Json.JsonReader reader, Type type, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
throw new NotSupportedException();
}
public class UInt64JsonConverter : System.Text.Json.Serialization.JsonConverter<ulong>
{
public override ulong Read(
ref System.Text.Json.Utf8JsonReader reader,
Type typeToConvert,
System.Text.Json.JsonSerializerOptions options) =>
ulong.Parse(reader.GetString()!);

public override void Write(
System.Text.Json.Utf8JsonWriter writer,
ulong integer,
System.Text.Json.JsonSerializerOptions options) =>
writer.WriteStringValue(integer.ToString());
}
1 change: 0 additions & 1 deletion implement/read-memory-64-bit/read-memory-64-bit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<ItemGroup>
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
</ItemGroup>

Expand Down

0 comments on commit efa14f5

Please sign in to comment.