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(int i) { if (i >= Parameters.Length) throw new IndexOutOfArrayLengthException(); JArray ??= JArray.FromObject(Parameters); return JArray[i].ToObject(); } 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(JsonString); } public static JsonObject[] GetObjects(string JsonString) { List jsons = new(); JsonSerializer serializer = new(); JsonTextReader reader = new(new StringReader(JsonString)) { SupportMultipleContent = true }; while (true) { if (!reader.Read()) { break; } JsonObject json = serializer.Deserialize(reader); jsons.Add(json); } return jsons.ToArray(); } } }