104 lines
2.0 KiB
C#
104 lines
2.0 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using UnityEngine;
|
|
|
|
public static class JsonHelper
|
|
{
|
|
[Serializable]
|
|
private class Wrapper<T>
|
|
{
|
|
public T[] Items;
|
|
}
|
|
|
|
public static T[] FromJson<T>(string json)
|
|
{
|
|
return JsonUtility.FromJson<Wrapper<T>>(json).Items;
|
|
}
|
|
|
|
public static string ToJson<T>(T[] array)
|
|
{
|
|
return JsonUtility.ToJson(new Wrapper<T>
|
|
{
|
|
Items = array
|
|
});
|
|
}
|
|
|
|
public static string ToJson<T>(T[] array, bool prettyPrint)
|
|
{
|
|
return JsonUtility.ToJson(new Wrapper<T>
|
|
{
|
|
Items = array
|
|
}, prettyPrint);
|
|
}
|
|
|
|
public static string fixJson(string value)
|
|
{
|
|
value = "{\"Items\":" + value + "}";
|
|
return value;
|
|
}
|
|
|
|
public static string fixArrayJson(string value)
|
|
{
|
|
value = "{\"Items\":[" + value + "]}";
|
|
return value;
|
|
}
|
|
|
|
public static int JsonToInt(string target, string s)
|
|
{
|
|
return int.Parse(Regex.Split(target, s)[1]);
|
|
}
|
|
|
|
public static float JsonToFloat(string target, string s)
|
|
{
|
|
return float.Parse(Regex.Split(target, s)[1]);
|
|
}
|
|
|
|
public static string JsonToString(string target, string s)
|
|
{
|
|
return Regex.Split(target, s)[1];
|
|
}
|
|
|
|
public static Vector3 JsonToVecter3(string target)
|
|
{
|
|
string[] array = Regex.Split(target, ",");
|
|
return new Vector3(float.Parse(array[0]), float.Parse(array[1]), float.Parse(array[2]));
|
|
}
|
|
|
|
public static Vector2 JsonToVecter2(string target)
|
|
{
|
|
string[] array = Regex.Split(target, ",");
|
|
return new Vector2(float.Parse(array[0]), float.Parse(array[1]));
|
|
}
|
|
|
|
public static string filterTheOutgoingString(string text)
|
|
{
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
for (int i = 0; i < text.Length; i++)
|
|
{
|
|
if (text[i] == '\\')
|
|
{
|
|
stringBuilder.Append('\\');
|
|
}
|
|
else if (text[i] == '"')
|
|
{
|
|
stringBuilder.Append("\\\"");
|
|
continue;
|
|
}
|
|
stringBuilder.Append(text[i]);
|
|
}
|
|
return stringBuilder.ToString();
|
|
}
|
|
|
|
public static string fixJsonString(string text)
|
|
{
|
|
if (text.Length == 0)
|
|
{
|
|
return "";
|
|
}
|
|
text = text.Remove(0, 1);
|
|
text = text.Remove(text.Length - 1, 1);
|
|
return text;
|
|
}
|
|
}
|