Пример #1
0
        public RlpArray EncodeWithJson(JToken item)
        {
            if (this.Nullable && (item == null || item.Type == JTokenType.Null))
            {
                return(new RlpArray());
            }
            RlpArray result = new RlpArray();

            foreach (IRlpKind kind in Properties)
            {
                if (kind is IRlpScalarKind)
                {
                    IRlpItem rlpItem = (kind as IRlpScalarKind).EncodeWithJson(item[kind.Name]);
                    result.Add(rlpItem);
                }
                else if (kind is IRlpArrayKind)
                {
                    RlpArray rArray = (kind as IRlpArrayKind).EncodeWithJson(item[kind.Name]);
                    result.Add(rArray);
                }
                else if (kind is IRplStructKind)
                {
                    RlpArray rArray = (kind as IRplStructKind).EncodeWithJson(item[kind.Name]);
                    result.Add(rArray);
                }
                else if (kind is IRlpCustomKind)
                {
                    IRlpItem rlpItem = (kind as IRlpCustomKind).EncodeWithJson(item[kind.Name]);
                    result.Add(rlpItem);
                }
            }
            return(result);
        }
Пример #2
0
        public static byte[][] DecodeToList(this RlpArray array)
        {
            List <byte[]> datas = new List <byte[]>(array.Count);

            datas.AddRange(array.Select(item => item.RlpData));
            return(datas.ToArray());
        }
Пример #3
0
        public static byte[][] DecodeToList(this RlpArray array)
        {
            List <byte[]> datas = new List <byte[]>(array.Count);

            foreach (IRlpItem item in array)
            {
                datas.Add(item.RlpData);
            }
            return(datas.ToArray());
        }
Пример #4
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);
        }
Пример #5
0
        public RlpArray EncodeToRlp(dynamic obj)
        {
            if (this.Nullable && obj == null)
            {
                return(new RlpArray());
            }
            RlpArray array   = new RlpArray();
            string   jsonStr = JsonConvert.SerializeObject(obj, new JsonBytesConverter());
            JObject  jsonObj = JObject.Parse(jsonStr);

            array = this.EncodeWithJson(jsonObj);
            return(array);
        }
Пример #6
0
        public IRlpItem Decode(byte[] bytes)
        {
            RlpArray      rlpArray      = new RlpArray();
            RLPCollection rlpCollection = new RLPCollection();

            rlpCollection = RLP.Decode(bytes) as RLPCollection;
            this.RlpData  = rlpCollection.RLPData;
            foreach (IRLPElement item in rlpCollection)
            {
                if (item.RLPData == null || item.RLPData[0] == 0x0)
                {
                    rlpArray.Add(new RlpItem());
                    continue;
                }

                var rlpItem = new RlpItem(item.RLPData);
                rlpArray.Add(rlpItem);
            }
            return(rlpArray);
        }
Пример #7
0
        public RlpArray EncodeWithJson(JToken items)
        {
            if (this.Nullable && (items == null || items.Type == JTokenType.Null))
            {
                return(new RlpArray());
            }
            RlpArray rlpArray = new RlpArray();

            foreach (var item in (items as JArray))
            {
                if (this.ItemKind is IRlpScalarKind)
                {
                    IRlpItem rlpItem = (this.ItemKind as IRlpScalarKind).EncodeWithJson(item as JValue);
                    rlpArray.Add(rlpItem);
                }
                else if (this.ItemKind is IRlpArrayKind)
                {
                    if (item is JArray)
                    {
                        JArray   jArray = item as JArray;
                        RlpArray rArray = (this.ItemKind as IRlpArrayKind).EncodeWithJson(jArray);
                        rlpArray.Add(rArray);
                    }
                    else
                    {
                        throw new ArgumentException("invalid item type");
                    }
                }
                else if (this.ItemKind is IRplStructKind)
                {
                    RlpArray rArray = (this.ItemKind as IRplStructKind).EncodeWithJson(item);
                    rlpArray.Add(rArray);
                }
                else if (this.ItemKind is IRlpCustomKind)
                {
                    IRlpItem rlpItem = (this.ItemKind as IRlpCustomKind).EncodeWithJson(item);
                    rlpArray.Add(rlpItem);
                }
            }
            return(rlpArray);
        }