Exemplo n.º 1
0
        /// <summary>
        /// 序列化Json
        /// </summary>
        /// <param name="node">Json根节点</param>
        /// <param name="strJsonFile">Json文件名称</param>
        /// <param name="bFormat">是否启用格式化</param>
        public static void Serialize(JsonNode node, string strJsonFile, bool bFormat = false)
        {
            if( !(node is JsonArray) && !(node is JsonObject) )
            {
                throw new Exception("JSON Serialize: Json root node is not obj or array!");
            }

            StreamWriter writer = new StreamWriter(strJsonFile);
            if( writer == null )
            {
                return;
            }

            node.Serialize(writer, bFormat ? 0 : -1);
            writer.Close();
        }
Exemplo n.º 2
0
 public override void Add(int nIndex, JsonNode value)
 {
     this[nIndex] = value;
 }
Exemplo n.º 3
0
 public override void Add(JsonNode value)
 {
     m_lstValue.Add(value);
 }
Exemplo n.º 4
0
 public virtual void Add(JsonNode value) { }
Exemplo n.º 5
0
 public override void Add(string strKey,JsonNode value) 
 {
     this[strKey] = value;
 }
Exemplo n.º 6
0
 public virtual void Add(string strKey, JsonNode value) { }
Exemplo n.º 7
0
 public virtual void Add(int nIndex, JsonNode value) { }
Exemplo n.º 8
0
 public override void Add(JsonNode value)
 {
     m_lstValue.Add(value);
 }
Exemplo n.º 9
0
        // Json解析,参考SimpJson
        public static JsonNode ParseJson(string strJson)
        {
            strJson = strJson.Trim();

            Stack <JsonNode> stack = new Stack <JsonNode>();
            JsonNode         ctx   = null;
            int    i         = 0;
            string Token     = "";
            string TokenName = "";
            bool   QuoteMode = false;

            while (i < strJson.Length)
            {
                switch (strJson[i])
                {
                case '{':
                    if (QuoteMode)
                    {
                        Token += strJson[i];
                        break;
                    }
                    stack.Push(new JsonObject());
                    if (ctx != null)
                    {
                        TokenName = TokenName.Trim();
                        if (ctx is JsonArray)
                        {
                            ctx.Add(stack.Peek());
                        }
                        else if (TokenName != "")
                        {
                            ctx.Add(TokenName, stack.Peek());
                        }
                    }
                    TokenName = "";
                    Token     = "";
                    ctx       = stack.Peek();
                    break;

                case '[':
                    if (QuoteMode)
                    {
                        Token += strJson[i];
                        break;
                    }

                    stack.Push(new JsonArray());
                    if (ctx != null)
                    {
                        TokenName = TokenName.Trim();
                        if (ctx is JsonArray)
                        {
                            ctx.Add(stack.Peek());
                        }
                        else if (TokenName != "")
                        {
                            ctx.Add(TokenName, stack.Peek());
                        }
                    }
                    TokenName = "";
                    Token     = "";
                    ctx       = stack.Peek();
                    break;

                case '}':
                case ']':
                    if (QuoteMode)
                    {
                        Token += strJson[i];
                        break;
                    }
                    if (stack.Count == 0)
                    {
                        throw new Exception("JSON Parse: Too many closing brackets");
                    }

                    stack.Pop();
                    if (Token != "")
                    {
                        TokenName = TokenName.Trim();
                        if (ctx is JsonArray)
                        {
                            ctx.Add(Token);
                        }
                        else if (TokenName != "")
                        {
                            ctx.Add(TokenName, Token);
                        }
                    }
                    TokenName = "";
                    Token     = "";
                    if (stack.Count > 0)
                    {
                        ctx = stack.Peek();
                    }
                    break;

                case ':':
                    if (QuoteMode)
                    {
                        Token += strJson[i];
                        break;
                    }
                    TokenName = Token;
                    Token     = "";
                    break;

                case '"':
                    QuoteMode ^= true;
                    break;

                case ',':
                    if (QuoteMode)
                    {
                        Token += strJson[i];
                        break;
                    }
                    if (Token != "")
                    {
                        if (ctx is JsonArray)
                        {
                            ctx.Add(Token);
                        }
                        else if (TokenName != "")
                        {
                            ctx.Add(TokenName, Token);
                        }
                    }
                    TokenName = "";
                    Token     = "";
                    break;

                case '\r':
                case '\n':
                    break;

                case ' ':
                case '\t':
                    if (QuoteMode)
                    {
                        Token += strJson[i];
                    }
                    break;

                case '\\':
                    ++i;
                    if (QuoteMode)
                    {
                        char C = strJson[i];
                        switch (C)
                        {
                        case 't': Token += '\t'; break;

                        case 'r': Token += '\r'; break;

                        case 'n': Token += '\n'; break;

                        case 'b': Token += '\b'; break;

                        case 'f': Token += '\f'; break;

                        case 'u':
                        {
                            string s = strJson.Substring(i + 1, 4);
                            Token += (char)int.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier);
                            i     += 4;
                            break;
                        }

                        default: Token += C; break;
                        }
                    }
                    break;

                default:
                    Token += strJson[i];
                    break;
                }
                ++i;
            }
            if (QuoteMode)
            {
                throw new Exception("JSON Parse: Quotation marks seems to be messed up.");
            }
            return(ctx);
        }
Exemplo n.º 10
0
 public override void Add(int nIndex, JsonNode value)
 {
     this[nIndex] = value;
 }
Exemplo n.º 11
0
 public override void Add(string strKey, JsonNode value)
 {
     this[strKey] = value;
 }
Exemplo n.º 12
0
 public virtual void Add(JsonNode value)
 {
 }
Exemplo n.º 13
0
 public virtual void Add(int nIndex, JsonNode value)
 {
 }
Exemplo n.º 14
0
 public virtual void Add(string strKey, JsonNode value)
 {
 }