microsoft-visualbasic-runtime/Serialization/JSON/Formatter/JsonFormatterStrategyContex...

169 lines
5.3 KiB
VB.net
Raw Normal View History

2019-07-17 22:01:31 +08:00
#Region "Microsoft.VisualBasic::60e9cc85d88a634488df990b70e6a31a, Serialization\JSON\Formatter\JsonFormatterStrategyContext.vb"
2018-08-02 20:14:48 +08:00
2019-06-04 20:43:55 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
2019-05-31 19:14:18 +08:00
2019-06-04 20:43:55 +08:00
' /********************************************************************************/
2019-05-31 19:14:18 +08:00
2019-06-04 20:43:55 +08:00
' Summaries:
2019-05-31 19:14:18 +08:00
2019-06-04 20:43:55 +08:00
' Class JsonFormatterStrategyContext
'
' Properties: Indent, IsInArrayScope, IsProcessingDoubleQuoteInitiatedString, IsProcessingSingleQuoteInitiatedString, IsProcessingString
' IsStart, WasLastCharacterABackSlash
'
' Sub: AddCharacterStrategy, AppendCurrentChar, AppendIndents, AppendNewLine, AppendSpace
' BuildContextIndents, ClearStrategies, CloseCurrentScope, EnterArrayScope, EnterObjectScope
' PrettyPrintCharacter
'
'
' /********************************************************************************/
2018-08-02 20:14:48 +08:00
#End Region
Imports System.Collections.Generic
Imports System.Text
2019-05-31 19:14:18 +08:00
Imports Microsoft.VisualBasic.Language.Default
2018-08-02 20:14:48 +08:00
Imports Microsoft.VisualBasic.Serialization.JSON.Formatter.Internals.Strategies
Namespace Serialization.JSON.Formatter.Internals
Friend NotInheritable Class JsonFormatterStrategyContext
Const Space As String = " "
2019-05-31 19:14:18 +08:00
Const SpacesPerIndent As Integer = 2
2018-08-02 20:14:48 +08:00
2019-05-31 19:14:18 +08:00
Dim m_indent As String = String.Empty
Dim currentCharacter As Char
Dim previousChar As Char
2018-08-02 20:14:48 +08:00
2019-05-31 19:14:18 +08:00
Dim outputBuilder As StringBuilder
2018-08-02 20:14:48 +08:00
2019-05-31 19:14:18 +08:00
ReadOnly scopeState As New FormatterScopeState()
ReadOnly strategies As New Dictionary(Of Char, ICharacterStrategy)()
2018-08-02 20:14:48 +08:00
Public ReadOnly Property Indent() As String
Get
If Me.m_indent = String.Empty Then
2019-05-31 19:14:18 +08:00
Me.m_indent = New String(Space, SpacesPerIndent)
2018-08-02 20:14:48 +08:00
End If
Return Me.m_indent
End Get
End Property
Public ReadOnly Property IsInArrayScope() As Boolean
Get
Return Me.scopeState.IsTopTypeArray
End Get
End Property
Private Sub AppendIndents(indents As Integer)
For i As Integer = 0 To indents - 1
Me.outputBuilder.Append(Indent)
Next
End Sub
Public IsProcessingVariableAssignment As Boolean
2019-05-31 19:14:18 +08:00
Public Property IsProcessingDoubleQuoteInitiatedString As Boolean
Public Property IsProcessingSingleQuoteInitiatedString As Boolean
2018-08-02 20:14:48 +08:00
Public ReadOnly Property IsProcessingString() As Boolean
Get
Return Me.IsProcessingDoubleQuoteInitiatedString OrElse Me.IsProcessingSingleQuoteInitiatedString
End Get
End Property
Public ReadOnly Property IsStart() As Boolean
Get
Return Me.outputBuilder.Length = 0
End Get
End Property
Public ReadOnly Property WasLastCharacterABackSlash() As Boolean
Get
Return Me.previousChar = "\"c
End Get
End Property
Public Sub PrettyPrintCharacter(curChar As Char, output As StringBuilder)
2019-05-31 19:14:18 +08:00
Dim strategy As ICharacterStrategy
2018-08-02 20:14:48 +08:00
2019-05-31 19:14:18 +08:00
If Not strategies.ContainsKey(curChar) Then
strategy = New DefaultCharacterStrategy
Else
strategy = strategies(curChar)
End If
2018-08-02 20:14:48 +08:00
2019-05-31 19:14:18 +08:00
Me.currentCharacter = curChar
2018-08-02 20:14:48 +08:00
Me.outputBuilder = output
2019-05-31 19:14:18 +08:00
Call strategy.Execute(Me)
2018-08-02 20:14:48 +08:00
Me.previousChar = curChar
End Sub
Public Sub AppendCurrentChar()
Me.outputBuilder.Append(Me.currentCharacter)
End Sub
Public Sub AppendNewLine()
Me.outputBuilder.Append(Environment.NewLine)
End Sub
Public Sub BuildContextIndents()
Me.AppendNewLine()
Me.AppendIndents(Me.scopeState.ScopeDepth)
End Sub
Public Sub EnterObjectScope()
Me.scopeState.PushObjectContextOntoStack()
End Sub
Public Sub CloseCurrentScope()
Me.scopeState.PopJsonType()
End Sub
Public Sub EnterArrayScope()
Me.scopeState.PushJsonArrayType()
End Sub
Public Sub AppendSpace()
Me.outputBuilder.Append(Space)
End Sub
Public Sub ClearStrategies()
Me.strategies.Clear()
End Sub
Public Sub AddCharacterStrategy(strategy As ICharacterStrategy)
Me.strategies(strategy.ForWhichCharacter) = strategy
End Sub
End Class
End Namespace