/// <summary>
        /// Serialize an object to a json array token
        /// </summary>
        /// <param name="data">The object to be serialized</param>
        /// <returns>The json array token</returns>
        public override LazyJsonToken Serialize(Object data)
        {
            if (data == null)
            {
                return(new LazyJsonNull());
            }

            Type dataType = data.GetType();

            if (dataType.IsArray == false)
            {
                return(new LazyJsonNull());
            }

            Array         dataArray = (data as Array);
            LazyJsonArray jsonArray = new LazyJsonArray();

            Type arrayElementType = dataType.GetElementType();

            Type jsonSerializerType = LazyJsonSerializer.FindTypeSerializer(arrayElementType);

            if (jsonSerializerType == null)
            {
                jsonSerializerType = LazyJsonSerializer.FindBuiltInSerializer(arrayElementType);
            }

            if (jsonSerializerType != null)
            {
                LazyJsonSerializerBase jsonSerializer = (LazyJsonSerializerBase)Activator.CreateInstance(jsonSerializerType);

                foreach (Object item in dataArray)
                {
                    jsonArray.TokenList.Add(jsonSerializer.Serialize(item));
                }
            }
            else
            {
                foreach (Object item in dataArray)
                {
                    jsonArray.TokenList.Add(LazyJsonSerializer.SerializeToken(item));
                }
            }

            return(jsonArray);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Serialize an object to a json list array token
        /// </summary>
        /// <param name="data">The object to be serialized</param>
        /// <returns>The json list array token</returns>
        public override LazyJsonToken Serialize(Object data)
        {
            if (data == null)
            {
                return(new LazyJsonNull());
            }

            Type dataType = data.GetType();

            if (dataType.IsGenericType == false || dataType.GetGenericTypeDefinition() != typeof(List <>))
            {
                return(new LazyJsonNull());
            }

            Int32         itemsCount      = (Int32)dataType.GetProperties().First(x => x.Name == "Count").GetValue(data);
            PropertyInfo  propertyIndexer = dataType.GetProperties().First(x => x.GetIndexParameters().Length == 1 && x.GetIndexParameters()[0].ParameterType == typeof(Int32));
            LazyJsonArray jsonArray       = new LazyJsonArray();

            Type jsonSerializerType = LazyJsonSerializer.FindTypeSerializer(dataType.GenericTypeArguments[0]);

            if (jsonSerializerType == null)
            {
                jsonSerializerType = LazyJsonSerializer.FindBuiltInSerializer(dataType.GenericTypeArguments[0]);
            }

            if (jsonSerializerType != null)
            {
                LazyJsonSerializerBase jsonSerializer = (LazyJsonSerializerBase)Activator.CreateInstance(jsonSerializerType);

                for (int i = 0; i < itemsCount; i++)
                {
                    jsonArray.TokenList.Add(jsonSerializer.Serialize(propertyIndexer.GetValue(data, new Object[] { i })));
                }
            }
            else
            {
                for (int i = 0; i < itemsCount; i++)
                {
                    jsonArray.TokenList.Add(LazyJsonSerializer.SerializeToken(propertyIndexer.GetValue(data, new Object[] { i })));
                }
            }

            return(jsonArray);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Serialize an object to a json dictionary array token
        /// </summary>
        /// <param name="data">The object to be serialized</param>
        /// <returns>The json dictionary array token</returns>
        public override LazyJsonToken Serialize(Object data)
        {
            if (data == null)
            {
                return(new LazyJsonNull());
            }

            Type dataType = data.GetType();

            if (dataType.IsGenericType == false || dataType.GetGenericTypeDefinition() != typeof(Dictionary <,>))
            {
                return(new LazyJsonNull());
            }

            Object        collection = null;
            Int32         itemsCount = (Int32)dataType.GetProperties().First(x => x.Name == "Count").GetValue(data);
            LazyJsonArray jsonArray  = new LazyJsonArray();

            Array keysArray = Array.CreateInstance(dataType.GenericTypeArguments[0], itemsCount);

            collection = dataType.GetProperties().First(x => x.Name == "Keys").GetValue(data);
            collection.GetType().GetMethods().First(x => x.Name == "CopyTo").Invoke(collection, new Object[] { keysArray, 0 });

            Array valuesArray = Array.CreateInstance(dataType.GenericTypeArguments[1], itemsCount);

            collection = dataType.GetProperties().First(x => x.Name == "Values").GetValue(data);
            collection.GetType().GetMethods().First(x => x.Name == "CopyTo").Invoke(collection, new Object[] { valuesArray, 0 });

            Type jsonSerializerType = null;
            LazyJsonSerializerBase jsonSerializerKey   = null;
            LazyJsonSerializerBase jsonSerializerValue = null;

            // Keys serializer
            jsonSerializerType = LazyJsonSerializer.FindTypeSerializer(dataType.GenericTypeArguments[0]);

            if (jsonSerializerType == null)
            {
                jsonSerializerType = LazyJsonSerializer.FindBuiltInSerializer(dataType.GenericTypeArguments[0]);
            }

            if (jsonSerializerType != null)
            {
                jsonSerializerKey = (LazyJsonSerializerBase)Activator.CreateInstance(jsonSerializerType);
            }

            // Values serializer
            jsonSerializerType = LazyJsonSerializer.FindTypeSerializer(dataType.GenericTypeArguments[1]);

            if (jsonSerializerType == null)
            {
                jsonSerializerType = LazyJsonSerializer.FindBuiltInSerializer(dataType.GenericTypeArguments[1]);
            }

            if (jsonSerializerType != null)
            {
                jsonSerializerValue = (LazyJsonSerializerBase)Activator.CreateInstance(jsonSerializerType);
            }

            for (int i = 0; i < itemsCount; i++)
            {
                LazyJsonToken jsonTokenKey   = null;
                LazyJsonToken jsonTokenValue = null;

                if (jsonSerializerKey != null)
                {
                    jsonTokenKey = jsonSerializerKey.Serialize(keysArray.GetValue(i));
                }
                else
                {
                    jsonTokenKey = LazyJsonSerializer.SerializeToken(keysArray.GetValue(i));
                }

                if (jsonSerializerValue != null)
                {
                    jsonTokenValue = jsonSerializerValue.Serialize(valuesArray.GetValue(i));
                }
                else
                {
                    jsonTokenValue = LazyJsonSerializer.SerializeToken(valuesArray.GetValue(i));
                }

                LazyJsonArray jsonArrayKeyValuePair = new LazyJsonArray();
                jsonArrayKeyValuePair.TokenList.Add(jsonTokenKey);
                jsonArrayKeyValuePair.TokenList.Add(jsonTokenValue);
                jsonArray.Add(jsonArrayKeyValuePair);
            }

            return(jsonArray);
        }