1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public static class ExtensionsDictionaryClass
- {
- public static string ToJson<TKey, TValue>(this Dictionary<TKey, TValue> source)
- {
- return JsonUtility.ToJson(new ExtensionsDictionaryClass.SerializeDictionary<TKey, TValue>(source));
- }
- public static string ToJson<TKey, TValue>(this Dictionary<TKey, TValue> source, bool prettyPrint)
- {
- return JsonUtility.ToJson(new ExtensionsDictionaryClass.SerializeDictionary<TKey, TValue>(source), prettyPrint);
- }
- public static void FromJson<TKey, TValue>(this Dictionary<TKey, TValue> source, string jsonText)
- {
- ExtensionsDictionaryClass.SerializeDictionary<TKey, TValue> serializeDictionary = JsonUtility.FromJson<ExtensionsDictionaryClass.SerializeDictionary<TKey, TValue>>(jsonText);
- source.Clear();
- if (serializeDictionary.keys != null)
- {
- for (int i = 0; i < serializeDictionary.keys.Length; i++)
- {
- source.Add(serializeDictionary.keys[i], serializeDictionary.values[i]);
- }
- }
- }
- [Serializable]
- public class SerializeDictionary<Tkey, TVal>
- {
- public SerializeDictionary(Dictionary<Tkey, TVal> src)
- {
- if (src == null)
- {
- this.keys = new Tkey[0];
- this.values = new TVal[0];
- return;
- }
- this.keys = new Tkey[src.Count];
- this.values = new TVal[src.Count];
- int num = 0;
- foreach (KeyValuePair<Tkey, TVal> keyValuePair in src)
- {
- this.keys[num] = keyValuePair.Key;
- this.values[num] = keyValuePair.Value;
- num++;
- }
- }
- public Tkey[] keys;
- public TVal[] values;
- }
- }
|