예제 #1
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));
 }
예제 #2
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);
 }
예제 #3
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));
 }
예제 #4
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);
        }