Skip to content

Commit

Permalink
change ycs to use system.text.json instead of newtonsoft json
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Nov 13, 2024
1 parent 1a41180 commit 0e33804
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
22 changes: 13 additions & 9 deletions src/Ycs/Structs/ContentJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@
// </copyright>
// ------------------------------------------------------------------------------

using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Ycs
{
public class ContentJson : IContentEx
{
internal const int _ref = 2;

private readonly List<object> _content;
private readonly List<JsonNode> _content;

internal ContentJson(IEnumerable<object> data)
internal ContentJson(IEnumerable<JsonNode> data)
{
_content = new List<object>(data);
_content = new List<JsonNode>(data);
}

private ContentJson(List<object> other)
private ContentJson(List<JsonNode> other)
{
_content = other;
}
Expand All @@ -29,7 +33,7 @@ private ContentJson(List<object> other)
public bool Countable => true;
public int Length => _content?.Count ?? 0;

public IReadOnlyList<object> GetContent() => _content.AsReadOnly();
public IReadOnlyList<object> GetContent() => new ReadOnlyCollection<object>(_content.OfType<object>().ToList());

public IContent Copy() => new ContentJson(_content);

Expand Down Expand Up @@ -68,22 +72,22 @@ void IContentEx.Write(IUpdateEncoder encoder, int offset)
encoder.WriteLength(len);
for (int i = offset; i < len; i++)
{
var jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(_content[i]);
var jsonStr = JsonSerializer.Serialize(_content[i]);
encoder.WriteString(jsonStr);
}
}

internal static ContentJson Read(IUpdateDecoder decoder)
{
var len = decoder.ReadLength();
var content = new List<object>(len);
var content = new List<JsonNode>(len);

for (int i = 0; i < len; i++)
{
var jsonStr = decoder.ReadString();
object jsonObj = string.Equals(jsonStr, "undefined")
JsonNode jsonObj = string.Equals(jsonStr, "undefined")
? null
: Newtonsoft.Json.JsonConvert.DeserializeObject(jsonStr);
: JsonSerializer.Deserialize<JsonNode>(jsonStr);
content.Add(jsonObj);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Ycs/Utils/UpdateDecoderV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// ------------------------------------------------------------------------------

using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace Ycs
{
Expand Down Expand Up @@ -197,7 +199,7 @@ public object ReadJson()
CheckDisposed();

var jsonString = Reader.ReadVarString();
var result = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);
var result = JsonSerializer.Deserialize<JsonNode>(jsonString);
return result;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Ycs/Utils/UpdateEncoderV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// ------------------------------------------------------------------------------

using System.Diagnostics;
using System.Text.Json;

namespace Ycs
{
Expand Down Expand Up @@ -201,7 +202,7 @@ public void WriteKey(string key)

public void WriteJson<T>(T any)
{
var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(any, typeof(T), null);
var jsonString = JsonSerializer.Serialize(any);
RestWriter.WriteVarString(jsonString);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Ycs/Ycs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Text.Json" />
</ItemGroup>

</Project>

0 comments on commit 0e33804

Please sign in to comment.