예제 #1
0
        /// <exception cref="System.MissingMethodException"/>
        /// <exception cref="java.lang.IllegalAccessException"/>
        /// <exception cref="java.lang.reflect.InvocationTargetException"/>
        /// <exception cref="VPackException"/>
        private void SerializeMap(
            string name,
            object value,
            VPackBuilder builder,
            Type keyType,
            IDictionary <string, object> additionalFields)
        {
            IDictionary map = (IDictionary)value;

            if (map.Count > 0)
            {
                IVPackKeyMapAdapter keyMapAdapter = this.GetKeyMapAdapter(keyType);
                if (keyMapAdapter != null)
                {
                    builder.Add(name, ValueType.OBJECT);
                    foreach (KeyValuePair <object, object> entry in map)
                    {
                        object entryValue = entry.Value;
                        this.AddValue(
                            keyMapAdapter.Serialize(entry.Key),
                            entryValue != null ? entryValue.GetType() : typeof(object),
                            entry.Value,
                            builder,
                            null,
                            additionalFields);
                    }

                    builder.Close();
                }
                else
                {
                    builder.Add(name, ValueType.ARRAY);
                    foreach (KeyValuePair <object, object> entry in map)
                    {
                        string s = null;
                        builder.Add(s, ValueType.OBJECT);
                        this.AddValue(ATTR_KEY, entry.Key.GetType(), entry.Key, builder, null, additionalFields);
                        this.AddValue(ATTR_VALUE, entry.Value.GetType(), entry.Value, builder, null, additionalFields);
                        builder.Close();
                    }

                    builder.Close();
                }
            }
            else
            {
                builder.Add(name, ValueType.OBJECT);
                builder.Close();
            }
        }
예제 #2
0
        /// <exception cref="System.MissingMethodException"/>
        /// <exception cref="java.lang.IllegalAccessException"/>
        /// <exception cref="java.lang.reflect.InvocationTargetException"/>
        /// <exception cref="VPackException"/>
        private void SerializeObject(
            string name,
            object entity,
            VPackBuilder builder,
            IDictionary <string, object> additionalFields)
        {
            IVPackSerializer serializer = this.serializers[entity.GetType()];

            if (serializer != null)
            {
                serializer.Serialize(builder, name, entity, this.serializationContext);
            }
            else
            {
                builder.Add(name, ValueType.OBJECT);
                this.SerializeFields(entity, builder, additionalFields);
                if (additionalFields.Count > 0)
                {
                    additionalFields.Clear();
                    builder.Close(true);
                }
                else
                {
                    builder.Close(false);
                }
            }
        }
예제 #3
0
 /// <exception cref="VPackBuilderException"/>
 public void Append(VPackBuilder builder, BigInteger value)
 {
     if (value <= 9 && value >= -6)
     {
         builder.AppendSmallInt((long)value);
     }
     else
     {
         builder.Add(unchecked ((byte)0x23));
         builder.Append(value, INTEGER_BYTES);
     }
 }
예제 #4
0
        /// <exception cref="System.MissingMethodException"/>
        /// <exception cref="java.lang.IllegalAccessException"/>
        /// <exception cref="java.lang.reflect.InvocationTargetException"/>
        /// <exception cref="VPackException"/>
        private void SerializeIterable(
            string name,
            object value,
            VPackBuilder builder,
            IDictionary <string, object> additionalFields)
        {
            builder.Add(name, ValueType.ARRAY);
            for (IEnumerator iterator = ((IEnumerable)value).GetEnumerator(); iterator.MoveNext();)
            {
                object element = iterator.Current;
                this.AddValue(null, element.GetType(), element, builder, null, additionalFields);
            }

            builder.Close();
        }
예제 #5
0
        /// <exception cref="System.MissingMethodException"/>
        /// <exception cref="java.lang.IllegalAccessException"/>
        /// <exception cref="java.lang.reflect.InvocationTargetException"/>
        /// <exception cref="VPackException"/>
        private void SerializeArray(
            string name,
            Array value,
            VPackBuilder builder,
            IDictionary <string, object> additionalFields)
        {
            builder.Add(name, ValueType.ARRAY);
            for (int i = 0; i < value.GetLength(0); i++)
            {
                object element = value.GetValue(i);
                this.AddValue(null, element.GetType(), element, builder, null, additionalFields);
            }

            builder.Close();
        }
예제 #6
0
        /// <exception cref="System.MissingMethodException"/>
        /// <exception cref="java.lang.IllegalAccessException"/>
        /// <exception cref="java.lang.reflect.InvocationTargetException"/>
        /// <exception cref="VPackException"/>
        private void AddValue(
            string name,
            Type type,
            object value,
            VPackBuilder builder,
            VPackCache.FieldInfo fieldInfo,
            IDictionary <string, object> additionalFields)
        {
            if (value == null)
            {
                if (this.serializeNullValues)
                {
                    builder.Add(name, ValueType.NULL);
                }
            }
            else
            {
                IVPackSerializer serializer = this.serializers[type];
                if (serializer != null)
                {
                    serializer.Serialize(builder, name, value, this.serializationContext);
                }
                else
                {
                    var info = type.GetTypeInfo();
                    if (type.IsGenericParameter)
                    {
                        if (!type.IsConstructedGenericType)
                        {
                            throw new TypeLoadException(string.Format("Creating open generic types are not supported. ({0})", type));
                        }

                        Type[] gens;

                        if (typeof(IDictionary <,>).GetTypeInfo().IsAssignableFrom(info))
                        {
                            gens = type.GetTargetType(typeof(IDictionary <,>)).GenericTypeArguments;
                            this.SerializeMap(name, value, builder, gens[0], additionalFields);
                        }
                        else if (typeof(IEnumerable <>).GetTypeInfo().IsAssignableFrom(info))
                        {
                            this.SerializeIterable(name, value, builder, additionalFields);
                        }
                        else if (type.IsConstructedGenericType)
                        {
                            this.SerializeObject(name, value, builder, additionalFields);
                        }
                    }
                    else
                    {
                        if (typeof(IDictionary).GetTypeInfo().IsAssignableFrom(info))
                        {
                            this.SerializeMap(name, value, builder, typeof(string), additionalFields);
                        }
                        else if (typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(info))
                        {
                            this.SerializeIterable(name, value, builder, additionalFields);
                        }
                        else if (info.IsArray)
                        {
                            this.SerializeArray(name, (Array)value, builder, additionalFields);
                        }
                        else
                        {
                            this.SerializeObject(name, value, builder, additionalFields);
                        }
                    }
                }
            }
        }