使用JsonTextReader提高Json.NET反序列化的效能

無風聽海發表於2023-02-24

一、碰到的問題
在伺服器的檔案系統上有一個業務生成的BigTable.json檔案,其可能包含的JSON字串很大,同時裡邊的集合會包含很多的記錄;我們使用以下的程式碼來反序列化,雖然使用了非同步的ReadAllTextAsync來讀取檔案,但是還是需要將整個的檔案內容都讀取到記憶體中,這樣會極大的佔用伺服器記憶體,同時分配太多物件或分配非常大的物件會導致垃圾收集減慢甚至停止應用程式;

string jsonStr = File.ReadAllTextAsync("json.txt").Result;
BigTable table = JsonConvert.DeserializeObject<BigTable>(jsonStr);

二、JsonTextReader的簡單用法

為了儘量減少記憶體使用和分配物件的數量,Json.NET提供了了解JsonTextReader,支援直接對流進行序列化和反序列化。每次讀取或寫入JSON片段,而不是將整個JSON字串載入到記憶體中,這在處理大於85kb的JSON檔案時尤其重要,以避免JSON字串最終出現在大物件堆中.

string json = @"{
   'CPU': 'Intel',
   'PSU': '500W',
   'Drives': [
     'DVD read/writer'
     /*(broken)*/,
     '500 gigabyte hard drive',
     '200 gigabyte hard drive'
   ]
}";

JsonTextReader reader = new JsonTextReader(new StringReader(json));
while (reader.Read())
{
    if (reader.Value != null)
    {
        Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
    }
    else
    {
        Console.WriteLine("Token: {0}", reader.TokenType);
    }
}



Token: StartObject
Token: PropertyName, Value: CPU
Token: String, Value: Intel
Token: PropertyName, Value: PSU
Token: String, Value: 500W
Token: PropertyName, Value: Drives
Token: StartArray
Token: String, Value: DVD read/writer
Token: Comment, Value: (broken)
Token: String, Value: 500 gigabyte hard drive
Token: String, Value: 200 gigabyte hard drive
Token: EndArray
Token: EndObject

三、簡單瞭解JsonTextReader的處理過程

JsonTextReader內部會透過State列舉記錄其現在讀取JSON字串所在的位;

        /// <summary>
        /// Specifies the state of the reader.
        /// </summary>
        protected internal enum State
        {
            /// <summary>
            /// A <see cref="JsonReader"/> read method has not been called.
            /// </summary>
            Start,

            /// <summary>
            /// The end of the file has been reached successfully.
            /// </summary>
            Complete,

            /// <summary>
            /// Reader is at a property.
            /// </summary>
            Property,

            /// <summary>
            /// Reader is at the start of an object.
            /// </summary>
            ObjectStart,

            /// <summary>
            /// Reader is in an object.
            /// </summary>
            Object,

            /// <summary>
            /// Reader is at the start of an array.
            /// </summary>
            ArrayStart,

            /// <summary>
            /// Reader is in an array.
            /// </summary>
            Array,

            /// <summary>
            /// The <see cref="JsonReader.Close()"/> method has been called.
            /// </summary>
            Closed,

            /// <summary>
            /// Reader has just read a value.
            /// </summary>
            PostValue,

            /// <summary>
            /// Reader is at the start of a constructor.
            /// </summary>
            ConstructorStart,

            /// <summary>
            /// Reader is in a constructor.
            /// </summary>
            Constructor,

            /// <summary>
            /// An error occurred that prevents the read operation from continuing.
            /// </summary>
            Error,

            /// <summary>
            /// The end of the file has been reached successfully.
            /// </summary>
            Finished
        }

JsonTextReader在Read方法內部會根據當前_currentState來決定下一步處理的邏輯;在開始的時候_currentState=State.Start,所以會呼叫ParseValue方法;

        public override bool Read()
        {
            EnsureBuffer();
            MiscellaneousUtils.Assert(_chars != null);

            while (true)
            {
                switch (_currentState)
                {
                    case State.Start:
                    case State.Property:
                    case State.Array:
                    case State.ArrayStart:
                    case State.Constructor:
                    case State.ConstructorStart:
                        return ParseValue();
                    case State.Object:
                    case State.ObjectStart:
                        return ParseObject();
                    case State.PostValue:
                        // returns true if it hits
                        // end of object or array
                        if (ParsePostValue(false))
                        {
                            return true;
                        }
                        break;
                    case State.Finished:
                        if (EnsureChars(0, false))
                        {
                            EatWhitespace();
                            if (_isEndOfFile)
                            {
                                SetToken(JsonToken.None);
                                return false;
                            }
                            if (_chars[_charPos] == '/')
                            {
                                ParseComment(true);
                                return true;
                            }

                            throw JsonReaderException.Create(this, "Additional text encountered after finished reading JSON content: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos]));
                        }
                        SetToken(JsonToken.None);
                        return false;
                    default:
                        throw JsonReaderException.Create(this, "Unexpected state: {0}.".FormatWith(CultureInfo.InvariantCulture, CurrentState));
                }
            }
        }

JsonTextReader的ParseValue方法會根據當前讀取的字元決定下一步的處理邏輯;由於_chars陣列預設初始化的時候第一個字元是\0,並且_charsUsed和_charPos都為0,所以會呼叫ReadData方法;

        private bool ParseValue()
        {
            MiscellaneousUtils.Assert(_chars != null);

            while (true)
            {
                char currentChar = _chars[_charPos];

                switch (currentChar)
                {
                    case '\0':
                        if (_charsUsed == _charPos)
                        {
                            if (ReadData(false) == 0)
                            {
                                return false;
                            }
                        }
                        else
                        {
                            _charPos++;
                        }
                        break;
                    case '"':
                    case '\'':
                        ParseString(currentChar, ReadType.Read);
                        return true;
                    case 't':
                        ParseTrue();
                        return true;
                    case 'f':
                        ParseFalse();
                        return true;
                    case 'n':
                        if (EnsureChars(1, true))
                        {
                            char next = _chars[_charPos + 1];

                            if (next == 'u')
                            {
                                ParseNull();
                            }
                            else if (next == 'e')
                            {
                                ParseConstructor();
                            }
                            else
                            {
                                throw CreateUnexpectedCharacterException(_chars[_charPos]);
                            }
                        }
                        else
                        {
                            _charPos++;
                            throw CreateUnexpectedEndException();
                        }
                        return true;
                    case 'N':
                        ParseNumberNaN(ReadType.Read);
                        return true;
                    case 'I':
                        ParseNumberPositiveInfinity(ReadType.Read);
                        return true;
                    case '-':
                        if (EnsureChars(1, true) && _chars[_charPos + 1] == 'I')
                        {
                            ParseNumberNegativeInfinity(ReadType.Read);
                        }
                        else
                        {
                            ParseNumber(ReadType.Read);
                        }
                        return true;
                    case '/':
                        ParseComment(true);
                        return true;
                    case 'u':
                        ParseUndefined();
                        return true;
                    case '{':
                        _charPos++;
                        SetToken(JsonToken.StartObject);
                        return true;
                    case '[':
                        _charPos++;
                        SetToken(JsonToken.StartArray);
                        return true;
                    case ']':
                        _charPos++;
                        SetToken(JsonToken.EndArray);
                        return true;
                    case ',':
                        // don't increment position, the next call to read will handle comma
                        // this is done to handle multiple empty comma values
                        SetToken(JsonToken.Undefined);
                        return true;
                    case ')':
                        _charPos++;
                        SetToken(JsonToken.EndConstructor);
                        return true;
                    case StringUtils.CarriageReturn:
                        ProcessCarriageReturn(false);
                        break;
                    case StringUtils.LineFeed:
                        ProcessLineFeed();
                        break;
                    case ' ':
                    case StringUtils.Tab:
                        // eat
                        _charPos++;
                        break;
                    default:
                        if (char.IsWhiteSpace(currentChar))
                        {
                            // eat
                            _charPos++;
                            break;
                        }
                        if (char.IsNumber(currentChar) || currentChar == '-' || currentChar == '.')
                        {
                            ParseNumber(ReadType.Read);
                            return true;
                        }

                        throw CreateUnexpectedCharacterException(currentChar);
                }
            }
        }

在JsonTextReader內部會透過_chars來讀取少量的字元;

        private int ReadData(bool append)
        {
            return ReadData(append, 0);
        }
        
        
        private int ReadData(bool append, int charsRequired)
        {
            if (_isEndOfFile)
            {
                return 0;
            }

            PrepareBufferForReadData(append, charsRequired);
            MiscellaneousUtils.Assert(_chars != null);

            int attemptCharReadCount = _chars.Length - _charsUsed - 1;

            int charsRead = _reader.Read(_chars, _charsUsed, attemptCharReadCount);

            _charsUsed += charsRead;

            if (charsRead == 0)
            {
                _isEndOfFile = true;
            }

            _chars[_charsUsed] = '\0';
            return charsRead;
        }

四、使用JsonTextReader最佳化反序列化

透過以上分析,我們可以直接使用二進位制的檔案流來讀取檔案,並將它傳遞給JsonTextReader,這樣就可以實現小片段的讀取並序列化;

using (FileStream s = File.Open("json.txt", FileMode.Open))
using (StreamReader sr = new StreamReader(s))
using (JsonReader reader = new JsonTextReader(sr))
{
    JsonSerializer serializer = new JsonSerializer();
    // read the json from a stream
    // json size doesn't matter because only a small piece is read at a time
    BigTable table = serializer.Deserialize<BigTable>(reader);
}
using (StreamReader sr = File.OpenText("json.txt"))
using (JsonReader reader = new JsonTextReader(sr))
{
    JsonSerializer serializer = new JsonSerializer();
    // read the json from a stream
    // json size doesn't matter because only a small piece is read at a time
    BigTable table = serializer.Deserialize<BigTable>(reader);
}
              

相關文章