示例#1
0
    public static OnlineMapsJSONItem Serialize(object obj)
    {
#if !UNITY_WP_8_1 || UNITY_EDITOR
        if (obj == null || obj is DBNull)
        {
            return(new OnlineMapsJSONValue(obj, OnlineMapsJSONValue.ValueType.NULL));
        }
#else
        if (obj == null)
        {
            return(new OnlineMapsJSONValue(obj, OnlineMapsJSONValue.ValueType.NULL));
        }
#endif
        if (obj is string || obj is bool || obj is int || obj is long || obj is short || obj is float || obj is double)
        {
            return(new OnlineMapsJSONValue(obj));
        }
        if (obj is IEnumerable)
        {
            IEnumerable         v     = (IEnumerable)obj;
            OnlineMapsJSONArray array = new OnlineMapsJSONArray();
            foreach (var item in v)
            {
                array.Add(Serialize(item));
            }
            return(array);
        }

        OnlineMapsJSONObject o = new OnlineMapsJSONObject();
        Type type = obj.GetType();
        IEnumerable <FieldInfo> fields = OnlineMapsReflectionHelper.GetFields(type, BindingFlags.Instance | BindingFlags.Public);
        foreach (FieldInfo field in fields)
        {
            o.Add(field.Name, Serialize(field.GetValue(obj)));
        }
        return(o);
    }
    /// <summary>
    /// Serializes an object to JSON.
    /// </summary>
    /// <param name="obj">Object</param>
    /// <returns>JSON</returns>
    public static OnlineMapsJSONItem Serialize(object obj)
    {
#if !UNITY_WP_8_1 || UNITY_EDITOR
        if (obj == null || obj is DBNull)
        {
            return(new OnlineMapsJSONValue(obj, OnlineMapsJSONValue.ValueType.NULL));
        }
#else
        if (obj == null)
        {
            return(new OnlineMapsJSONValue(obj, OnlineMapsJSONValue.ValueType.NULL));
        }
#endif
        if (obj is string || obj is bool || obj is int || obj is long || obj is short || obj is float || obj is double)
        {
            return(new OnlineMapsJSONValue(obj));
        }
        if (obj is UnityEngine.Object)
        {
            if (!(obj is Component || obj is ScriptableObject))
            {
                return(new OnlineMapsJSONValue((obj as UnityEngine.Object).GetInstanceID()));
            }
        }
        if (obj is IDictionary)
        {
            IDictionary          d          = obj as IDictionary;
            OnlineMapsJSONObject dv         = new OnlineMapsJSONObject();
            ICollection          keys       = d.Keys;
            ICollection          values     = d.Values;
            IEnumerator          keysEnum   = keys.GetEnumerator();
            IEnumerator          valuesEnum = values.GetEnumerator();
            while (keysEnum.MoveNext() && valuesEnum.MoveNext())
            {
                object k = keysEnum.Current;
                object v = valuesEnum.Current;

                dv.Add(k as string, Serialize(v));
            }
            return(dv);
        }
        if (obj is IEnumerable)
        {
            IEnumerable         v     = (IEnumerable)obj;
            OnlineMapsJSONArray array = new OnlineMapsJSONArray();
            foreach (var item in v)
            {
                array.Add(Serialize(item));
            }
            return(array);
        }

        OnlineMapsJSONObject o    = new OnlineMapsJSONObject();
        Type         type         = obj.GetType();
        BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public;
        if (OnlineMapsReflectionHelper.CheckIfAnonymousType(type))
        {
            bindingFlags |= BindingFlags.NonPublic;
        }
        IEnumerable <FieldInfo> fields = OnlineMapsReflectionHelper.GetFields(type, bindingFlags);
        foreach (FieldInfo field in fields)
        {
            string fieldName = field.Name;
            if (field.Attributes == (FieldAttributes.Private | FieldAttributes.InitOnly))
            {
                fieldName = fieldName.Trim('<', '>');
            }
            o.Add(fieldName, Serialize(field.GetValue(obj)));
        }
        return(o);
    }