Example #1
0
        /// <summary>
        /// Serializes using custom methods (JsonAdaptor, IJsonable, Type serializer).
        /// </summary>
        private void SerializeCustomData(object data)
        {
            Type       dataType       = data.GetType();
            JsonObject serializedData = null;

            // Serialize objects registered to adaptor.
            serializedData = JsonAdaptor.Serialize(dataType, data);
            if (serializedData != null)
            {
                SerializeObject(serializedData);
                return;
            }

            // Serialize jsonable objects
            IJsonable jsonableObject = data as IJsonable;

            if (jsonableObject != null)
            {
                serializedData = jsonableObject.ToJsonObject();
                if (serializedData != null)
                {
                    SerializeObject(jsonableObject.ToJsonObject());
                    return;
                }
            }

            // Use reflection.
            serializedData = JsonTypeSerializer.Serialize(dataType, data);
            if (serializedData != null)
            {
                SerializeObject(serializedData);
                return;
            }

            //This is when all the above methods fail.
            //Just return the stringified data.
            sb.Append('"');
            AppendEscapedString(data.ToString());
            sb.Append('"');
        }
Example #2
0
        /// <summary>
        /// Deserializes the specified JsonData for a specific type.
        /// Highly recommended to use Json.Parse with Type parameter instead.
        /// </summary>
        public static object Deserialize(Type t, object instance, JsonObject data)
        {
            object adaptorResult = JsonAdaptor.Deserialize(t, data);

            if (adaptorResult != null)
            {
                return(adaptorResult);
            }

            //IJsonable method requires an instance to be present.
            if (instance == null)
            {
                instance = DynamicService.CreateObject(t);
                if (instance == null)
                {
                    RenLog.Log(LogLevel.Error, string.Format(
                                   "JsonDeserializer.Deserialize - Failed to instantiate a dynamic object of type ({0}). If possible, try adding a parameterless constructor.",
                                   t.Name
                                   ));
                    return(null);
                }
            }

            IJsonable jsonable = instance as IJsonable;

            if (jsonable != null)
            {
                jsonable.FromJsonObject(data);
                return(instance);
            }

            //No deserializer is available.
            RenLog.Log(LogLevel.Warning, string.Format(
                           "JsonDeserializer.Deserialize - There is no deserializer available for type ({0}). Returning null."
                           ));
            return(null);
        }