예제 #1
0
        public static JSONNode SerializeObject(object target)
        {
            if (target == null)
            {
                return(new JSONNode());
            }

            var objectType = target.GetType();


            //Handle special types first
            if (objectType == typeof(Guid))
            {
                return(new JSONData(target.ToString()));
            }
            //primitive detection
            var node = SerializePrimitive(target);

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

            //if
            if (objectType == typeof(DateTime))
            {
                var t = (DateTime)target;
                return(new JSONData(t.ToString()));
            }


            if (typeof(IList).IsAssignableFrom(objectType)) // collection detected
            {
                //var type = objectType.GetGenericArguments()[0];
                var list      = target as IEnumerable;
                var jsonArray = new JSONArray();

                if (list != null)
                {
                    foreach (var instance in list)
                    {
                        jsonArray.Add(SerializeObject(instance));
                    }
                }

                return(jsonArray);
            }



            //enum detection

            if (objectType.IsEnum)
            {
                return(new JSONData((int)target));
            }


            //poco-like detection

            JSONClass result = new JSONClass();

            var properties = target.GetType().GetProperties();

            foreach (var propertyInfo in properties)
            {
                if (propertyInfo.IsDefined(typeof(JsonProperty), true) || propertyInfo.Name == "Identifier")
                {
                    var value = propertyInfo.GetValue(target, null);
                    if (value != null)
                    {
                        result.Add(propertyInfo.Name, SerializeObject(value));
                    }
                }
            }

            return(result);
        }
예제 #2
0
        public static JSONNode Deserialize(System.IO.BinaryReader aReader)
        {
            JSONBinaryTag type = (JSONBinaryTag)aReader.ReadByte();

            switch (type)
            {
            case JSONBinaryTag.Array:
            {
                int       count = aReader.ReadInt32();
                JSONArray tmp   = new JSONArray();
                for (int i = 0; i < count; i++)
                {
                    tmp.Add(Deserialize(aReader));
                }
                return(tmp);
            }

            case JSONBinaryTag.Class:
            {
                int       count = aReader.ReadInt32();
                JSONClass tmp   = new JSONClass();
                for (int i = 0; i < count; i++)
                {
                    string key = aReader.ReadString();
                    var    val = Deserialize(aReader);
                    tmp.Add(key, val);
                }
                return(tmp);
            }

            case JSONBinaryTag.Value:
            {
                return(new JSONData(aReader.ReadString()));
            }

            case JSONBinaryTag.IntValue:
            {
                return(new JSONData(aReader.ReadInt32()));
            }

            case JSONBinaryTag.DoubleValue:
            {
                return(new JSONData(aReader.ReadDouble()));
            }

            case JSONBinaryTag.BoolValue:
            {
                return(new JSONData(aReader.ReadBoolean()));
            }

            case JSONBinaryTag.FloatValue:
            {
                return(new JSONData(aReader.ReadSingle()));
            }

            default:
            {
                throw new Exception("Error deserializing JSON. Unknown tag: " + type);
            }
            }
        }