Пример #1
0
 public RlpArrayKind(IRlpKind itemKind, string name, int maxLength, bool nullable)
 {
     this.ItemKind   = itemKind;
     this.Name       = name;
     this._maxLength = maxLength;
     this.Nullable   = nullable;
 }
Пример #2
0
        public static byte[] Encode(IRlpKind kind, dynamic obj)
        {
            IRlpItem rlpItem = null;

            if (kind is IRlpScalarKind)
            {
                rlpItem = (kind as IRlpScalarKind).EncodeToRlp(obj);
                return(rlpItem.Encode());
            }
            else if (kind is IRlpArrayKind)
            {
                if (obj is Array)
                {
                    rlpItem = (kind as IRlpArrayKind).EncodeToRlp(obj);
                    return(rlpItem.Encode());
                }
                else
                {
                    throw new ArgumentException("invalid item type");
                }
            }
            else if (kind is IRplStructKind)
            {
                rlpItem = (kind as IRplStructKind).EncodeToRlp(obj);
                return(rlpItem.Encode());
            }
            else if (kind is IRlpCustomKind)
            {
                rlpItem = (kind as IRlpCustomKind).EncodeToRlp(obj);
                return(rlpItem.Encode());
            }
            return(rlpItem.Encode());
        }
Пример #3
0
        public JToken DecodeToJson(IRlpItem rlp)
        {
            JObject  json     = new JObject();
            RlpArray rlpArray = new RlpArray();

            if (rlp.RlpType == RlpType.Array)
            {
                rlpArray = (rlp as RlpArray);
            }
            else
            {
                rlpArray = ((new RlpArray()).Decode(rlp.RlpData) as RlpArray);
            }

            if (this.Nullable && rlpArray.Count == 0)
            {
                return(new JObject());
            }

            if (rlpArray.Count == Properties.Count)
            {
                for (int index = 0; index < rlpArray.Count; index++)
                {
                    IRlpKind kind = Properties[index];
                    if (kind is IRlpScalarKind)
                    {
                        JValue jvalue = (kind as IRlpScalarKind).DecodeToJson(rlpArray[index]);
                        json[kind.Name] = jvalue;
                    }
                    else if (kind is IRlpArrayKind)
                    {
                        var value = (kind as IRlpArrayKind).DecodeToJson(rlpArray[index]);
                        json[kind.Name] = value;
                    }
                    else if (kind is IRplStructKind)
                    {
                        var value = (kind as IRplStructKind).DecodeToJson(rlpArray[index]);
                        json[kind.Name] = value;
                    }
                    else if (kind is IRlpCustomKind)
                    {
                        var value = (kind as IRlpCustomKind).DecodeToJson(rlpArray[index]);
                        json[kind.Name] = value;
                    }
                }
            }
            else
            {
                throw new ArgumentException("Decode properties count not match properties count");
            }

            return(json);
        }
Пример #4
0
        public static dynamic Decode(IRlpKind kind, byte[] data, Type type)
        {
            dynamic  result  = null;
            IRlpItem rlpItem = new RlpItem(data);

            if (kind is IRlpScalarKind)
            {
                result = (kind as IRlpScalarKind).DecodeFromRlp(rlpItem);
            }
            else if (kind is IRlpArrayKind)
            {
                result = (kind as IRlpArrayKind).DecodeFromRlp(rlpItem, type);
            }
            else if (kind is IRplStructKind)
            {
                result = (kind as IRplStructKind).DecodeFromRlp(rlpItem, type);
            }
            else if (kind is IRlpCustomKind)
            {
                result = (kind as IRlpCustomKind).DecodeFromRlp(rlpItem, type);
            }
            return(result);
        }
Пример #5
0
 public RlpArrayKind(IRlpKind itemKind, string name) : this(itemKind, name, 0, false)
 {
     this.Name = name;
 }
Пример #6
0
 public RlpArrayKind(IRlpKind itemKind, int maxLength) : this(itemKind, "", maxLength, false)
 {
     this._maxLength = maxLength;
 }
Пример #7
0
 public RlpArrayKind(IRlpKind itemKind) : this(itemKind, "", 0, false)
 {
     this.ItemKind = itemKind;
 }