Imports System.Collections.Generic
tem.Text.RegularExpressions
am Hzhsh hahahahaha null Public Sub Main()
Dim jsonLiteral As String = "{""key"": [1, 2, 3], ""nested"": {""object"": true}}"
Console.WriteLine(JSON.Parse(jsonLiteral))
Public Function Parse(ByVal literal As String) As XDocument
If String.IsNullOrWhitespace(literal) Then Return Nothing
Dim document As XDocument = Nothing
Dim value As XElement = Parse_Value(literal, 0)
If value IsNot Nothing Then
document = New XDocument(New XDeclaration("1.0", "utf-8", "yes"), value)
Private Function Parse_Value(ByVal source As String, ByRef index As Integer) As XElement
index = JSON.SkipWhitespace(source, index)
Dim node As XElement = JSON.Parse_Null(source, index)
node = JSON.Parse_Boolean(source, index)
node = JSON.Parse_Number(source, index)
node = JSON.Parse_String(source, index)
node = JSON.Parse_Array(source, index)
node = JSON.Parse_Object(source, index)
Private Function Parse_Object(ByVal source As String, ByRef index As Integer) As XElement
Dim node As XElement = Nothing
If source(index) = "{"c Then
Dim keys, values As New List(Of XElement)
Dim tempIndex As Integer = JSON.SkipWhitespace(source, index + 1)
Dim key As XElement = Nothing
Dim item As XElement = Nothing
Do While tempIndex < source.Length AndAlso source(tempIndex) <> "}"c
key = Parse_String(source, tempIndex)
If key IsNot Nothing Then
tempIndex = JSON.SkipWhitespace(source, tempIndex)
If source(tempIndex) = ":"c Then
tempIndex = JSON.SkipWhitespace(source, tempIndex + 1)
item = Parse_Value(source, tempIndex)
If item IsNot Nothing Then
tempIndex = JSON.SkipWhitespace(source, tempIndex)
If source(tempIndex) = ","c Then
tempIndex = JSON.SkipWhitespace(source, tempIndex + 1)
ElseIf source(tempIndex) <> ","c AndAlso source(tempIndex) <> "}"c Then
Throw New Exception("Unexpected token at position: " & tempIndex + 1 & ". Expected a comma to separate Object items.")
Throw New Exception("Invalid item in array at position: " & tempIndex + 1)
Throw New Exception("Unexpected token at position: " & tempIndex + 1 & ". Expected a colon to separate Array items.")
Throw New Exception("Unexpected token at position: " & tempIndex + 1 & ". Expected a String to represent the key/value pair of an Object.")
If tempIndex < source.Length AndAlso source(tempIndex) = "}"c Then
node = New XElement("object")
For i As Integer = 0 To keys.Count - 1
node.Add(New XElement(keys.Item(i).Value, values.Item(i)))
Private Function Parse_Array(ByVal source As String, ByRef index As Integer) As XElement
Dim node As XElement = Nothing
If source(index) = "["c Then
Dim nodes As List(Of XElement) = New List(Of XElement)
Dim tempIndex As Integer = JSON.SkipWhitespace(source, index + 1)
Dim item As XElement = Nothing
Do While tempIndex < source.Length AndAlso source(tempIndex) <> "]"c
item = Parse_Value(source, tempIndex)
If item IsNot Nothing Then
tempIndex = JSON.SkipWhitespace(source, tempIndex)
If source(tempIndex) = ","c Then
tempIndex = JSON.SkipWhitespace(source, tempIndex + 1)
ElseIf source(tempIndex) <> ","c AndAlso source(tempIndex) <> "]"c Then
Throw New Exception("Unexpected token at position: " & tempIndex + 1 & ". Expected a comma to separate Array items.")
Throw New Exception("Invalid item in array at position: " & tempIndex + 1)
If tempIndex < source.Length AndAlso source(tempIndex) = "]"c Then
node = New XElement("array", nodes)
Private Function Parse_String(ByVal source As String, ByRef index As Integer) As XElement
Dim node As XElement = Nothing
Dim pattern As Regex = New Regex("""([^""\\]|\\[""\\\/bfnrt])*""", RegexOptions.IgnoreCase)
Dim m As Match = pattern.Match(source, index)
If m.Success AndAlso m.Index = index Then
node = New XElement("string", m.Value.Substring(1, m.Value.Length - 2))
Private Function Parse_Number(ByVal source As String, ByRef index As Integer) As XElement
Dim node As XElement = Nothing
Dim pattern As Regex = New Regex("-?([1-9]\d*|0)(.\d+)?([eE][-+]?\d+)?")
Dim m As Match = pattern.Match(source, index)
If m.Success AndAlso m.Index = index Then
node = New XElement("number", m.Value)
Private Function Parse_Boolean(ByVal source As String, ByRef index As Integer) As XElement
Dim node As XElement = Nothing
Dim startSubstring As String = source.Substring(index)
If startSubstring.IndexOf("true", StringComparison.OrdinalIgnoreCase) = 0 Then
node = New XElement("boolean", startSubstring.Substring(0, 4))
ElseIf startSubstring.IndexOf("false", StringComparison.OrdinalIgnoreCase) = 0 Then
node = New XElement("boolean", startSubstring.Substring(0, 5))
Private Function Parse_Null(ByVal source As String, ByRef index As Integer) As XElement
Dim node As XElement = Nothing
If source.Substring(index).IndexOf("null", StringComparison.OrdinalIgnoreCase) = 0 Then
node = New XElement("null")
Private Function SkipWhitespace(ByVal source As String, ByVal index As Integer) As Integer
Do While index < source.Length AndAlso Char.IsWhiteSpace(source(index))