JsonUtility源码
时间: 2023-07-05 08:10:48 浏览: 159
json源代码
JsonUtility是Unity内置的用于序列化和反序列化JSON数据的工具类,以下是它的部分源码:
```c#
public static class JsonUtility
{
// 将一个对象序列化为JSON格式的字符串
public static string ToJson(object obj, bool prettyPrint = false)
{
if (obj == null)
return "null";
var jsonWriter = new JsonWriter { prettyPrint = prettyPrint };
SerializeValue(obj, jsonWriter);
return jsonWriter.ToString();
}
// 将一个JSON格式的字符串反序列化为指定类型的对象
public static T FromJson<T>(string json)
{
if (string.IsNullOrEmpty(json))
return default(T);
var reader = new JsonReader(json);
return (T)DeserializeValue(reader, typeof(T));
}
// 序列化对象
private static void SerializeValue(object value, JsonWriter writer)
{
if (value == null)
{
writer.Write(null);
}
else if (value is bool)
{
writer.Write((bool)value);
}
else if (value is string)
{
writer.Write((string)value);
}
else if (value is char)
{
writer.Write((char)value);
}
else if (value is Enum)
{
writer.Write((int)value);
}
else if (value is int)
{
writer.Write((int)value);
}
else if (value is uint)
{
writer.Write((uint)value);
}
else if (value is long)
{
writer.Write((long)value);
}
else if (value is ulong)
{
writer.Write((ulong)value);
}
else if (value is float)
{
writer.Write((float)value);
}
else if (value is double)
{
writer.Write((double)value);
}
else if (value is decimal)
{
writer.Write((decimal)value);
}
else if (value is Array)
{
SerializeArray((Array)value, writer);
}
else if (value is IList)
{
SerializeList((IList)value, writer);
}
else if (value is IDictionary)
{
SerializeDictionary((IDictionary)value, writer);
}
else if (value is UnityEngine.Object)
{
SerializeUnityObject((UnityEngine.Object)value, writer);
}
else
{
SerializeObject(value, writer);
}
}
// 反序列化对象
private static object DeserializeValue(JsonReader reader, Type type)
{
if (type == typeof(bool))
{
return reader.ReadBoolean();
}
else if (type == typeof(string))
{
return reader.ReadString();
}
else if (type == typeof(char))
{
return reader.ReadChar();
}
else if (type.IsEnum)
{
return Enum.ToObject(type, reader.ReadInt32());
}
else if (type == typeof(int))
{
return reader.ReadInt32();
}
else if (type == typeof(uint))
{
return reader.ReadUInt32();
}
else if (type == typeof(long))
{
return reader.ReadInt64();
}
else if (type == typeof(ulong))
{
return reader.ReadUInt64();
}
else if (type == typeof(float))
{
return reader.ReadSingle();
}
else if (type == typeof(double))
{
return reader.ReadDouble();
}
else if (type == typeof(decimal))
{
return reader.ReadDecimal();
}
else if (type.IsArray)
{
return DeserializeArray(reader, type.GetElementType());
}
else if (typeof(IList).IsAssignableFrom(type))
{
return DeserializeList(reader, type);
}
else if (typeof(IDictionary).IsAssignableFrom(type))
{
return DeserializeDictionary(reader, type);
}
else if (typeof(UnityEngine.Object).IsAssignableFrom(type))
{
return DeserializeUnityObject(reader, type);
}
else
{
return DeserializeObject(reader, type);
}
}
// 序列化数组
private static void SerializeArray(Array array, JsonWriter writer)
{
writer.WriteArrayStart();
for (int i = 0; i < array.Length; i++)
{
SerializeValue(array.GetValue(i), writer);
}
writer.WriteArrayEnd();
}
// 序列化列表
private static void SerializeList(IList list, JsonWriter writer)
{
writer.WriteArrayStart();
for (int i = 0; i < list.Count; i++)
{
SerializeValue(list[i], writer);
}
writer.WriteArrayEnd();
}
// 序列化字典
private static void SerializeDictionary(IDictionary dictionary, JsonWriter writer)
{
writer.WriteObjectStart();
foreach (DictionaryEntry entry in dictionary)
{
writer.WritePropertyName(entry.Key.ToString());
SerializeValue(entry.Value, writer);
}
writer.WriteObjectEnd();
}
// 序列化Unity对象
private static void SerializeUnityObject(UnityEngine.Object obj, JsonWriter writer)
{
writer.WriteObjectStart();
writer.WritePropertyName("instanceID");
writer.Write(obj.GetInstanceID());
writer.WriteObjectEnd();
}
// 序列化普通对象
private static void SerializeObject(object obj, JsonWriter writer)
{
writer.WriteObjectStart();
var fields = obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
foreach (var field in fields)
{
if (field.IsNotSerialized) continue;
writer.WritePropertyName(field.Name);
SerializeValue(field.GetValue(obj), writer);
}
writer.WriteObjectEnd();
}
// 反序列化数组
private static Array DeserializeArray(JsonReader reader, Type elementType)
{
var list = new List<object>();
reader.ReadArrayStart();
while (!reader.IsArrayEnd)
{
list.Add(DeserializeValue(reader, elementType));
}
reader.ReadArrayEnd();
var array = Array.CreateInstance(elementType, list.Count);
for (int i = 0; i < list.Count; i++)
{
array.SetValue(list[i], i);
}
return array;
}
// 反序列化列表
private static object DeserializeList(JsonReader reader, Type type)
{
var elementType = type.GetGenericArguments()[0];
var list = Activator.CreateInstance(type) as IList;
reader.ReadArrayStart();
while (!reader.IsArrayEnd)
{
list.Add(DeserializeValue(reader, elementType));
}
reader.ReadArrayEnd();
return list;
}
// 反序列化字典
private static object DeserializeDictionary(JsonReader reader, Type type)
{
var keyType = type.GetGenericArguments()[0];
var valueType = type.GetGenericArguments()[1];
var dictionary = Activator.CreateInstance(type) as IDictionary;
reader.ReadObjectStart();
while (!reader.IsObjectEnd)
{
var key = DeserializeValue(reader, keyType);
var value = DeserializeValue(reader, valueType);
dictionary.Add(key, value);
}
reader.ReadObjectEnd();
return dictionary;
}
// 反序列化Unity对象
private static UnityEngine.Object DeserializeUnityObject(JsonReader reader, Type type)
{
var instanceID = 0;
reader.ReadObjectStart();
while (!reader.IsObjectEnd)
{
var propertyName = reader.ReadPropertyName();
if (propertyName == "instanceID")
{
instanceID = reader.ReadInt32();
}
else
{
reader.SkipValue();
}
}
reader.ReadObjectEnd();
return UnityEngine.Object.FindObjectFromInstanceID(instanceID);
}
// 反序列化普通对象
private static object DeserializeObject(JsonReader reader, Type type)
{
var obj = Activator.CreateInstance(type);
reader.ReadObjectStart();
while (!reader.IsObjectEnd)
{
var propertyName = reader.ReadPropertyName();
var field = type.GetField(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (field != null)
{
field.SetValue(obj, DeserializeValue(reader, field.FieldType));
}
else
{
reader.SkipValue();
}
}
reader.ReadObjectEnd();
return obj;
}
}
```
以上是JsonUtility的部分源码,它主要包含了ToJson和FromJson两个方法,分别用于将一个对象序列化为JSON格式的字符串和将一个JSON格式的字符串反序列化为指定类型的对象。在序列化和反序列化时,JsonUtility会根据对象的类型选择不同的处理方式。例如,如果对象是一个数组,则会调用SerializeArray方法进行序列化;如果对象是一个Unity对象,则会调用SerializeUnityObject方法进行序列化。在反序列化时也是同样的方式。
阅读全文