예제 #1
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);
                }
            }
        }
예제 #2
0
            /// <exception cref="VPackBuilderException"/>
            public void Append(VPackBuilder builder, ValueType value)
            {
                switch (value)
                {
                case ValueType.NULL:
                {
                    builder.AppendNull();
                    break;
                }

                case ValueType.ARRAY:
                {
                    builder.AddArray(false);
                    break;
                }

                case ValueType.OBJECT:
                {
                    builder.AddObject(false);
                    break;
                }

                default:
                {
                    throw new VPackValueTypeException(ValueType.ARRAY, ValueType.OBJECT, ValueType.NULL);
                }
                }
            }
예제 #3
0
 /// <exception cref="VPackParserException"/>
 public void Serialize(VPackBuilder builder, string attribute, object entity)
 {
     this._enclosing.Serialize(
         attribute,
         entity,
         entity.GetType(),
         builder,
         new Dictionary <string, object>());
 }
예제 #4
0
        /// <exception cref="System.MissingMethodException"/>
        /// <exception cref="java.lang.IllegalAccessException"/>
        /// <exception cref="java.lang.reflect.InvocationTargetException"/>
        /// <exception cref="VPackException"/>
        private void SerializeField(
            object entity,
            VPackBuilder builder,
            VPackCache.FieldInfo fieldInfo,
            IDictionary <string, object> additionalFields)
        {
            string fieldName = fieldInfo.FieldName;
            Type   type      = fieldInfo.Type;
            object value     = fieldInfo.get(entity);

            this.AddValue(fieldName, type, value, builder, fieldInfo, additionalFields);
        }
예제 #5
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);
     }
 }
예제 #6
0
        /// <exception cref="VPackParserException"/>
        public virtual VPackSlice Serialize(object entity, Type type, IDictionary <string, object> additionalFields)
        {
            if (type == typeof(VPackSlice))
            {
                return((VPackSlice)entity);
            }

            VPackBuilder builder = new VPackBuilder(this.builderOptions);

            this.Serialize(null, entity, type, builder, new Dictionary <string, object>(additionalFields));
            return(builder.Slice());
        }
예제 #7
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();
            }
        }
예제 #8
0
        /// <exception cref="VPackException"/>
        public virtual VPackSlice FromJson(string json, bool includeNullValues)
        {
            VPackBuilder builder = new VPackBuilder();

            try
            {
                new VPackJsonRParser(builder, includeNullValues).Parse(json);
            }
            catch (JsonReaderException e)
            {
                throw new VPackBuilderException(e);
            }

            return(builder.Slice());
        }
예제 #9
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();
        }
예제 #10
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();
        }
예제 #11
0
 /// <exception cref="VPackParserException"/>
 private void Serialize(
     string name,
     object entity,
     Type type,
     VPackBuilder builder,
     IDictionary <string, object> additionalFields)
 {
     try
     {
         this.AddValue(name, type, entity, builder, null, additionalFields);
     }
     catch (Exception e)
     {
         throw new VPackParserException(e);
     }
 }
예제 #12
0
        /// <exception cref="System.MissingMethodException"/>
        /// <exception cref="java.lang.IllegalAccessException"/>
        /// <exception cref="java.lang.reflect.InvocationTargetException"/>
        /// <exception cref="VPackException"/>
        private void SerializeFields(object entity, VPackBuilder builder, IDictionary <string, object> additionalFields)
        {
            IDictionary <string, VPackCache.FieldInfo> fields = this.cache.getFields(entity.GetType());

            foreach (VPackCache.FieldInfo fieldInfo in fields.Values)
            {
                if (fieldInfo.IsSerialize)
                {
                    this.SerializeField(entity, builder, fieldInfo, additionalFields);
                }
            }

            foreach (KeyValuePair <string, object> entry in additionalFields)
            {
                string key = entry.Key;
                if (!fields.ContainsKey(key))
                {
                    object value = entry.Value;
                    this.AddValue(key, value != null ? value.GetType() : null, value, builder, null, additionalFields);
                }
            }
        }
예제 #13
0
 /// <exception cref="VPackBuilderException"/>
 public void Append(VPackBuilder builder, bool value)
 {
     builder.AppendBoolean(value);
 }
예제 #14
0
 void IVPackSerializer.Serialize(VPackBuilder builder, string attribute, object value, IVPackSerializationContext context)
 {
     Serialize(builder, attribute, (T)value, context);
 }
예제 #15
0
 /// <exception cref="VPackException"/>
 public abstract void Serialize(VPackBuilder builder, string attribute, T value, IVPackSerializationContext context);
예제 #16
0
 /// <exception cref="VPackBuilderException"/>
 public void Append(VPackBuilder builder, decimal value)
 {
     builder.AppendDouble((double)value);
 }
예제 #17
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);
                        }
                    }
                }
            }
        }
예제 #18
0
 /// <exception cref="VPackBuilderException"/>
 public void Append(VPackBuilder builder, DateTime value)
 {
     builder.AppendDate(value);
 }
예제 #19
0
 /// <exception cref="VPackBuilderException"/>
 public void Append(VPackBuilder builder, string value)
 {
     builder.AppendString(value);
 }
예제 #20
0
 /// <exception cref="VPackBuilderException"/>
 public void Append(VPackBuilder builder, float value)
 {
     builder.AppendDouble(value);
 }
예제 #21
0
 /// <exception cref="VPackBuilderException"/>
 public void Append(VPackBuilder builder, char value)
 {
     builder.AppendString(value.ToString());
 }
예제 #22
0
 /// <exception cref="VPackBuilderException"/>
 public void Append(VPackBuilder builder, byte[] value)
 {
     builder.AppendBinary(value);
 }
예제 #23
0
 /// <exception cref="VPackBuilderException"/>
 public void Append(VPackBuilder builder, Value value)
 {
     builder.Set(value);
 }
예제 #24
0
 /// <exception cref="VPackBuilderException"/>
 public void Append(VPackBuilder builder, VPackSlice value)
 {
     builder.AppendVPack(value);
 }