Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Scoop support #402

Merged
merged 8 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
50 changes: 50 additions & 0 deletions source/UninstallTools/Factory/Json/DynamicStringArrayConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace UninstallTools.Factory.Json
{
/// <summary>
/// Handle JSON string array entry that has one dimension less or more.
/// </summary>
internal class DynamicStringArrayConverter : JsonConverter<string[]>
{
public override string[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// 0-dimension
if (reader.TokenType == JsonTokenType.String)
{
return new[] { reader.GetString() };
}
else if (reader.TokenType == JsonTokenType.StartArray)
{
List<string> results = new();
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
// nested
if (reader.TokenType == JsonTokenType.StartArray)
{
var clone = reader;
_ = clone.Read();
results.Add(clone.GetString()); // take first value of nested array only
reader.Skip();
}
// normal
else if (reader.TokenType == JsonTokenType.String)
{
results.Add(reader.GetString());
}
}
return results.ToArray();
}

throw new JsonException();
}

public override void Write(Utf8JsonWriter writer, string[] value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace UninstallTools.Factory.Json
{
internal class PowerShellDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
{
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
var value = reader.GetString();
// Windows PowerShell: /Date(1640995200000)/
if (value.StartsWith("/", StringComparison.Ordinal))
{
var timestamp = long.Parse(value.Substring(6, value.Length - 8));
return DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
}
// PowerShell Core: 2022-01-01T00:00:00.0000000+00:00
else
{
return DateTimeOffset.Parse(value);
}
}
// Windows PowerShell: nested { value: /Date(1640995200000)/ }
else if (reader.TokenType == JsonTokenType.StartObject)
{
var clone = reader;
reader.Skip();
while (clone.Read() && clone.TokenType != JsonTokenType.EndObject)
{
if (clone.TokenType == JsonTokenType.PropertyName && clone.GetString() == "value")
{
_ = clone.Read();
var value = clone.GetString();
var timestamp = long.Parse(value.Substring(6, value.Length - 8));
return DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
}
}
}

throw new JsonException();
}

public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
}
Loading