Exemplo n.º 1
0
        private JObject PrepareJsonItem(VMUTStackItem item)
        {
            var ret = new JObject
            {
                ["type"]  = item.Type.ToString(),
                ["value"] = item.Value
            };

            switch (item.Type)
            {
            case VMUTStackItemType.Null:
            {
                ret["type"] = VMUTStackItemType.Null.ToString();
                ret.Remove("value");
                break;
            }

            case VMUTStackItemType.Pointer:
            {
                ret["type"]  = VMUTStackItemType.Pointer.ToString();
                ret["value"] = item.Value.Value <int>();
                break;
            }

            case VMUTStackItemType.String:
            {
                // Easy access

                ret["type"]  = VMUTStackItemType.ByteArray.ToString();
                ret["value"] = Encoding.UTF8.GetBytes(item.Value.Value <string>());
                break;
            }

            case VMUTStackItemType.ByteArray:
            case VMUTStackItemType.Buffer:
            {
                var value = ret["value"].Value <string>();
                ret["value"] = value.FromHexString();
                break;
            }

            case VMUTStackItemType.Integer:
            {
                // Ensure format

                ret["value"] = ret["value"].Value <string>();
                break;
            }

            case VMUTStackItemType.Struct:
            case VMUTStackItemType.Array:
            {
                var array = (JArray)ret["value"];

                for (int x = 0, m = array.Count; x < m; x++)
                {
                    array[x] = PrepareJsonItem(JsonConvert.DeserializeObject <VMUTStackItem>(array[x].ToString()));
                }

                ret["value"] = array;
                break;
            }

            case VMUTStackItemType.Map:
            {
                var obj = (JObject)ret["value"];

                foreach (var prop in obj.Properties())
                {
                    obj[prop.Name] = PrepareJsonItem(JsonConvert.DeserializeObject <VMUTStackItem>(prop.Value.ToString()));
                }

                ret["value"] = obj;
                break;
            }
            }

            return(ret);
        }
Exemplo n.º 2
0
        private JObject PrepareJsonItem(VMUTStackItem item)
        {
            var ret = new JObject
            {
                ["type"]  = item.Type.ToString(),
                ["value"] = item.Value
            };

            switch (item.Type)
            {
            case VMUTStackItemType.String:
            {
                // Easy access

                ret["type"]  = VMUTStackItemType.ByteArray.ToString();
                ret["value"] = Encoding.UTF8.GetBytes(item.Value.Value <string>());
                break;
            }

            case VMUTStackItemType.ByteArray:
            {
                var value = ret["value"].Value <string>();

                if (value.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
                {
                    ret["value"] = value.FromHexString();
                }
                else
                {
                    ret["value"] = Convert.FromBase64String(value);
                }

                break;
            }

            case VMUTStackItemType.Integer:
            {
                // Ensure format

                ret["value"] = ret["value"].Value <string>();
                break;
            }

            case VMUTStackItemType.Struct:
            case VMUTStackItemType.Array:
            {
                var array = (JArray)ret["value"];

                for (int x = 0, m = array.Count; x < m; x++)
                {
                    array[x] = PrepareJsonItem(JsonConvert.DeserializeObject <VMUTStackItem>(array[x].ToString()));
                }

                ret["value"] = array;
                break;
            }

            case VMUTStackItemType.Map:
            {
                var obj = (JObject)ret["value"];

                foreach (var prop in obj.Properties())
                {
                    obj[prop.Name] = PrepareJsonItem(JsonConvert.DeserializeObject <VMUTStackItem>(prop.Value.ToString()));
                }

                ret["value"] = obj;
                break;
            }
            }

            return(ret);
        }