Exemplo n.º 1
0
        /// <inheritdoc/>
        public override object Convert(object value, BinaricJsonSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer), "引数がnullです");
            }
            if (value == null)
            {
                return(ArraySerializeEntry.Null);
            }
            if (value is not Array array)
            {
                throw new SerializationException("配列に変換出来ませんでした");
            }
            var result = new ArraySerializeEntry(array.GetType().GetElementType(), array.Length);

            for (int i = 0; i < array.Length; i++)
            {
                var current = array.GetValue(i);
                if (current == null)
                {
                    result.Items[i] = null;
                    continue;
                }
                var converter = serializer.GetConverter(current.GetType()) ?? throw new SerializationException("コンバータを取得出来ませんでした");
                var setValue  = converter.Convert(current, serializer);
                result.Items[i] = (ReflectionHelper.GetTypeName(current.GetType()), setValue);
            }
            return(result);
        }
Exemplo n.º 2
0
 /// <summary>
 /// <see cref="JsonElement"/>を復元する
 /// </summary>
 /// <param name="element">復元する<see cref="JsonElement"/>のインスタンス</param>
 /// <param name="serializer">使用するシリアライザ</param>
 /// <exception cref="ArgumentNullException"><paramref name="serializer"/>がnull</exception>
 /// <returns><paramref name="element"/>を復元したオブジェクト</returns>
 public object Parse(JsonElement element, BinaricJsonSerializer serializer)
 {
     if (serializer == null)
     {
         throw new ArgumentNullException(nameof(serializer), "引数がnullです");
     }
     if (element.ValueKind == JsonValueKind.Null)
     {
         return(null);
     }
     if (element.ValueKind == JsonValueKind.Array)
     {
         var genericType = type.GetElementType();
         var array       = Array.CreateInstance(genericType, element.GetArrayLength());
         var index       = 0;
         var e           = element.EnumerateArray();
         var parser      = new JsonElementParser(genericType);
         while (e.MoveNext())
         {
             array.SetValue(parser.Parse(e.Current, serializer), index++);
         }
         return(array);
     }
     return(serializer.Deserialize(element.GetRawText(), type));
 }
Exemplo n.º 3
0
 /// <inheritdoc/>
 public override object ConvertBack(object value, BinaricJsonSerializer serializer)
 {
     if (value is JsonElement element)
     {
         var parser = new JsonElementParser(type);
         return(parser.Parse(element, serializer));
     }
     return(value);
 }
Exemplo n.º 4
0
 /// <inheritdoc/>
 public override object Convert(object value, BinaricJsonSerializer serializer)
 {
     if (serializer == null)
     {
         throw new ArgumentNullException(nameof(serializer), "引数がnullです");
     }
     if (value == null)
     {
         return(null);
     }
     return(((Type)value).FullName);
 }
Exemplo n.º 5
0
 public sealed override object ConvertBack(object value, BinaricJsonSerializer serializer)
 {
     if (value is JsonElement element)
     {
         var parser = new JsonElementParser(Type);
         return(parser.Parse(element, serializer));
     }
     if (value is not SerializeEntry entry)
     {
         throw new ArgumentException("エントリへの変換が出来ませんでした", nameof(value));
     }
     return(FromSerializeEntry(entry, serializer));
 }
Exemplo n.º 6
0
        /// <inheritdoc/>
        public override object ConvertBack(object value, BinaricJsonSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer), "引数がnullです");
            }
            if (value is JsonElement __element)
            {
                var parser = new JsonElementParser(typeof(ArraySerializeEntry));
                var v      = parser.Parse(__element, serializer);
                return(v);
            }
            //if (value is Array __array) return __array;
            if (value is not ArraySerializeEntry entry)
            {
                throw new SerializationException("エントリーに変換出来ませんでした");
            }
            if (entry.IsNull)
            {
                return(null);
            }
            var result = Array.CreateInstance(Type.GetType(entry.ItemTypeName), entry.Items.Length);

            for (int i = 0; i < entry.Items.Length; i++)
            {
                if (entry.Items[i] == null)
                {
                    result.SetValue(null, i);
                    continue;
                }
                var(name, current) = entry.Items[i];
                var valueType = Type.GetType(name);
                if (current == null)
                {
                    result.SetValue(null, i);
                    continue;
                }
                if (current is JsonElement element)
                {
                    var parser = new JsonElementParser(valueType);
                    result.SetValue(parser.Parse(element, serializer), i);
                    continue;
                }
                var converter = serializer.GetConverter(valueType) ?? throw new SerializationException("コンバータを取得出来ませんでした");
                result.SetValue(converter.ConvertBack(current, serializer), i);
            }
            return(result);
        }
Exemplo n.º 7
0
 /// <inheritdoc/>
 public override object ConvertBack(object value, BinaricJsonSerializer serializer)
 {
     if (serializer == null)
     {
         throw new ArgumentNullException(nameof(serializer), "引数がnullです");
     }
     if (value == null)
     {
         return(null);
     }
     if (value is not string typeName)
     {
         throw new SerializationException("文字列に変換できませんでした");
     }
     return(Type.GetType(typeName));
 }
Exemplo n.º 8
0
 public sealed override object Convert(object value, BinaricJsonSerializer provider) => ToSerializeEntry(value, provider);
Exemplo n.º 9
0
 /// <summary>
 /// <see cref="SerializeEntry"/>に変換する
 /// </summary>
 /// <param name="value"><see cref="SerializeEntry"/>に変換する値</param>
 /// <param name="serializer">使用するシリアライザ</param>
 /// <exception cref="ArgumentNullException"><paramref name="serializer"/>がnull</exception>
 /// <returns><paramref name="value"/>の情報を格納する<see cref="SerializeEntry"/></returns>
 protected abstract SerializeEntry ToSerializeEntry(object value, BinaricJsonSerializer serializer);
Exemplo n.º 10
0
 /// <summary>
 /// <see cref="SerializeEntry"/>からオブジェクトを復元する
 /// </summary>
 /// <param name="entry">使用する<see cref="SerializeEntry"/></param>
 /// <param name="serializer">使用するシリアライザ</param>
 /// <exception cref="ArgumentNullException"><paramref name="entry"/>または<paramref name="serializer"/>がnull</exception>
 /// <returns><paramref name="entry"/>をもとに復元された値</returns>
 protected abstract object FromSerializeEntry(SerializeEntry entry, BinaricJsonSerializer serializer);
Exemplo n.º 11
0
 /// <inheritdoc/>
 public override object Convert(object value, BinaricJsonSerializer serializer) => value;
        /// <inheritdoc/>
        protected override SerializeEntry ToSerializeEntry(object value, BinaricJsonSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer), "引数がnullです");
            }
            if (value == null)
            {
                return(SerializeEntry.Null);
            }

            var type    = value.GetType();
            var methods = ReflectionHelper.GetInstanceMethods(type);

            var result = new SerializeEntry(type);

            // OnSerializing実装メソッドを実行
            for (int i = 0; i < methods.Length; i++)
            {
                if (!ReflectionHelper.HasAttribute <OnSerializingAttribute>(methods[i]))
                {
                    continue;
                }
                var parameters = methods[i].GetParameters();
                if (parameters.Length != 1 || parameters[0].ParameterType != typeof(StreamingContext))
                {
                    continue;
                }
                methods[i].Invoke(value, new object[] { StreamingContext });
            }

            // フィールドの保存
            var fields = ReflectionHelper.GetInstanceFields(type);

            for (int i = 0; i < fields.Length; i++)
            {
                if (ReflectionHelper.HasAttribute <NonSerializedAttribute>(fields[i]))
                {
                    continue;
                }
                var converter  = serializer.GetConverter(fields[i].FieldType);
                var savedValue = converter.Convert(fields[i].GetValue(value), serializer);
                result.Fields.Add(fields[i].Name, (ReflectionHelper.GetTypeName(fields[i].FieldType), savedValue));
            }

            // OnSerialized実装メソッドを実行
            for (int i = 0; i < methods.Length; i++)
            {
                if (!ReflectionHelper.HasAttribute <OnSerializedAttribute>(methods[i]))
                {
                    continue;
                }
                var parameters = methods[i].GetParameters();
                if (parameters.Length != 1 || parameters[0].ParameterType != typeof(StreamingContext))
                {
                    continue;
                }
                methods[i].Invoke(value, new object[] { StreamingContext });
            }

            return(result);
        }
        /// <inheritdoc/>
        protected override object FromSerializeEntry(SerializeEntry entry, BinaricJsonSerializer serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer), "引数がnullです");
            }
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry), "引数がnullです");
            }
            if (entry.IsNull)
            {
                return(null);
            }

            var type    = Type.GetType(entry.TypeName);
            var result  = FormatterServices.GetUninitializedObject(type);
            var methods = ReflectionHelper.GetInstanceMethods(type);

            // OnDeserializing実装メソッドを実行
            for (int i = 0; i < methods.Length; i++)
            {
                if (!ReflectionHelper.HasAttribute <OnDeserializingAttribute>(methods[i]))
                {
                    continue;
                }
                var parameters = methods[i].GetParameters();
                if (parameters.Length != 1 || parameters[0].ParameterType != typeof(StreamingContext))
                {
                    continue;
                }
                methods[i].Invoke(result, new object[] { StreamingContext });
            }

            // フィールドの復元
            foreach (var(fieldName, (typeName, value)) in entry.Fields)
            {
                var fieldInfo = ReflectionHelper.GetInstanceField(type, fieldName);
                if (fieldInfo == null)
                {
                    if (serializer.IgnoreNotFoundMember)
                    {
                        continue;
                    }
                    throw new SerializationException("フィールドの復元に失敗しました");
                }
                var converter = serializer.GetConverter(Type.GetType(typeName)) ?? throw new SerializationException("コンバータの取得に失敗しました");
                var setValue  = converter.ConvertBack(value, serializer);
                fieldInfo.SetValue(result, setValue);
            }

            // OnDeserialized実装メソッドを実行
            for (int i = 0; i < methods.Length; i++)
            {
                if (!ReflectionHelper.HasAttribute <OnDeserializedAttribute>(methods[i]))
                {
                    continue;
                }
                var parameters = methods[i].GetParameters();
                if (parameters.Length != 1 || parameters[0].ParameterType != typeof(StreamingContext))
                {
                    continue;
                }
                methods[i].Invoke(result, new object[] { StreamingContext });
            }

            // IDeserializationCallback.OnDeserialitionを実行
            if (result is IDeserializationCallback id)
            {
                id.OnDeserialization(null);
            }

            return(result);
        }