milimoe b08d62d6f8
Fixed #15 (#17)
* Fix #15; Start to Rebuild Architecture.

* Fixed #15
2023-04-21 01:06:23 +08:00

68 lines
2.0 KiB
C#

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Milimoe.FunGame.Core.Library.Constant;
namespace Milimoe.FunGame.Core.Library.Common.Network
{
[Serializable]
public struct JsonObject
{
public SocketMessageType MessageType { get; } = SocketMessageType.Unknown;
public Guid Token { get; }
public object[] Parameters { get; }
public string JsonString { get; }
private JArray? JArray;
public JsonObject(SocketMessageType MessageType, Guid Token, params object[] Parameters)
{
this.MessageType = MessageType;
this.Token = Token;
this.Parameters = Parameters;
this.JsonString = JsonConvert.SerializeObject(this, Formatting.Indented);
}
public T? GetObject<T>(int i)
{
if (i >= Parameters.Length) throw new IndexOutOfArrayLengthException();
JArray ??= JArray.FromObject(Parameters);
return JArray[i].ToObject<T>();
}
public static string GetString(SocketMessageType MessageType, Guid Token, params object[] Parameters)
{
return new JsonObject(MessageType, Token, Parameters).JsonString;
}
public static JsonObject GetObject(string JsonString)
{
return JsonConvert.DeserializeObject<JsonObject>(JsonString);
}
public static JsonObject[] GetObjects(string JsonString)
{
List<JsonObject> jsons = new();
JsonSerializer serializer = new();
JsonTextReader reader = new(new StringReader(JsonString))
{
SupportMultipleContent = true
};
while (true)
{
if (!reader.Read())
{
break;
}
JsonObject json = serializer.Deserialize<JsonObject>(reader);
jsons.Add(json);
}
return jsons.ToArray();
}
}
}