public PObject Create()
            {
                if (Type == PDictionary.Type)
                {
                    var dictionary = new PDictionary();
                    foreach (var v in Values)
                    {
                        if (v.Required)
                        {
                            dictionary.Add(v.Identifier, v.Create());
                        }
                    }

                    // If nothing was required, create an initial one anyway
                    if (dictionary.Count == 0)
                    {
                        var first = Values.FirstOrDefault();
                        if (first == null)
                        {
                            dictionary.Add("newNode", PObject.Create(PString.Type));
                        }
                        else
                        {
                            dictionary.Add(first.Identifier ?? "newNode", first.Create());
                        }
                    }
                    return(dictionary);
                }
                else if (Type == PArray.Type)
                {
                    var array = new PArray();
                    foreach (var v in Values)
                    {
                        if (v.Required)
                        {
                            array.Add(v.Create());
                        }
                    }

                    // If nothing was required, create an initial one anyway
                    if (array.Count == 0)
                    {
                        var first = Values.FirstOrDefault();
                        if (first == null)
                        {
                            array.Add(PObject.Create(ArrayType));
                        }
                        else
                        {
                            array.Add(first.Create());
                        }
                    }
                    return(array);
                }
                else if (Values.Any())
                {
                    return(Values.First().Create());
                }
                else
                {
                    var obj = PObject.Create(Type);
                    if (!string.IsNullOrEmpty(Identifier) && !(this is Key))
                    {
                        obj.SetValue(Identifier);
                    }
                    return(obj);
                }
            }
Example #2
0
        internal static PObject Conv(NSObject val)
        {
            if (val == null)
            {
                return(null);
            }
            if (val is NSDictionary)
            {
                var result = new PDictionary();
                foreach (var pair in (NSDictionary)val)
                {
                    string k = pair.Key.ToString();
                    result[k] = Conv(pair.Value);
                }
                return(result);
            }

            if (val is NSArray)
            {
                var result = new PArray();
                var arr    = NSArray.ArrayFromHandle <NSObject> (((NSArray)val).Handle);
                if (arr == null)
                {
                    return(null);
                }
                foreach (var f in arr)
                {
                    if (f != null)
                    {
                        result.Add(Conv(f));
                    }
                }
                return(result);
            }

            if (val is NSString)
            {
                return(((NSString)val).ToString());
            }
            if (val is NSNumber)
            {
                var nr  = (NSNumber)val;
                var str = Marshal.PtrToStringAnsi(MonoMac.ObjCRuntime.Messaging.IntPtr_objc_msgSend(val.Handle, selObjCType));
                if (str == "c" || str == "C" || str == "B")
                {
                    return(nr.BoolValue);
                }
                return(nr.Int32Value);
            }
            if (val is NSDate)
            {
                return(PDate.referenceDate + TimeSpan.FromSeconds(((NSDate)val).SecondsSinceReferenceDate));
            }

            if (val is NSData)
            {
                var data  = (NSData)val;
                var bytes = new byte[data.Length];
                Marshal.Copy(data.Bytes, bytes, 0, (int)data.Length);
                return(bytes);
            }

            throw new NotSupportedException(val.ToString());
        }
Example #3
0
        public static PObject FromNSObject(NSObject val)
        {
            if (val == null)
            {
                return(null);
            }

            var dict = val as NSDictionary;

            if (dict != null)
            {
                var result = new PDictionary();
                foreach (var pair in dict)
                {
                    string k = pair.Key.ToString();
                    result[k] = FromNSObject(pair.Value);
                }
                return(result);
            }

            var arr = val as NSArray;

            if (arr != null)
            {
                var  result = new PArray();
                uint count  = arr.Count;
                for (uint i = 0; i < count; i++)
                {
                    var obj = MonoMac.ObjCRuntime.Runtime.GetNSObject(arr.ValueAt(i));
                    if (obj != null)
                    {
                        result.Add(FromNSObject(obj));
                    }
                }
                return(result);
            }

            var str = val as NSString;

            if (str != null)
            {
                return(str.ToString());
            }

            var nr = val as NSNumber;

            if (nr != null)
            {
                char t;
                unsafe {
                    t = (char)*((byte *)MonoMac.ObjCRuntime.Messaging.IntPtr_objc_msgSend(val.Handle, selObjCType));
                }
                if (t == 'c' || t == 'C' || t == 'B')
                {
                    return(nr.BoolValue);
                }
                return(nr.Int32Value);
            }

            var date = val as NSDate;

            if (date != null)
            {
                return((DateTime)date);
            }

            var data = val as NSData;

            if (data != null)
            {
                var bytes = new byte[data.Length];
                Marshal.Copy(data.Bytes, bytes, 0, (int)data.Length);
                return(bytes);
            }

            throw new NotSupportedException(val.ToString());
        }