コード例 #1
0
 static EasyJsonController()
 {
     __defaultControllers = new Dictionary <Type, EasyJsonController>();
     __defaultControllers[typeof(Type)]           = new JsonSystemTypeController();
     __defaultControllers[typeof(Dictionary <,>)] = new JsonDictionaryController();
     __defaultControllers[typeof(List <>)]        = new JsonListController();
     __defaultControllers[typeof(Array)]          = new JsonArrayController();
     __defaultControllers[typeof(String)]         = new JsonStringController();
     __defaultControllers[typeof(Int32)]          = new JsonInt32Controller();
     __defaultControllers[typeof(Int16)]          = new JsonInt16Controller();
     __defaultControllers[typeof(Int64)]          = new JsonInt64Controller();
     __defaultControllers[typeof(Byte)]           = new JsonByteController();
     __defaultControllers[typeof(Char)]           = new JsonCharController();
     __defaultControllers[typeof(Single)]         = new JsonFloatController();
     __defaultControllers[typeof(Double)]         = new JsonDoubleController();
     __defaultControllers[typeof(Decimal)]        = new JsonDecimalController();
     __defaultControllers[typeof(UInt16)]         = new JsonUInt16Controller();
     __defaultControllers[typeof(UInt32)]         = new JsonUInt32Controller();
     __defaultControllers[typeof(UInt64)]         = new JsonUInt64Controller();
     __defaultControllers[typeof(Boolean)]        = new JsonBoolController();
     __defaultControllers[typeof(Enum)]           = new JsonEnumController();
     __defaultControllers[typeof(DateTime)]       = new JsonDateTimeController();
     __defaultControllers[typeof(Nullable <>)]    = new JsonNullableController();
     __defaultControllers[typeof(EasyJsonData)]   = new JsonDataController();
 }
コード例 #2
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;
            }
        }