コード例 #1
0
    public override OnlineMapsJSONItem GetAll(string k)
    {
        OnlineMapsJSONItem  item = GetThis(k);
        OnlineMapsJSONArray arr  = null;

        if (item != null)
        {
            arr = new OnlineMapsJSONArray();
            arr.Add(item);
        }
        var enumerator = _table.GetEnumerator();

        while (enumerator.MoveNext())
        {
            item = enumerator.Current.Value;
            OnlineMapsJSONArray subArr = item.GetAll(k) as OnlineMapsJSONArray;
            if (subArr != null)
            {
                if (arr == null)
                {
                    arr = new OnlineMapsJSONArray();
                }
                arr.AddRange(subArr);
            }
        }
        return(arr);
    }
コード例 #2
0
    public override OnlineMapsJSONItem GetAll(string k)
    {
        OnlineMapsJSONItem  item = GetThis(k);
        OnlineMapsJSONArray arr  = null;

        if (item != null)
        {
            arr = new OnlineMapsJSONArray();
            arr.Add(item);
        }
        for (int i = 0; i < _count; i++)
        {
            item = array[i];
            OnlineMapsJSONArray subArr = item.GetAll(k) as OnlineMapsJSONArray;
            if (subArr != null)
            {
                if (arr == null)
                {
                    arr = new OnlineMapsJSONArray();
                }
                arr.AddRange(subArr);
            }
        }
        return(arr);
    }
    public static OnlineMapsJSONItem Serialize(object obj)
    {
        if (obj == null || obj is DBNull)
        {
            return(new OnlineMapsJSONValue(obj, OnlineMapsJSONValue.ValueType.NULL));
        }
        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();

        FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
        foreach (FieldInfo field in fields)
        {
            o.Add(field.Name, Serialize(field.GetValue(obj)));
        }
        return(o);
    }
コード例 #4
0
ファイル: JSONExample.cs プロジェクト: dimatti/maps_test
        /// <summary>
        /// Dynamic generations of JSON
        /// </summary>
        private void DynamicGeneration()
        {
            // Create Object node
            OnlineMapsJSONObject rootNode = new OnlineMapsJSONObject();

            // Create and add value nodes
            rootNode.Add("text", new OnlineMapsJSONValue("Hello"));
            rootNode.Add("text2", new OnlineMapsJSONValue("World", OnlineMapsJSONValue.ValueType.STRING));

            // Create and add array nodes
            OnlineMapsJSONArray childs = new OnlineMapsJSONArray();

            rootNode.Add("childs", childs);
            childs.Add(new OnlineMapsJSONValue(255));

            // Combine nodes
            OnlineMapsJSONObject anotherNode = new OnlineMapsJSONObject();

            anotherNode.Add("text3", new OnlineMapsJSONValue("Another Node text"));
            anotherNode.Add("text", new OnlineMapsJSONValue("This value will be ignored in rootNode, because it already has a node with the text key."));
            rootNode.Combine(anotherNode);

            // Serialize an anonymous class, and combine it with rootNode
            rootNode.AppendObject(new
            {
                x    = 123,
                y    = 456,
                item = Item.Generate()
            });

            Debug.Log(rootNode.ToString());
        }
コード例 #5
0
 /// <summary>
 /// Adds an elements to the array.
 /// </summary>
 /// <param name="collection">Array of elements</param>
 public void AddRange(OnlineMapsJSONArray collection)
 {
     if (collection == null)
     {
         return;
     }
     array.AddRange(collection.array);
     _count += collection._count;
 }
コード例 #6
0
    protected virtual OnlineMapsJSONItem SaveSettings()
    {
        OnlineMapsJSONArray jitems = new OnlineMapsJSONArray();

        foreach (U marker in items)
        {
            jitems.Add(marker.ToJSON());
        }
        OnlineMapsJSONObject json = new OnlineMapsJSONObject();

        json.Add("settings", new OnlineMapsJSONObject());
        json.Add("items", jitems);
        return(json);
    }
コード例 #7
0
    private OnlineMapsJSONItem GetThis(string key)
    {
        int kindex;

        if (key.Contains("/"))
        {
            int    index    = key.IndexOf("/");
            string k        = key.Substring(0, index);
            string nextPart = key.Substring(index + 1);

            if (k == "*")
            {
                OnlineMapsJSONArray arr = new OnlineMapsJSONArray();
                for (int i = 0; i < _count; i++)
                {
                    OnlineMapsJSONItem item = array[i][nextPart];
                    if (item != null)
                    {
                        arr.Add(item);
                    }
                }
                return(arr);
            }
            if (int.TryParse(k, out kindex))
            {
                if (kindex < 0 || kindex >= _count)
                {
                    return(null);
                }
                OnlineMapsJSONItem item = array[kindex];
                return(item[nextPart]);
            }
        }
        if (key == "*")
        {
            return(this);
        }
        if (int.TryParse(key, out kindex))
        {
            return(this[kindex]);
        }
        return(null);
    }
コード例 #8
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);
    }
コード例 #9
0
    private OnlineMapsJSONArray ParseArray()
    {
        OnlineMapsJSONArray array = new OnlineMapsJSONArray();

        lookAheadToken = Token.None;

        while (true)
        {
            switch (LookAhead())
            {
            case Token.Comma:
                lookAheadToken = Token.None;
                break;

            case Token.Squared_Close:
                lookAheadToken = Token.None;
                return(array);

            default:
                array.Add(ParseValue());
                break;
            }
        }
    }
コード例 #10
0
    /// <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);
    }