107 lines
2.4 KiB
C#
107 lines
2.4 KiB
C#
|
|
/************************
|
|||
|
|
* Json<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> *
|
|||
|
|
************************/
|
|||
|
|
using UnityEngine;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System;
|
|||
|
|
|
|||
|
|
#region
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>װ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>л<EFBFBD><D0BB>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
[Serializable]
|
|||
|
|
public class Serialization<T>
|
|||
|
|
{
|
|||
|
|
[SerializeField]
|
|||
|
|
List<T> target;
|
|||
|
|
public List<T> ToList() { return target; }
|
|||
|
|
|
|||
|
|
public Serialization(List<T> target)
|
|||
|
|
{
|
|||
|
|
this.target = target;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>װ<EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>л<EFBFBD><D0BB>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
[Serializable]
|
|||
|
|
public class Serialization<TKey, TValue> : ISerializationCallbackReceiver
|
|||
|
|
{
|
|||
|
|
[SerializeField]
|
|||
|
|
List<TKey> keys;
|
|||
|
|
[SerializeField]
|
|||
|
|
List<TValue> values;
|
|||
|
|
|
|||
|
|
Dictionary<TKey, TValue> target;
|
|||
|
|
public Dictionary<TKey, TValue> ToDictionary() { return target; }
|
|||
|
|
|
|||
|
|
public Serialization(Dictionary<TKey, TValue> target)
|
|||
|
|
{
|
|||
|
|
this.target = target;
|
|||
|
|
}
|
|||
|
|
public void OnBeforeSerialize()
|
|||
|
|
{
|
|||
|
|
keys = new List<TKey>(target.Keys);
|
|||
|
|
values = new List<TValue>(target.Values);
|
|||
|
|
}
|
|||
|
|
public void OnAfterDeserialize()
|
|||
|
|
{
|
|||
|
|
var count = Math.Min(keys.Count, values.Count);
|
|||
|
|
target = new Dictionary<TKey, TValue>(count);
|
|||
|
|
for (var i = 0; i < count; ++i)
|
|||
|
|
{
|
|||
|
|
target.Add(keys[i], values[i]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// JSON<4F><4E><EFBFBD>ݹ<EFBFBD><DDB9><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public class JsonTools
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>תJSON<4F>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static string ListToJson<T>(List<T> l)
|
|||
|
|
{
|
|||
|
|
return JsonUtility.ToJson(new Serialization<T>(l));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// JSON<4F>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static List<T> ListFromJson<T>(string str)
|
|||
|
|
{
|
|||
|
|
return JsonUtility.FromJson<Serialization<T>>(str).ToList();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>תJSON<4F>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static string DicToJson<TKey, TValue>(Dictionary<TKey, TValue> dic)
|
|||
|
|
{
|
|||
|
|
return JsonUtility.ToJson(new Serialization<TKey, TValue>(dic));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// JSON<4F>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static Dictionary<TKey, TValue> DicFromJson<TKey, TValue>(string str)
|
|||
|
|
{
|
|||
|
|
return JsonUtility.FromJson<Serialization<TKey, TValue>>(str).ToDictionary();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// JSON<4F>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public static T ObjectFromJson<T>(string str)
|
|||
|
|
{
|
|||
|
|
return JsonUtility.FromJson<T>(str);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|