Skip to content

Commit ff44e7e

Browse files
scorteanucosminMrBlue
authored andcommitted
Update Serializer, moved from Oxide.Common
1 parent d36c5db commit ff44e7e

4 files changed

Lines changed: 44 additions & 7 deletions

File tree

src/Common/Constants.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using System.Text.RegularExpressions;
2-
using Oxide.CompilerServices;
32

43
namespace Oxide.CSharp.Common
54
{
65
internal static class Constants
76
{
8-
//TODO: Move to Oxide.Common & optimize this is a temporary solution
9-
internal static readonly ISerializer Serializer = new Serializer();
7+
internal static readonly Serializer Serializer = new();
108

119
internal const string CompilerDownloadUrl = "https://downloads.oxidemod.com/artifacts/Oxide.Compiler/{0}/";
1210
internal const string CompilerBasicArguments = "-unsafe true --setting:Force true -ms true";

src/CompilerService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Oxide.Core.Logging;
55
using Oxide.Logging;
66
using Oxide.Plugins;
7-
using Oxide.Pooling;
87
using References::Mono.Unix.Native;
98
using System;
109
using System.Collections.Generic;
@@ -16,7 +15,6 @@
1615
using System.Net.Cache;
1716
using System.Security.Cryptography;
1817
using System.Text;
19-
using System.Text.RegularExpressions;
2018
using System.Threading;
2119
using Oxide.CompilerServices;
2220
using Oxide.Core.Extensions;

src/Patching/Publicizer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using System.IO;
44
using Oxide.CSharp.Patching.Validation;
55
using References::Mono.Cecil;
6-
using System;
7-
using System.IO;
86

97
namespace Oxide.CSharp.Patching
108
{

src/Serializer.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
extern alias References;
2+
using System.IO;
3+
using System.Text;
4+
using References::Newtonsoft.Json;
5+
6+
namespace Oxide.CSharp;
7+
8+
public class Serializer
9+
{
10+
private readonly JsonSerializer _jsonSerializer;
11+
private readonly UTF8Encoding _encoding;
12+
13+
public Serializer()
14+
{
15+
_jsonSerializer = new JsonSerializer();
16+
_encoding = new UTF8Encoding(false);
17+
}
18+
19+
public void SerializeToStream<T>(MemoryStream memoryStream, T type, int bufferSize) where T : class
20+
{
21+
using StreamWriter streamWriter = new(memoryStream, _encoding, bufferSize, leaveOpen: true);
22+
23+
_jsonSerializer.Serialize(streamWriter, type);
24+
25+
streamWriter.Flush();
26+
}
27+
28+
public byte[] Serialize<T>(T type) where T : class
29+
{
30+
using MemoryStream memoryStream = new();
31+
using StreamWriter streamWriter = new(memoryStream, _encoding);
32+
_jsonSerializer.Serialize(streamWriter, type);
33+
streamWriter.Flush();
34+
return memoryStream.ToArray();
35+
}
36+
37+
public T Deserialize<T>(byte[] data) where T : class
38+
{
39+
using MemoryStream memoryStream = new(data);
40+
using StreamReader streamReader = new(memoryStream, _encoding);
41+
return (T)_jsonSerializer.Deserialize(streamReader, typeof(T));
42+
}
43+
}

0 commit comments

Comments
 (0)