Ejemplo n.º 1
0
    /// <summary>
    /// Parses the string json into a value
    /// </summary>
    /// <param name="json">A JSON string.</param>
    /// <returns>An ArrayList, a Hashtable, a double, a string, null, true, or false</returns>
    public static object jsonDecode(string json)
    {
        // save the string for debug information
        MUJson.lastDecode = json;

        if (json != null)
        {
            char[] charArray = json.ToCharArray();
            int    index     = 0;
            bool   success   = true;
            object value     = MUJson.parseValue(charArray, ref index, ref success);

            if (success)
            {
                MUJson.lastErrorIndex = -1;
            }
            else
            {
                MUJson.lastErrorIndex = index;
            }

            return(value);
        }
        else
        {
            return(null);
        }
    }
Ejemplo n.º 2
0
    public static object jsonDecode(string json)
    {
        MUJson.lastDecode = json;
        object result;

        if (json != null)
        {
            char[] charArray = json.ToCharArray();
            int    index     = 0;
            bool   success   = true;
            object value     = MUJson.parseValue(charArray, ref index, ref success);
            if (success)
            {
                MUJson.lastErrorIndex = -1;
            }
            else
            {
                MUJson.lastErrorIndex = index;
            }
            result = value;
        }
        else
        {
            result = null;
        }
        return(result);
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Converts a Hashtable / ArrayList / Dictionary(string,string) object into a JSON string
    /// </summary>
    /// <param name="json">A Hashtable / ArrayList</param>
    /// <returns>A JSON encoded string, or null if object 'json' is not serializable</returns>
    public static string jsonEncode(object json)
    {
        var builder = new StringBuilder(BUILDER_CAPACITY);
        var success = MUJson.serializeValue(json, builder);

        return(success ? builder.ToString() : null);
    }
Ejemplo n.º 4
0
    protected static object parseValue(char[] json, ref int index, ref bool success)
    {
        switch (MUJson.lookAhead(json, index))
        {
        case 1:
            return(MUJson.parseObject(json, ref index));

        case 3:
            return(MUJson.parseArray(json, ref index));

        case 7:
            return(MUJson.parseString(json, ref index));

        case 8:
            return(MUJson.parseNumber(json, ref index));

        case 9:
            MUJson.nextToken(json, ref index);
            return(bool.Parse("TRUE"));

        case 10:
            MUJson.nextToken(json, ref index);
            return(bool.Parse("FALSE"));

        case 11:
            MUJson.nextToken(json, ref index);
            return(null);
        }
        success = false;
        return(null);
    }
Ejemplo n.º 5
0
    protected static bool serializeObject(Hashtable anObject, StringBuilder builder)
    {
        builder.Append("{");
        IDictionaryEnumerator e = anObject.GetEnumerator();
        bool first = true;

        while (e.MoveNext())
        {
            string key   = e.Key.ToString();
            object value = e.Value;
            if (!first)
            {
                builder.Append(", ");
            }
            MUJson.serializeString(key, builder);
            builder.Append(":");
            if (!MUJson.serializeValue(value, builder))
            {
                return(false);
            }
            first = false;
        }
        builder.Append("}");
        return(true);
    }
Ejemplo n.º 6
0
    protected static double parseNumber(char[] json, ref int index)
    {
        MUJson.eatWhitespace(json, ref index);
        int lastIndex  = MUJson.getLastIndexOfNumber(json, index);
        int charLength = lastIndex - index + 1;

        char[] numberCharArray = new char[charLength];
        Array.Copy(json, index, numberCharArray, 0, charLength);
        index = lastIndex + 1;
        return(double.Parse(new string(numberCharArray)));
    }
Ejemplo n.º 7
0
    protected static bool serializeObjectOrArray(object objectOrArray, StringBuilder builder)
    {
        bool result;

        if (objectOrArray is Hashtable)
        {
            result = MUJson.serializeObject((Hashtable)objectOrArray, builder);
        }
        else
        {
            result = (objectOrArray is ArrayList && MUJson.serializeArray((ArrayList)objectOrArray, builder));
        }
        return(result);
    }
Ejemplo n.º 8
0
 protected static bool serializeValue(object value, StringBuilder builder)
 {
     if (value == null)
     {
         builder.Append("null");
     }
     else if (value.GetType().IsArray)
     {
         MUJson.serializeArray(new ArrayList((ICollection)value), builder);
     }
     else if (value is string)
     {
         MUJson.serializeString((string)value, builder);
     }
     else if (value is char)
     {
         MUJson.serializeString(Convert.ToString((char)value), builder);
     }
     else if (value is Hashtable)
     {
         MUJson.serializeObject((Hashtable)value, builder);
     }
     else if (value is Dictionary <string, string> )
     {
         MUJson.serializeDictionary((Dictionary <string, string>)value, builder);
     }
     else if (value is ArrayList)
     {
         MUJson.serializeArray((ArrayList)value, builder);
     }
     else if (value is bool && (bool)value)
     {
         builder.Append("true");
     }
     else if (value is bool && !(bool)value)
     {
         builder.Append("false");
     }
     else
     {
         if (!value.GetType().IsPrimitive)
         {
             return(false);
         }
         MUJson.serializeNumber(Convert.ToDouble(value), builder);
     }
     return(true);
 }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="client"></param>
        /// <param name="optType"></param>
        /// <param name="optTag"></param>
        /// <param name="logRecordType">每个写日志的地方,都应该在LogRecordType中增加一个定义,格式不要修改,只改数字;
        /// 原则上,上线运行后,修改记录参数的个数和内容,也应该重新定义一个新的ID,以保证日志分析程序能正确的分析原来的格式和新的格式;
        /// </param>
        /// <param name="args">所传参数必须能默认转化为字符串形式表示其值</param>
        public static void AddGameEvent(LogRecordType logRecordType, params object[] args)
        {
            try
            {
                if (!GameManager.FlagEnableGameEventLog)
                {
                    return;
                }

                int    serverId = GameManager.ServerId;
                string eventMsg = string.Format("{0}\t{1}",
                                                (int)logRecordType,
                                                serverId
                                                );

                if (logRecordType == LogRecordType.Json)
                {
                    if (args[0].GetType() == typeof(Hashtable))
                    {
                        eventMsg += "\t" + MUJson.jsonEncode(args[0]);
                    }
                    else
                    {
                        Hashtable table = new Hashtable();
                        for (int i = 0; i < args.Length - 1; i += 2)
                        {
                            table.Add(args[i], args[1]);
                        }

                        eventMsg += "\t" + MUJson.jsonEncode(table);
                    }
                }
                else
                {
                    foreach (var arg in args)
                    {
                        eventMsg += "\t" + arg.ToString();
                    }
                }

                SystemRoleEvents[(int)RoleEvent.GameEvent].AddEvent(eventMsg, EventLevels.Important);
            }
            catch (System.Exception ex)
            {
                LogManager.WriteLog(LogTypes.Exception, ex.ToString());
            }
        }
Ejemplo n.º 10
0
    protected static Hashtable parseObject(char[] json, ref int index)
    {
        Hashtable table = new Hashtable();

        MUJson.nextToken(json, ref index);
        bool done = false;

        while (!done)
        {
            int token = MUJson.lookAhead(json, index);
            if (token != 0)
            {
                if (token == 6)
                {
                    MUJson.nextToken(json, ref index);
                }
                else
                {
                    if (token == 2)
                    {
                        MUJson.nextToken(json, ref index);
                        return(table);
                    }
                    string name = MUJson.parseString(json, ref index);
                    if (name == null)
                    {
                        return(null);
                    }
                    token = MUJson.nextToken(json, ref index);
                    if (token != 5)
                    {
                        return(null);
                    }
                    bool   success = true;
                    object value   = MUJson.parseValue(json, ref index, ref success);
                    if (!success)
                    {
                        return(null);
                    }
                    table[name] = value;
                }
                continue;
            }
            return(null);
        }
        return(table);
    }
Ejemplo n.º 11
0
    protected static bool serializeDictionary(Dictionary <string, string> dict, StringBuilder builder)
    {
        builder.Append("{");
        bool first = true;

        foreach (KeyValuePair <string, string> kv in dict)
        {
            if (!first)
            {
                builder.Append(", ");
            }
            MUJson.serializeString(kv.Key, builder);
            builder.Append(":");
            MUJson.serializeString(kv.Value, builder);
            first = false;
        }
        builder.Append("}");
        return(true);
    }
Ejemplo n.º 12
0
    protected static bool serializeArray(ArrayList anArray, StringBuilder builder)
    {
        builder.Append("[");
        bool first = true;

        for (int i = 0; i < anArray.Count; i++)
        {
            object value = anArray[i];
            if (!first)
            {
                builder.Append(", ");
            }
            if (!MUJson.serializeValue(value, builder))
            {
                return(false);
            }
            first = false;
        }
        builder.Append("]");
        return(true);
    }
Ejemplo n.º 13
0
        /// <summary>
        /// 添加购物车
        /// </summary>
        /// <param name="cookies"></param>
        /// <param name="waresId"></param>
        /// <returns></returns>
        public bool AddCart(string cookies, string waresId)
        {
            JDHttpHelper jdHttp = new JDHttpHelper();

            jdHttp.Accept      = "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01";
            jdHttp.ContentType = "application/x-www-form-urlencoded";
            jdHttp.Cookies     = cookies;

            Dictionary <string, string> headerDic = new Dictionary <string, string>();

            headerDic["X-Requested-With"] = "XMLHttpRequest";
            headerDic["Accept-Encoding"]  = "gzip, deflate, br";
            headerDic["Accept-Language"]  = "zh-CN,zh;q=0.8";

            //string queryParam = string.Format("pid={0}&rid={1}",waresId,new Random().NextDouble());
            //string url = string.Format("https://cart.jd.com/tproduct?{0}",queryParam);
            string url = "https://cart.jd.com/gate.action";

            //https://cart.jd.com/gate.action
            //jdHttp.Postdata = queryParam;
            jdHttp.Postdata = string.Format("pid={0}&ptype=1&pcount=1&packId=0&targetId=0&f=3&fc=1&t=0&outSkus=&random={1}&locationId=5-274-3207-51527&callback=", waresId, new Random().NextDouble());
            //jdHttp.Referer = "https://cart.jd.com/addToCart.html?rcd=1&pid=2142731&pc=1&nr=1&rid=1496069725603&em=";
            string result = jdHttp.GetHtml(url, "post", Encoding.UTF8, headerDic).TrimStart('(').TrimEnd(')');
            //Hashtable 格式化json
            Hashtable hs = (Hashtable)MUJson.jsonDecode(result);

            if (hs == null || hs["flag"] != "true")
            {
                return(false);
            }
            else
            {
                /*Wares wa = new Wares() {
                 * id = waresId
                 * };*/
            }
            return(true);
        }
Ejemplo n.º 14
0
    protected static ArrayList parseArray(char[] json, ref int index)
    {
        ArrayList array = new ArrayList();

        MUJson.nextToken(json, ref index);
        bool done = false;

        while (!done)
        {
            int token = MUJson.lookAhead(json, index);
            if (token != 0)
            {
                if (token == 6)
                {
                    MUJson.nextToken(json, ref index);
                }
                else
                {
                    if (token == 4)
                    {
                        MUJson.nextToken(json, ref index);
                        break;
                    }
                    bool   success = true;
                    object value   = MUJson.parseValue(json, ref index, ref success);
                    if (!success)
                    {
                        return(null);
                    }
                    array.Add(value);
                }
                continue;
            }
            return(null);
        }
        return(array);
    }
Ejemplo n.º 15
0
    protected static string parseString(char[] json, ref int index)
    {
        string s = "";

        MUJson.eatWhitespace(json, ref index);
        char c        = json[index++];
        bool complete = false;

        while (!complete)
        {
            if (index == json.Length)
            {
                break;
            }
            c = json[index++];
            if (c == '"')
            {
                complete = true;
                break;
            }
            if (c == '\\')
            {
                if (index == json.Length)
                {
                    break;
                }
                c = json[index++];
                if (c == '"')
                {
                    s += '"';
                }
                else if (c == '\\')
                {
                    s += '\\';
                }
                else if (c == '/')
                {
                    s += '/';
                }
                else if (c == 'b')
                {
                    s += '\b';
                }
                else if (c == 'f')
                {
                    s += '\f';
                }
                else if (c == 'n')
                {
                    s += '\n';
                }
                else if (c == 'r')
                {
                    s += '\r';
                }
                else if (c == 't')
                {
                    s += '\t';
                }
                else if (c == 'u')
                {
                    int remainingLength = json.Length - index;
                    if (remainingLength < 4)
                    {
                        break;
                    }
                    char[] unicodeCharArray = new char[4];
                    Array.Copy(json, index, unicodeCharArray, 0, 4);
                    s      = s + "&#x" + new string(unicodeCharArray) + ";";
                    index += 4;
                }
            }
            else
            {
                s += c;
            }
        }
        string result;

        if (!complete)
        {
            result = null;
        }
        else
        {
            result = s;
        }
        return(result);
    }
Ejemplo n.º 16
0
    protected static int lookAhead(char[] json, int index)
    {
        int saveIndex = index;

        return(MUJson.nextToken(json, ref saveIndex));
    }
Ejemplo n.º 17
0
    protected static int nextToken(char[] json, ref int index)
    {
        MUJson.eatWhitespace(json, ref index);
        int result;

        if (index == json.Length)
        {
            result = 0;
        }
        else
        {
            char c = json[index];
            index++;
            char c2 = c;
            switch (c2)
            {
            case '"':
                return(7);

            case '#':
            case '$':
            case '%':
            case '&':
            case '\'':
            case '(':
            case ')':
            case '*':
            case '+':
            case '.':
            case '/':
                break;

            case ',':
                return(6);

            case '-':
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                return(8);

            case ':':
                return(5);

            default:
                switch (c2)
                {
                case '[':
                    return(3);

                case '\\':
                    break;

                case ']':
                    return(4);

                default:
                    switch (c2)
                    {
                    case '{':
                        return(1);

                    case '}':
                        return(2);
                    }
                    break;
                }
                break;
            }
            index--;
            int remainingLength = json.Length - index;
            if (remainingLength >= 5)
            {
                if (json[index] == 'f' && json[index + 1] == 'a' && json[index + 2] == 'l' && json[index + 3] == 's' && json[index + 4] == 'e')
                {
                    index += 5;
                    return(10);
                }
            }
            if (remainingLength >= 4)
            {
                if (json[index] == 't' && json[index + 1] == 'r' && json[index + 2] == 'u' && json[index + 3] == 'e')
                {
                    index += 4;
                    return(9);
                }
            }
            if (remainingLength >= 4)
            {
                if (json[index] == 'n' && json[index + 1] == 'u' && json[index + 2] == 'l' && json[index + 3] == 'l')
                {
                    index += 4;
                    return(11);
                }
            }
            result = 0;
        }
        return(result);
    }
Ejemplo n.º 18
0
    public static string jsonEncode(object json)
    {
        StringBuilder builder = new StringBuilder(2000);

        return(MUJson.serializeValue(json, builder) ? builder.ToString() : null);
    }