Пример #1
0
 public object FromJsonArray( JsonArray array )
 {
     jsonItems = new List<object> ();
     foreach ( object item in array )
         Add ( item );
     return this;
 }
Пример #2
0
        private static JsonArray ArrayParse( Stream stream )
        {
            JsonArray jsonArray = new JsonArray ();

            StringBuilder automataToken = null;
            TokenType tokenType = TokenType.Unknown;

            BinaryReader br = new BinaryReader ( stream );
            br.ReadChar ();

            while ( stream.CanRead && stream.Position != stream.Length )
            {
                char readedCharacter = br.ReadChar ();

                switch ( tokenType )
                {
                    case TokenType.Unknown:
                        {
                            if ( readedCharacter == ' ' || readedCharacter == '\t' )
                                continue;
                            else if ( readedCharacter == '"' )
                                tokenType = TokenType.String;
                            else if ( readedCharacter == '{' )
                                jsonArray.Add ( Parse ( stream ) );
                            else if ( readedCharacter == '[' )
                                jsonArray.Add ( ArrayParse ( stream ) );
                            else if ( readedCharacter == ']' )
                            {
                                return jsonArray;
                            }
                            else if ( ( readedCharacter == 't' || readedCharacter == 'f' ) ||
                                ( readedCharacter == 'T' || readedCharacter == 'F' ) )
                            {
                                stream.Seek ( -1, SeekOrigin.Current );
                                tokenType = TokenType.Boolean;
                            }
                            else if ( readedCharacter == 'n' || readedCharacter == 'N' )
                            {
                                stream.Seek ( -1, SeekOrigin.Current );
                                tokenType = TokenType.Null;
                            }
                            else if ( readedCharacter >= '0' &&
                                readedCharacter <= '9' )
                            {
                                stream.Seek ( -1, SeekOrigin.Current );
                                tokenType = TokenType.Integer;
                            }
                            automataToken = new StringBuilder ();
                        }
                        break;
                    case TokenType.String:
                        {
                            if ( readedCharacter == '"' )
                            {
                                jsonArray.Add ( automataToken.ToString () );
                                tokenType = TokenType.Unknown;
                                automataToken = null;
                                break;
                            }
                            else if ( readedCharacter == '\\' )
                            {
                                readedCharacter = br.ReadChar ();
                                if ( readedCharacter == 'n' )
                                    automataToken.Append ( '\n' );
                                else
                                    automataToken.Append ( readedCharacter );
                            }
                            automataToken.Append ( readedCharacter );
                        }
                        break;
                    case TokenType.Boolean:
                        {
                            if ( automataToken.ToString ().ToLower () == "true" ||
                                automataToken.ToString ().ToLower () == "false" )
                            {
                                jsonArray.Add ( bool.Parse ( automataToken.ToString () ) );
                                tokenType = TokenType.Unknown;
                                automataToken = null;
                                break;
                            }
                            automataToken.Append ( readedCharacter );
                        }
                        break;
                    case TokenType.Null:
                        {
                            if ( automataToken.ToString ().ToLower () == "null" )
                            {
                                jsonArray.Add ( null );
                                tokenType = TokenType.Unknown;
                                automataToken = null;
                                break;
                            }
                            automataToken.Append ( readedCharacter );
                        }
                        break;
                    case TokenType.Integer:
                        {
                            if ( readedCharacter == '.' )
                                tokenType = TokenType.FloatingPoint;
                            else if ( !( readedCharacter >= '0' &&
                                readedCharacter <= '9' ) )
                            {
                                jsonArray.Add ( int.Parse ( automataToken.ToString () ) );
                                tokenType = TokenType.Unknown;
                                automataToken = null;
                                break;
                            }
                            automataToken.Append ( readedCharacter );
                        }
                        break;
                    case TokenType.FloatingPoint:
                        {
                            if ( !( readedCharacter >= '0' &&
                                   readedCharacter <= '9' ) )
                            {
                                jsonArray.Add ( double.Parse ( automataToken.ToString () ) );
                                tokenType = TokenType.Unknown;
                                automataToken = null;
                                break;
                            }
                            automataToken.Append ( readedCharacter );
                        }
                        break;
                }
            }

            return jsonArray;
        }