Пример #1
0
        private static object FromJsonObject(Type t, BjSJsonObject obj)
        {
            object result = Activator.CreateInstance(t);

            PropertyInfo[] properties = t.GetProperties().Where(pi => pi.CanRead && pi.CanWrite).ToArray();
            foreach (BjSJsonObjectMember member in obj)
            {
                PropertyInfo p = properties.FirstOrDefault(pi => pi.Name.Equals(member.Name));
                if (p == null || !p.CanRead || !p.CanWrite)
                {
                    continue;
                }
                object value = FromJsonValue(member.Value, p.PropertyType);
                if (value == null)
                {
                    p.SetValue(result, value, null);
                    continue;
                }
                if (value is IncompatibleValue)
                {
                    continue;
                }
                p.SetValue(result, value, null);
            }

            return(result);
        }
Пример #2
0
 public ObjectState(BjSJsonReader reader, Action <BjSJsonObject> onDone)
 {
     _reader      = reader;
     _onDone      = onDone;
     _curr        = new BjSJsonObject();
     _internState = InternalState.WaitForName;
 }
Пример #3
0
        /// <param name="stream">A text stream containing Json text</param>
        public BjSJsonObject(StreamReader stream) : this()
        {
            BjSJsonObject obj = BjSJsonHelper.BjSJsonReader.Parse(stream).FirstOrDefault(o => o is BjSJsonObject) as BjSJsonObject;

            if (obj != null)
            {
                this.Members = obj.Members;
            }
        }
Пример #4
0
        /// <param name="filename">Path to the json data file</param>
        /// <param name="fileEncoding">The text encoding of the file - if null Encoding.Default is taken</param>
        public BjSJsonObject(string filename, Encoding fileEncoding) : this()
        {
            if (fileEncoding == null)
            {
                fileEncoding = Encoding.Default;
            }

            using (StreamReader s = new StreamReader(filename, fileEncoding))
            {
                BjSJsonObject obj = BjSJsonHelper.BjSJsonReader.Parse(s).FirstOrDefault(o => o is BjSJsonObject) as BjSJsonObject;
                if (obj != null)
                {
                    this.Members = obj.Members;
                }
            }
        }
Пример #5
0
        private static BjSJsonObject ToJsonObject(Type t, object obj)
        {
            BjSJsonObject result = new BjSJsonObject();

            PropertyInfo[] properties = t.GetProperties();
            foreach (PropertyInfo pi in properties)
            {
                if (pi.CanRead && pi.CanWrite)
                {
                    object val  = pi.GetValue(obj, null);
                    object jVal = ToJsonValue(val);
                    if (!(jVal is IncompatibleValue))
                    {
                        result.Add(pi.Name, jVal);
                    }
                }
            }

            return(result);
        }
Пример #6
0
 /// <summary>
 /// Tries to map a BjSJsonObject's content into an instance of the specified type T
 /// </summary>
 public static T FromJson <T>(BjSJsonObject obj) where T : class, new()
 {
     return((T)FromJsonObject(typeof(T), obj));
 }