1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| public static class JsonParser { private static NullFormatter nullFormatter = new NullFormatter(); private static EnumFormatter enumFormatter = new EnumFormatter(); private static ArrayFormatter arrayFormatter = new ArrayFormatter(); private static ReflectionFormatter reflectionFormatter = new ReflectionFormatter(); private static PolymorphicFormatter polymorphicFormatter = new PolymorphicFormatter(); private static readonly Dictionary<Type, IJsonFormatter> formatterDict = new Dictionary<Type, IJsonFormatter>() { {typeof(bool), new BooleanFormatter()}, {typeof(byte), new ByteFormatter()}, {typeof(sbyte), new SByteFormatter()}, {typeof(short), new Int16Formatter()}, {typeof(ushort), new UInt16Formatter()}, {typeof(int), new Int32Formatter()}, {typeof(uint), new UInt32Formatter()}, {typeof(long), new Int64Formatter()}, {typeof(ulong), new UInt64Formatter()}, {typeof(float), new SingleFormatter()}, {typeof(double), new DoubleFormatter()}, {typeof(decimal), new DecimalFormatter()}, {typeof(char), new CharFormatter()}, {typeof(string), new StringFormatter()}, {typeof(List<>), new ListFormatter()}, {typeof(Dictionary<,>), new DictionaryFormatter()}, {typeof(JsonObject), new JsonObjectFormatter()}, {typeof(JsonValue), new JsonValueFormatter()}, {typeof(Hash128), new Hash128Formatter()}, {typeof(Vector2),new Vector2Formatter()}, {typeof(Vector3),new Vector3Formatter()}, {typeof(Vector4),new Vector4Formatter()}, {typeof(Quaternion),new QuaternionFormatter()}, {typeof(Color),new ColorFormatter()}, {typeof(Bounds),new BoundsFormatter()}, {typeof(Rect),new RectFormatter()}, {typeof(Keyframe),new KeyFrameFormatter()}, {Type.GetType("System.RuntimeType,mscorlib"),new RuntimeTypeFormatter()}, {typeof(DateTime),new DateTimeFormatter()}, };
public static void AddCustomJsonFormatter(Type type, IJsonFormatter formatter) { formatterDict[type] = formatter; } internal static void InternalToJson(object obj, Type type, Type realType = null, int depth = 1,bool checkPolymorphic = true) {
if (!realType.IsGenericType) { if (formatterDict.TryGetValue(realType, out IJsonFormatter formatter)) { formatter.ToJson(obj,type,realType, depth); return; } } else { if (formatterDict.TryGetValue(realType.GetGenericTypeDefinition(), out IJsonFormatter formatter)) { formatter.ToJson(obj,type,realType,depth); return; } }
#if FUCK_LUA if (type is ILRuntime.Reflection.ILRuntimeType ilrtType && ilrtType.ILType.IsEnum) { formatterDict[typeof(int)].ToJson(obj, type, realType,depth); return; } #endif if (obj is Enum e) { enumFormatter.ToJson(e, type, realType, depth); return; } if (obj is Array array) { arrayFormatter.ToJson(array,type,realType, depth); return; } reflectionFormatter.ToJson(obj,type,realType,depth); } internal static object InternalParseJson(Type type,Type realType = null,bool checkPolymorphic = true) { object result; else if (formatterDict.TryGetValue(realType, out IJsonFormatter formatter)) { result = formatter.ParseJson(type, realType); } else if (realType.IsGenericType && formatterDict.TryGetValue(realType.GetGenericTypeDefinition(), out formatter)) { result = formatter.ParseJson(type,realType); } #if FUCK_LUA else if (type is ILRuntime.Reflection.ILRuntimeType ilrtType && ilrtType.ILType.IsEnum) { result = formatterDict[typeof(int)].ParseJson(type, realType); } #endif else if (realType.IsEnum) { result = enumFormatter.ParseJson(type, realType); } else if (realType.IsArray) { result = arrayFormatter.ParseJson(type,realType); } else { result = reflectionFormatter.ParseJson(type,realType); }
if (result is IJsonParserCallbackReceiver receiver) { receiver.OnParseJsonEnd(); }
return result; }
}
|