| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using UnityEngine;
- namespace UniWebViewExternal {
- // A JSON utility extension which supports top-level simple types, arrays, and generic lists.
- public static class UniWebViewJsonUtility {
-
- public static T FromJson<T>(string json) {
- if (json == null) {
- return default(T);
- }
- json = json.Trim();
- Type type = typeof(T);
-
- if (IsSimpleType(type)) {
- return ParseSimpleType<T>(json);
- }
- if (type.IsArray) {
- return ParseArray<T>(json);
- }
- if (IsGenericList(type)) {
- return ParseList<T>(json);
- }
- try {
- return JsonUtility.FromJson<T>(json);
- } catch {
- // 如果失败,尝试包装后解析
- return ParseWrappedValue<T>(json);
- }
- }
- public static string ToJson(object obj, bool prettyPrint = false) {
- if (obj == null) {
- return "null";
- }
- Type type = obj.GetType();
-
- if (IsSimpleType(type)) {
- return ConvertSimpleTypeToJson(obj);
- }
- if (type.IsArray) {
- return ConvertArrayToJson(obj, prettyPrint);
- }
- if (IsGenericList(type)) {
- return ConvertListToJson(obj, prettyPrint);
- }
- // Use optimized approach: try Unity first, fallback to reflection if needed
- return ToJsonOptimized(obj, prettyPrint);
- }
- private static bool IsSimpleType(Type type) {
- return type == typeof(string) ||
- type == typeof(int) ||
- type == typeof(float) ||
- type == typeof(double) ||
- type == typeof(bool) ||
- type == typeof(long) ||
- type == typeof(short) ||
- type == typeof(byte) ||
- type == typeof(char);
- }
- private static bool IsGenericList(Type type) {
- return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>);
- }
- // Cache for type serialization capability to avoid repeated reflection
- private static readonly System.Collections.Generic.Dictionary<System.Type, bool> _typeSerializabilityCache
- = new System.Collections.Generic.Dictionary<System.Type, bool>();
- private static string ToJsonOptimized(object obj, bool prettyPrint) {
- Type type = obj.GetType();
-
- // Check cache first
- bool canUseUnityJson;
- if (_typeSerializabilityCache.TryGetValue(type, out canUseUnityJson)) {
- if (canUseUnityJson) {
- return JsonUtility.ToJson(obj, prettyPrint);
- } else {
- return ConvertObjectWithReflection(obj);
- }
- }
-
- // Not in cache, determine serializability using fast checks first
- bool fastCheck = CanUseUnityJsonUtilityFast(type);
- if (fastCheck) {
- // Fast check says it should work, use Unity JsonUtility
- _typeSerializabilityCache[type] = true;
- return JsonUtility.ToJson(obj, prettyPrint);
- }
-
- // Fast check failed, try Unity JsonUtility and verify result
- string unityResult = JsonUtility.ToJson(obj, prettyPrint);
- if (unityResult != "{}" || !HasSerializableContent(obj)) {
- // Unity succeeded or object is actually empty
- _typeSerializabilityCache[type] = true;
- return unityResult;
- }
-
- // Unity failed, use reflection and cache the result
- _typeSerializabilityCache[type] = false;
- return ConvertObjectWithReflection(obj);
- }
- private static bool CanUseUnityJsonUtilityFast(System.Type type) {
- // Fast checks without serialization test
-
- // Check if type has [Serializable] attribute
- if (System.Attribute.IsDefined(type, typeof(System.SerializableAttribute))) {
- return true;
- }
-
- // Check if it's a MonoBehaviour or ScriptableObject
- if (typeof(MonoBehaviour).IsAssignableFrom(type) ||
- typeof(ScriptableObject).IsAssignableFrom(type)) {
- return true;
- }
-
- // Check if it's a struct (structs are serializable by default in Unity)
- if (type.IsValueType && !type.IsEnum && !type.IsPrimitive) {
- return true;
- }
-
- // Anonymous types and non-serializable classes will fail fast check
- return false;
- }
-
- private static bool HasSerializableContent(object obj) {
- if (obj == null) return false;
-
- Type type = obj.GetType();
-
- // Check if object has any public fields or properties
- var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
- var fields = type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
-
- foreach (var prop in properties) {
- if (prop.CanRead && prop.GetIndexParameters().Length == 0) {
- try {
- var value = prop.GetValue(obj);
- if (value != null) return true;
- } catch {
- // Skip properties that can't be read
- }
- }
- }
-
- foreach (var field in fields) {
- try {
- var value = field.GetValue(obj);
- if (value != null) return true;
- } catch {
- // Skip fields that can't be read
- }
- }
-
- return false;
- }
- private static T ParseSimpleType<T>(string json) {
- Type type = typeof(T);
- json = json.Trim();
- try {
- if (type == typeof(string)) {
- // 移除引号
- if (json.StartsWith("\"") && json.EndsWith("\"")) {
- json = json.Substring(1, json.Length - 2);
- json = UnescapeJsonString(json);
- }
- return (T)(object)json;
- } else if (type == typeof(int)) {
- return (T)(object)int.Parse(json, CultureInfo.InvariantCulture);
- } else if (type == typeof(float)) {
- return (T)(object)float.Parse(json, CultureInfo.InvariantCulture);
- } else if (type == typeof(double)) {
- return (T)(object)double.Parse(json, CultureInfo.InvariantCulture);
- } else if (type == typeof(bool)) {
- return (T)(object)bool.Parse(json);
- } else if (type == typeof(long)) {
- return (T)(object)long.Parse(json, CultureInfo.InvariantCulture);
- } else if (type == typeof(short)) {
- return (T)(object)short.Parse(json, CultureInfo.InvariantCulture);
- } else if (type == typeof(byte)) {
- return (T)(object)byte.Parse(json, CultureInfo.InvariantCulture);
- } else if (type == typeof(char)) {
- if (json.StartsWith("\"") && json.EndsWith("\"") && json.Length == 3) {
- return (T)(object)json[1];
- }
- return (T)(object)char.Parse(json);
- }
- } catch (Exception e) {
- UniWebViewLogger.Instance.Critical($"Failed to parse simple type {type.Name}: {e.Message}");
- }
- return default(T);
- }
- private static T ParseArray<T>(string json) {
- Type elementType = typeof(T).GetElementType();
-
- string wrappedJson = $"{{\"items\":{json}}}";
-
- Type wrapperType = typeof(ArrayWrapper<>).MakeGenericType(elementType);
- object wrapper = JsonUtility.FromJson(wrappedJson, wrapperType);
- var itemsField = wrapperType.GetField("items");
- return (T)itemsField.GetValue(wrapper);
- }
- private static T ParseList<T>(string json) {
- Type elementType = typeof(T).GetGenericArguments()[0];
-
- string wrappedJson = $"{{\"items\":{json}}}";
-
- Type wrapperType = typeof(ArrayWrapper<>).MakeGenericType(elementType);
- object wrapper = JsonUtility.FromJson(wrappedJson, wrapperType);
-
- var itemsField = wrapperType.GetField("items");
- Array array = (Array)itemsField.GetValue(wrapper);
-
- Type listType = typeof(List<>).MakeGenericType(elementType);
- var list = Activator.CreateInstance(listType);
- var addMethod = listType.GetMethod("Add");
- foreach (var item in array) {
- addMethod.Invoke(list, new[] { item });
- }
- return (T)list;
- }
- private static T ParseWrappedValue<T>(string json) {
- string wrappedJson = $"{{\"value\":{json}}}";
- try {
- var wrapper = JsonUtility.FromJson<ValueWrapper<T>>(wrappedJson);
- return wrapper.value;
- } catch {
- UniWebViewLogger.Instance.Critical($"Failed to parse JSON: {json}");
- return default(T);
- }
- }
- // Single-pass unescape for the small set of escapes produced by ConvertSimpleTypeToJson
- // (\" \\ \n \r \t). A chained String.Replace is wrong here: unescaping "\\" to "\" first can
- // turn a literal "\\n" (backslash + n) into a newline on a later pass.
- private static string UnescapeJsonString(string s) {
- var sb = new System.Text.StringBuilder(s.Length);
- for (int i = 0; i < s.Length; i++) {
- char c = s[i];
- if (c == '\\' && i + 1 < s.Length) {
- char next = s[i + 1];
- switch (next) {
- case '"': sb.Append('"'); i++; break;
- case '\\': sb.Append('\\'); i++; break;
- case 'n': sb.Append('\n'); i++; break;
- case 'r': sb.Append('\r'); i++; break;
- case 't': sb.Append('\t'); i++; break;
- default: sb.Append(c); break;
- }
- } else {
- sb.Append(c);
- }
- }
- return sb.ToString();
- }
- private static string ConvertSimpleTypeToJson(object obj) {
- Type type = obj.GetType();
- if (type == typeof(string)) {
- string str = (string)obj;
- str = str.Replace("\\", "\\\\")
- .Replace("\"", "\\\"")
- .Replace("\n", "\\n")
- .Replace("\r", "\\r")
- .Replace("\t", "\\t");
- return $"\"{str}\"";
- } else if (type == typeof(bool)) {
- return obj.ToString().ToLower();
- } else if (type == typeof(float) || type == typeof(double)) {
- // Format with the invariant culture so the JSON always uses '.' as the decimal
- // separator, regardless of the app's current culture. The old Replace(",", ".")
- // hack produced invalid JSON under cultures that use '.' as a thousands separator.
- return ((IFormattable)obj).ToString(null, CultureInfo.InvariantCulture);
- } else if (type == typeof(char)) {
- return $"\"{obj}\"";
- } else {
- return obj.ToString();
- }
- }
- private static string ConvertArrayToJson(object obj, bool prettyPrint) {
- Array array = (Array)obj;
- Type elementType = obj.GetType().GetElementType();
-
- // Handle object arrays or mixed-type arrays using reflection
- if (elementType == typeof(object) || !CanUseUnityJsonUtilityFast(elementType)) {
- var sb = new System.Text.StringBuilder("[");
- for (int i = 0; i < array.Length; i++) {
- if (i > 0) sb.Append(",");
- sb.Append(ConvertObjectWithReflection(array.GetValue(i)));
- }
- sb.Append("]");
- return sb.ToString();
- }
-
- // Use Unity's JsonUtility for homogeneous typed arrays
- Type wrapperType = typeof(ArrayWrapper<>).MakeGenericType(elementType);
- object wrapper = Activator.CreateInstance(wrapperType);
-
- var itemsField = wrapperType.GetField("items");
- itemsField.SetValue(wrapper, array);
-
- string json = JsonUtility.ToJson(wrapper, prettyPrint);
-
- int start = json.IndexOf("[");
- int end = json.LastIndexOf("]") + 1;
- return json.Substring(start, end - start);
- }
- private static string ConvertListToJson(object obj, bool prettyPrint) {
- Type elementType = obj.GetType().GetGenericArguments()[0];
- // 转换 List 为数组
- var toArrayMethod = obj.GetType().GetMethod("ToArray");
- Array array = (Array)toArrayMethod.Invoke(obj, null);
- return ConvertArrayToJson(array, prettyPrint);
- }
- private static string ConvertObjectWithReflection(object obj) {
- if (obj == null) return "null";
-
- Type type = obj.GetType();
-
- // Handle basic types that might not be caught by IsSimpleType
- if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal)) {
- return ConvertSimpleTypeToJson(obj);
- }
-
- // Handle collections
- if (obj is System.Collections.IEnumerable enumerable && !(obj is string)) {
- var sb = new System.Text.StringBuilder("[");
- bool flag = true;
- foreach (var item in enumerable) {
- if (!flag) sb.Append(",");
- sb.Append(ConvertObjectWithReflection(item));
- flag = false;
- }
- sb.Append("]");
- return sb.ToString();
- }
-
- // Handle objects using reflection
- var json = new System.Text.StringBuilder("{");
- var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
- var fields = type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
-
- bool first = true;
-
- // Serialize properties
- foreach (var prop in properties) {
- if (prop.CanRead && prop.GetIndexParameters().Length == 0) { // Skip indexers
- try {
- var value = prop.GetValue(obj);
- if (!first) json.Append(",");
- json.Append("\"").Append(prop.Name).Append("\":");
- json.Append(ConvertObjectWithReflection(value));
- first = false;
- } catch {
- // Skip properties that can't be read
- }
- }
- }
-
- // Serialize fields
- foreach (var field in fields) {
- try {
- var value = field.GetValue(obj);
- if (!first) json.Append(",");
- json.Append("\"").Append(field.Name).Append("\":");
- json.Append(ConvertObjectWithReflection(value));
- first = false;
- } catch {
- // Skip fields that can't be read
- }
- }
-
- json.Append("}");
- return json.ToString();
- }
- [Serializable]
- private class ArrayWrapper<T> {
- public T[] items;
- }
- [Serializable]
- private class ValueWrapper<T> {
- public T value;
- }
- }
- }
|