Example #1
0
        void _writeObject(object obj, EasyJsonWriter writer)
        {
            if (obj == null)
            {
                writer.WriteJsonValue(null);
                return;
            }

            if (++_depth > MAX_DEPTH)
            {
                throw new EasyJsonException("Class nested level is beyond max depth, check whether there is a loop");
            }

            Type t = obj.GetType();

            EasyJsonController controller = GetController(t);

            if (controller != null)
            {
                writer.WriteJsonValue(controller.ObjectToJson(obj));
                return;
            }

            writer.WriteObjectStart();

            JsonClassMetadata metadata = _getMetadata(t);

            foreach (KeyValuePair <string, FieldInfo> kv in metadata.Fields)
            {
                _writeObject(kv.Key, kv.Value.GetValue(obj), writer);
            }
            foreach (KeyValuePair <string, PropertyInfo> kv in metadata.Properties)
            {
                MethodInfo getMethod = kv.Value.GetGetMethod();
                if (getMethod == null)
                {
                    throw new EasyJsonException("Fail to access getter of " + kv.Value.Name);
                }

                if (getMethod.GetParameters().Length > 0)
                {
                    continue;
                }

                _writeObject(kv.Key, getMethod.Invoke(obj, null), writer);
            }

            if (metadata.IsDynamicType)
            {
                _writeObject(DYNAMIC_TYPE_NAME, t, writer);
            }

            writer.WriteObjectEnd();
            writer.WriteJsonValue("");
        }
Example #2
0
        public override string ObjectToJson(object obj)
        {
            Type ut = Enum.GetUnderlyingType(obj.GetType());
            EasyJsonController controller = GetController(ut);

            if (controller == null)
            {
                throw new EasyJsonException("Doesn't support Enum parser whose underlying type is " + ut.ToString());
            }

            return(controller.ObjectToJson(Convert.ChangeType(obj, ut)));
        }
        public override object JsonToObject(EasyJsonData data, Type t)
        {
            if (data.IsNullValue)
            {
                return(null);
            }

            Type ut = Nullable.GetUnderlyingType(t);
            EasyJsonController controller = GetController(ut);

            if (controller == null)
            {
                throw new EasyJsonException("Doesn't support JSON parser for Nullable " + ut.ToString());
            }

            return(controller.JsonToObject(data, ut));
        }
        public override string ObjectToJson(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            Type ut = Nullable.GetUnderlyingType(obj.GetType());
            EasyJsonController controller = GetController(ut);

            if (controller == null)
            {
                throw new EasyJsonException("Doesn't support JSON parser for Nullable " + ut.ToString());
            }

            return(controller.ObjectToJson(obj));
        }
Example #5
0
        internal static EasyJsonController GetMainController()
        {
            EasyJsonController controller = null;

            lock (__controllers)
            {
                int id = Thread.CurrentThread.ManagedThreadId;
                __controllers.TryGetValue(id, out controller);
                if (controller == null)
                {
                    controller        = new EasyJsonController();
                    __controllers[id] = controller;
                }
            }

            return(controller);
        }
Example #6
0
        protected EasyJsonController GetController(Type t)
        {
            if (t.IsGenericType)
            {
                t = t.GetGenericTypeDefinition();
            }
            else if (t.IsEnum)
            {
                t = typeof(Enum);
            }
            else if (t.IsArray)
            {
                t = typeof(Array);
            }
            else if (t.IsSubclassOf(typeof(Type)))
            {
                t = typeof(Type);
            }

            EasyJsonController controller = null;

            _controllers.TryGetValue(t, out controller);
            if (controller == null)
            {
                if (MainController != this)
                {
                    controller = MainController.GetController(t);
                }
                else
                {
                    __defaultControllers.TryGetValue(t, out controller);
                }
            }

            return(controller);
        }
Example #7
0
        public static void UnregisterController(Type t)
        {
            EasyJsonController current = GetMainController();

            current.UnregisterController(t);
        }
Example #8
0
        public static void RegisterController(Type t, EasyJsonController controller)
        {
            EasyJsonController current = GetMainController();

            current.RegisterController(t, controller);
        }
Example #9
0
        public static object ToObject(EasyJsonData jsonData, Type t)
        {
            EasyJsonController controller = GetMainController();

            return(controller.JsonToObject(jsonData, t));
        }
Example #10
0
        public static T ToObject <T>(EasyJsonData jsonData)
        {
            EasyJsonController controller = GetMainController();

            return((T)(controller.JsonToObject(jsonData, typeof(T))));
        }
Example #11
0
        public static T ToObject <T>(string json)
        {
            EasyJsonController controller = GetMainController();

            return((T)(controller.JsonToObject(json, typeof(T))));
        }
Example #12
0
        public static string ToJson(object obj)
        {
            EasyJsonController controller = GetMainController();

            return(controller.ObjectToJson(obj));
        }
Example #13
0
 public void RegisterController(Type t, EasyJsonController controller)
 {
     _controllers[t] = controller;
 }
Example #14
0
        object _readObject(EasyJsonData data, Type t)
        {
            if (data.IsNullValue)
            {
                return(null);
            }

            try
            {
                // If user register a specific type controller but still invoke base JsonToObject, there
                // will make a loop: base -> GetController(t) -> child -> base
                if (_checkedData != data)
                {
                    _checkedData = data;

                    if (data.Type == EasyJsonDataType.JsonObject)
                    {
                        EasyJsonData typeData = data[DYNAMIC_TYPE_NAME];
                        if (typeData != null)
                        {
                            JsonSystemTypeController typeController = GetController(typeof(Type)) as JsonSystemTypeController;
                            if (typeController == null)
                            {
                                throw new EasyJsonException("Can't find System.Type controller to process dynamic type");
                            }
                            t = typeController.JsonToObject(typeData, typeof(Type)) as Type;
                        }
                    }

                    EasyJsonController controller = GetController(t);
                    if (controller != null)
                    {
                        return(controller.JsonToObject(data, t));
                    }
                }

                if (data.Type != EasyJsonDataType.JsonObject)
                {
                    throw new EasyJsonException("Can't load JSON data for " + t.ToString());
                }

                object            inst     = Activator.CreateInstance(t);
                JsonClassMetadata metadata = _getMetadata(t);

                foreach (KeyValuePair <string, FieldInfo> kv in metadata.Fields)
                {
                    EasyJsonData fieldData = data[kv.Key];
                    if (fieldData != null)
                    {
                        FieldInfo field = kv.Value;
                        field.SetValue(inst, _readObject(fieldData, field.FieldType));
                    }
                }
                foreach (KeyValuePair <string, PropertyInfo> kv in metadata.Properties)
                {
                    EasyJsonData propertyData = data[kv.Key];
                    if (propertyData != null)
                    {
                        PropertyInfo property = kv.Value;
                        property.SetValue(inst, _readObject(propertyData, property.PropertyType), null);
                    }
                }

                return(inst);
            }
            finally
            {
                _checkedData = null;
            }
        }