コード例 #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("");
        }
コード例 #2
0
        JsonClassMetadata _getMetadata(Type t)
        {
            JsonClassMetadata metadata = null;

            _metadataCache.TryGetValue(t, out metadata);
            if (metadata == null)
            {
                metadata          = new JsonClassMetadata(t);
                _metadataCache[t] = metadata;
            }

            return(metadata);
        }
コード例 #3
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;
            }
        }