예제 #1
0
 private static void AddToRefCache(Type type, object value, Hashtable classTable)
 {
     if (classTable != null)
     {
         Dictionary <object, JsonSerializer.SerializeObjInfo> dict = JsonSerializer.serializeCache[type];
         long id = JsonSerializer.curObjectId += 1;
         dict[value] = new JsonSerializer.SerializeObjInfo
         {
             id         = id,
             classTable = classTable
         };
     }
 }
예제 #2
0
        private static JsonSerializer.SerializeObjInfo TryGetSerializedObject(Type type, object obj)
        {
            Dictionary <object, JsonSerializer.SerializeObjInfo> dictionary = null;

            JsonSerializer.SerializeObjInfo result = null;
            if (!JsonSerializer.serializeCache.TryGetValue(type, out dictionary))
            {
                dictionary = new Dictionary <object, JsonSerializer.SerializeObjInfo>();
                JsonSerializer.serializeCache[type] = dictionary;
                return(null);
            }

            if (dictionary.TryGetValue(obj, out result))
            {
                return(result);
            }

            return(null);
        }
예제 #3
0
        public static object SerializeObject(object value, object context)
        {
            Type type = value.GetType();

            JsonSerializer.SerializeObjInfo serializeObjInfo = JsonSerializer.TryGetSerializedObject(type, value);
            if (serializeObjInfo != null)
            {
                Hashtable hashtable = new Hashtable();
                hashtable[STR_CLS_TYPE] = -1;
                hashtable[STR_REF_ID]   = serializeObjInfo.id;
                serializeObjInfo.classTable[STR_REF_ID] = serializeObjInfo.id;
                return(hashtable);
            }

            object result = null;

            try
            {
                if (type.IsEnum)
                {
                    result = JsonSerializer.SerializeEnum(value, type);
                }
                else
                {
                    if (type.IsArray)
                    {
                        result = JsonSerializer.SerializeArray(value, type, context);
                    }
                    else
                    {
                        if (value is IList)
                        {
                            result = JsonSerializer.SerializeList(value, type, context);
                        }
                        else if (value is IDictionary)
                        {
                            result = JsonSerializer.SerializeDict(value, type, context);
                        }
                        else
                        {
                            if (JsonSerializer.IsBaseType(type))
                            {
                                //result = value;
                                result = CreateValueTable(value);
                            }
                            else
                            {
                                if (UnitySerializeObjectType.IsSerializeType(type))
                                {
                                    result = JsonSerializer.SerializeUnityStruct(value, type, context);
                                }
                                else
                                {
                                    if (type.IsClass || type.IsValueType)
                                    {
                                        result = JsonSerializer.SerializeClass(value, type, context);
                                    }
                                    else
                                    {
                                        LogCat.LogError("unsupport serialize type:" + type.ToString());
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogCat.LogError("SerializeObject failed:" + ex.ToString());
            }

            return(result);
        }