conversion to System.Text.Json

This commit is contained in:
Vincent Cloutier 2023-03-12 14:10:59 -04:00
parent 6dc006bc66
commit 8b5d03e0f1
23 changed files with 77 additions and 107 deletions

View file

@ -1,8 +1,8 @@
using System;
using System.IO;
using System.Text.Json;
using System.Runtime.CompilerServices;
using BirdsiteLive.Common.Settings;
using Newtonsoft.Json;
namespace BSLManager.Tools
{
@ -94,7 +94,7 @@ namespace BSLManager.Tools
if (!File.Exists(LocalFileName)) return null;
var jsonContent = File.ReadAllText(LocalFileName);
var content = JsonConvert.DeserializeObject<LocalSettingsData>(jsonContent);
var content = JsonSerializer.Deserialize<LocalSettingsData>(jsonContent);
return content;
}
catch (Exception)
@ -105,7 +105,7 @@ namespace BSLManager.Tools
private void SaveLocalSettings(LocalSettingsData data)
{
var jsonContent = JsonConvert.SerializeObject(data);
var jsonContent = JsonSerializer.Serialize(data);
File.WriteAllText(LocalFileName, jsonContent);
}
}

View file

@ -1,6 +1,7 @@
using System;
using BirdsiteLive.ActivityPub.Models;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using System.Text.Json;
namespace BirdsiteLive.ActivityPub
{
@ -10,22 +11,21 @@ namespace BirdsiteLive.ActivityPub
{
try
{
var activity = JsonConvert.DeserializeObject<Activity>(json);
var activity = JsonSerializer.Deserialize<Activity>(json);
switch (activity.type)
{
case "Follow":
return JsonConvert.DeserializeObject<ActivityFollow>(json);
return JsonSerializer.Deserialize<ActivityFollow>(json);
case "Undo":
var a = JsonConvert.DeserializeObject<ActivityUndo>(json);
var a = JsonSerializer.Deserialize<ActivityUndo>(json);
if(a.apObject.type == "Follow")
return JsonConvert.DeserializeObject<ActivityUndoFollow>(json);
return JsonSerializer.Deserialize<ActivityUndoFollow>(json);
break;
case "Delete":
return JsonConvert.DeserializeObject<ActivityDelete>(json);
return JsonSerializer.Deserialize<ActivityDelete>(json);
case "Accept":
var accept = JsonConvert.DeserializeObject<ActivityAccept>(json);
//var acceptType = JsonConvert.DeserializeObject<Activity>(accept.apObject);
switch ((accept.apObject as dynamic).type.ToString())
var accept = JsonSerializer.Deserialize<ActivityAccept>(json);
switch (accept.apObject.type)
{
case "Follow":
var acceptFollow = new ActivityAcceptFollow()
@ -34,13 +34,13 @@ namespace BirdsiteLive.ActivityPub
id = accept.id,
actor = accept.actor,
context = accept.context,
apObject = new ActivityFollow()
apObject = new NestedActivity()
{
id = (accept.apObject as dynamic).id?.ToString(),
type = (accept.apObject as dynamic).type?.ToString(),
actor = (accept.apObject as dynamic).actor?.ToString(),
context = (accept.apObject as dynamic).context?.ToString(),
apObject = (accept.apObject as dynamic).@object?.ToString()
id = accept.apObject.id,
type = accept.apObject.type,
actor = accept.apObject.actor,
context = accept.apObject.context,
apObject = accept.apObject.apObject,
}
};
return acceptFollow;
@ -58,7 +58,7 @@ namespace BirdsiteLive.ActivityPub
private class Ac : Activity
{
[JsonProperty("object")]
[JsonPropertyName("object")]
public Activity apObject { get; set; }
}
}

View file

@ -6,7 +6,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>

View file

@ -1,39 +0,0 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace BirdsiteLive.ActivityPub.Converters
{
public class ContextArrayConverter : JsonConverter
{
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var result = new List<string>();
var list = serializer.Deserialize<List<object>>(reader);
foreach (var l in list)
{
if (l is string s)
result.Add(s);
else
{
var str = JsonConvert.SerializeObject(l);
result.Add(str);
}
}
return result.ToArray();
}
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
}
}

View file

@ -1,17 +1,16 @@
using System.Text.Json.Serialization;
using Newtonsoft.Json;
namespace BirdsiteLive.ActivityPub
{
public class Activity
{
[JsonProperty("@context")]
[JsonPropertyName("@context")]
public object context { get; set; }
public string id { get; set; }
public string type { get; set; }
public string actor { get; set; }
//[JsonProperty("object")]
//[JsonPropertyName("object")]
//public string apObject { get; set; }
}
}

View file

@ -1,10 +1,10 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub
{
public class ActivityAccept : Activity
{
[JsonProperty("object")]
public object apObject { get; set; }
[JsonPropertyName("object")]
public NestedActivity apObject { get; set; }
}
}

View file

@ -1,10 +1,10 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub
{
public class ActivityAcceptFollow : Activity
{
[JsonProperty("object")]
public ActivityFollow apObject { get; set; }
[JsonPropertyName("object")]
public NestedActivity apObject { get; set; }
}
}

View file

@ -1,10 +1,10 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub
{
public class ActivityAcceptUndoFollow : Activity
{
[JsonProperty("object")]
[JsonPropertyName("object")]
public ActivityUndoFollow apObject { get; set; }
}
}

View file

@ -1,6 +1,5 @@
using System;
using BirdsiteLive.ActivityPub.Models;
using Newtonsoft.Json;
using BirdsiteLive.ActivityPub.Models;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub
{
@ -10,7 +9,7 @@ namespace BirdsiteLive.ActivityPub
public string[] to { get; set; }
public string[] cc { get; set; }
[JsonProperty("object")]
[JsonPropertyName("object")]
public Note apObject { get; set; }
}
}

View file

@ -1,10 +1,10 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub.Models
{
public class ActivityDelete : Activity
{
[JsonProperty("object")]
public object apObject { get; set; }
[JsonPropertyName("object")]
public string apObject { get; set; }
}
}

View file

@ -1,10 +1,10 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub
{
public class ActivityFollow : Activity
{
[JsonProperty("object")]
[JsonPropertyName("object")]
public string apObject { get; set; }
}
}

View file

@ -1,10 +1,10 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub
{
public class ActivityRejectFollow : Activity
{
[JsonProperty("object")]
[JsonPropertyName("object")]
public ActivityFollow apObject { get; set; }
}
}

View file

@ -1,10 +1,10 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub
{
public class ActivityUndo : Activity
{
[JsonProperty("object")]
[JsonPropertyName("object")]
public Activity apObject { get; set; }
}
}

View file

@ -1,10 +1,10 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub
{
public class ActivityUndoFollow : Activity
{
[JsonProperty("object")]
[JsonPropertyName("object")]
public ActivityFollow apObject { get; set; }
}
}

View file

@ -1,14 +1,12 @@
using System.Net;
using BirdsiteLive.ActivityPub.Converters;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub
{
public class Actor
{
//[JsonPropertyName("@context")]
[JsonProperty("@context")]
[JsonConverter(typeof(ContextArrayConverter))]
[JsonPropertyName("@context")]
public string[] context { get; set; } = new[] { "https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1" };
public string id { get; set; }
public string type { get; set; }

View file

@ -1,12 +1,11 @@
using BirdsiteLive.ActivityPub.Converters;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub.Models
{
public class Followers
{
[JsonProperty("@context")]
[JsonConverter(typeof(ContextArrayConverter))]
[JsonPropertyName("@context")]
public string context { get; set; } = "https://www.w3.org/ns/activitystreams";
public string id { get; set; }

View file

@ -0,0 +1,16 @@
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub
{
public class NestedActivity
{
[JsonPropertyName("@context")]
public object context { get; set; }
public string id { get; set; }
public string type { get; set; }
public string actor { get; set; }
[JsonPropertyName("object")]
public string apObject { get; set; }
}
}

View file

@ -1,12 +1,10 @@
using BirdsiteLive.ActivityPub.Converters;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
namespace BirdsiteLive.ActivityPub.Models
{
public class Note
{
[JsonProperty("@context")]
[JsonConverter(typeof(ContextArrayConverter))]
[JsonPropertyName("@context")]
public string[] context { get; set; } = new[] { "https://www.w3.org/ns/activitystreams" };
public string id { get; set; }

View file

@ -4,13 +4,14 @@ using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using BirdsiteLive.ActivityPub;
using BirdsiteLive.ActivityPub.Converters;
using BirdsiteLive.ActivityPub.Models;
using BirdsiteLive.Common.Settings;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace BirdsiteLive.Domain
{
@ -52,7 +53,7 @@ namespace BirdsiteLive.Domain
var content = await result.Content.ReadAsStringAsync();
var actor = JsonConvert.DeserializeObject<Actor>(content);
var actor = JsonSerializer.Deserialize<Actor>(content);
if (string.IsNullOrWhiteSpace(actor.url)) actor.url = objectId;
return actor;
}
@ -78,7 +79,7 @@ namespace BirdsiteLive.Domain
if (!string.IsNullOrWhiteSpace(inbox))
usedInbox = inbox;
var json = JsonConvert.SerializeObject(data, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
var json = JsonSerializer.Serialize(data, new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
var date = DateTime.UtcNow.ToUniversalTime();
var httpDate = date.ToString("r");

View file

@ -188,7 +188,7 @@ namespace BirdsiteLive.Domain
id = $"{activity.apObject}#accepts/follows/{Guid.NewGuid()}",
type = "Accept",
actor = activity.apObject,
apObject = new ActivityFollow()
apObject = new NestedActivity()
{
id = activity.id,
type = activity.type,

View file

@ -10,7 +10,6 @@ using BirdsiteLive.Common.Settings;
using BirdsiteLive.Domain;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Newtonsoft.Json;
namespace BirdsiteLive.Controllers
{

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Net.Mime;
using System.Text.RegularExpressions;
using System.Threading;
@ -19,7 +20,6 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
namespace BirdsiteLive.Controllers
{
@ -111,7 +111,7 @@ namespace BirdsiteLive.Controllers
if (isSaturated) return new ObjectResult("Too Many Requests") { StatusCode = 429 };
if (notFound) return NotFound();
var apUser = _userService.GetUser(user);
var jsonApUser = JsonConvert.SerializeObject(apUser);
var jsonApUser = System.Text.Json.JsonSerializer.Serialize(apUser);
return Content(jsonApUser, "application/activity+json; charset=utf-8");
}
}
@ -155,7 +155,7 @@ namespace BirdsiteLive.Controllers
if (r.Contains("application/activity+json"))
{
var jsonApUser = JsonConvert.SerializeObject(status);
var jsonApUser = JsonSerializer.Serialize(status);
return Content(jsonApUser, "application/activity+json; charset=utf-8");
}
}
@ -185,7 +185,7 @@ namespace BirdsiteLive.Controllers
var status = _statusService.GetActivity(id, tweet);
var jsonApUser = JsonConvert.SerializeObject(status);
var jsonApUser = JsonSerializer.Serialize(status);
return Content(jsonApUser, "application/activity+json; charset=utf-8");
}
@ -269,7 +269,7 @@ namespace BirdsiteLive.Controllers
{
id = $"https://{_instanceSettings.Domain}/users/{id}/followers"
};
var jsonApUser = JsonConvert.SerializeObject(followers);
var jsonApUser = JsonSerializer.Serialize(followers);
return Content(jsonApUser, "application/activity+json; charset=utf-8");
}
}

View file

@ -19,7 +19,8 @@
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"ASPNETCORE_ENVIRONMENT": "Development",
"Instance__ParallelTwitterRequests": "0"
},
"applicationUrl": "http://localhost:5000"
},