Example #1
0
        private static string ToString(ContractParameter parameter, HashSet <ContractParameter> context)
        {
            switch (parameter.Value)
            {
            case null:
                return("(null)");

            case byte[] data:
                return(data.ToHexString());

            case IList <ContractParameter> data:
                if (context is null)
                {
                    context = new HashSet <ContractParameter>();
                }
                if (context.Contains(parameter))
                {
                    return("(array)");
                }
                else
                {
                    context.Add(parameter);
                    StringBuilder sb = new StringBuilder();
                    sb.Append('[');
                    foreach (ContractParameter item in data)
                    {
                        sb.Append(ToString(item, context));
                        sb.Append(", ");
                    }
                    if (data.Count > 0)
                    {
                        sb.Length -= 2;
                    }
                    sb.Append(']');
                    return(sb.ToString());
                }

            case IList <KeyValuePair <ContractParameter, ContractParameter> > data:
                if (context is null)
                {
                    context = new HashSet <ContractParameter>();
                }
                if (context.Contains(parameter))
                {
                    return("(map)");
                }
                else
                {
                    context.Add(parameter);
                    StringBuilder sb = new StringBuilder();
                    sb.Append('[');
                    foreach (var item in data)
                    {
                        sb.Append('{');
                        sb.Append(ToString(item.Key, context));
                        sb.Append(',');
                        sb.Append(ToString(item.Value, context));
                        sb.Append('}');
                        sb.Append(", ");
                    }
                    if (data.Count > 0)
                    {
                        sb.Length -= 2;
                    }
                    sb.Append(']');
                    return(sb.ToString());
                }

            default:
                return(parameter.Value.ToString());
            }
        }
Example #2
0
        private static JObject ToJson(ContractParameter parameter, HashSet <ContractParameter> context)
        {
            JObject json = new JObject();

            json["type"] = parameter.Type;
            if (parameter.Value != null)
            {
                switch (parameter.Type)
                {
                case ContractParameterType.Signature:
                case ContractParameterType.ByteArray:
                    json["value"] = ((byte[])parameter.Value).ToHexString();
                    break;

                case ContractParameterType.Boolean:
                    json["value"] = (bool)parameter.Value;
                    break;

                case ContractParameterType.Integer:
                case ContractParameterType.Hash160:
                case ContractParameterType.Hash256:
                case ContractParameterType.PublicKey:
                case ContractParameterType.String:
                    json["value"] = parameter.Value.ToString();
                    break;

                case ContractParameterType.Array:
                    if (context is null)
                    {
                        context = new HashSet <ContractParameter>();
                    }
                    else if (context.Contains(parameter))
                    {
                        throw new InvalidOperationException();
                    }
                    context.Add(parameter);
                    json["value"] = new JArray(((IList <ContractParameter>)parameter.Value).Select(p => ToJson(p, context)));
                    break;

                case ContractParameterType.Map:
                    if (context is null)
                    {
                        context = new HashSet <ContractParameter>();
                    }
                    else if (context.Contains(parameter))
                    {
                        throw new InvalidOperationException();
                    }
                    context.Add(parameter);
                    json["value"] = new JArray(((IList <KeyValuePair <ContractParameter, ContractParameter> >)parameter.Value).Select(p =>
                    {
                        JObject item  = new JObject();
                        item["key"]   = ToJson(p.Key, context);
                        item["value"] = ToJson(p.Value, context);
                        return(item);
                    }));
                    break;
                }
            }
            return(json);
        }