Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions jsonObject.class.asp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class JSONobject
dim i_debug, i_depth, i_parent
dim i_properties, i_version, i_defaultPropertyName
dim i_properties_count, i_properties_capacity
dim i_lowerCaseKeys
private vbback

' Set to true to show the internals of the parsing mecanism
Expand All @@ -41,6 +42,16 @@ class JSONobject
i_defaultPropertyName = value
end property


' Gets/sets the automatic lowercasing of all property names when added, effectivelly turning property names case insensitive
public property get lowerCaseKeys
lowerCaseKeys = i_lowerCaseKeys
end property

public property let lowerCaseKeys(value)
i_lowerCaseKeys = value
end property


' The depth of the object in the chain, starting with 1
public property get depth
Expand Down Expand Up @@ -82,6 +93,7 @@ class JSONobject
i_depth = 0
i_debug = false
i_defaultPropertyName = JSON_DEFAULT_PROPERTY_NAME
i_lowerCaseKeys = false

set i_parent = nothing
redim i_properties(-1)
Expand Down Expand Up @@ -596,7 +608,7 @@ class JSONobject
found = false

i = 0

do while i < i_properties_count
set p = i_properties(i)

Expand Down Expand Up @@ -663,7 +675,11 @@ class JSONobject
if prop.name = JSON_ROOT_KEY then
out = out & ("""" & obj.defaultPropertyName & """:")
else
out = out & ("""" & prop.name & """:")
if i_lowerCaseKeys then
out = out & ("""" & lcase(prop.name) & """:")
else
out = out & ("""" & prop.name & """:")
end if
end if

if isArray(value) or GetTypeName(value) = "JSONarray" then
Expand Down Expand Up @@ -960,7 +976,7 @@ end class
' JSON array class
' Represents an array of JSON objects and values
class JSONarray
dim i_items, i_depth, i_parent, i_version, i_defaultPropertyName
dim i_items, i_depth, i_parent, i_version, i_defaultPropertyName, i_lowerCaseKeys
dim i_items_count, i_items_capacity

' The class version
Expand Down Expand Up @@ -1012,6 +1028,7 @@ class JSONarray
set i_parent = value
i_depth = i_parent.depth + 1
i_defaultPropertyName = i_parent.defaultPropertyName
i_lowerCaseKeys = i_parent.lowerCaseKeys
end property

' Gets/sets the default property name generated when loading recordsets and arrays (default "data")
Expand All @@ -1024,11 +1041,21 @@ class JSONarray
end property


' Gets/sets the automatic lowercasing of all property names when added, effectivelly turning property names case insensitive
public property get lowerCaseKeys
lowerCaseKeys = i_lowerCaseKeys
end property

public property let lowerCaseKeys(value)
i_lowerCaseKeys = value
end property


' Constructor and destructor
private sub class_initialize
i_version = "2.4.0"
i_defaultPropertyName = JSON_DEFAULT_PROPERTY_NAME
i_lowerCaseKeys = false
redim i_items(-1)
i_items_count = 0
i_items_capacity = 0
Expand Down Expand Up @@ -1193,7 +1220,7 @@ end class
' represents a name/value pair of a JSON object
class JSONpair
dim i_name, i_value
dim i_parent
dim i_parent, i_lowerCaseKeys

' The name or key of the pair
public property get name
Expand Down Expand Up @@ -1228,11 +1255,23 @@ class JSONpair

public property set parent(val)
set i_parent = val
i_lowerCaseKeys = i_parent.lowerCaseKeys
end property


' Get/sets the automatic lowercasing of all property names when added, effectivelly turning property names case insensitive
public property get lowerCaseKeys
lowerCaseKeys = i_lowerCaseKeys
end property

public property let lowerCaseKeys(value)
i_lowerCaseKeys = value
end property


' Constructor and destructor
private sub class_initialize
i_lowerCaseKeys = false
end sub

private sub class_terminate
Expand Down
48 changes: 26 additions & 22 deletions test.asp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ response.buffer = true
<h1>JSON Object and Array Tests</h1>
<%
server.ScriptTimeout = 10
dim jsonObj, jsonString, jsonArr, outputObj
dim jsonObj, jsonString, jsonArr, outputObj, start
dim testLoad, testAdd, testRemove, testValue, testChange, testArrayPush, testLoadRecordset
dim testLoadArray, testChangeDefaultPropertyName, testGetItemAt
dim testLoadArray, testChangeDefaultPropertyName, testLowercaseKeys

testLoad = true
testLoadArray = true
Expand All @@ -47,34 +47,36 @@ response.buffer = true
testLoadRecordset = true

testChangeDefaultPropertyName = true

testLowercaseKeys = false

set jsonObj = new JSONobject
set jsonArr = new jsonArray

jsonObj.debug = false
jsonObj.debug = false ' switch to true to print internal parsing

if testLowercaseKeys then
jsonObj.lowerCaseKeys = true
jsonArr.lowerCaseKeys = true
end if

%><p><b>Lowercase keys<% if testLowercaseKeys then %> enabled<% else %> disabled<% end if %></b></p><%

if testLoad then
jsonString = "{ ""strings"" : ""valorTexto"", ""numbers"": 123.456, ""bools"": true, ""arrays"": [1, ""2"", 3.4, [5, -6, [7, 8, [[[""9"", ""10""]]]]]], ""emptyArray"": [], ""emptyObject"": {}, ""objects"": { ""prop1"": ""outroTexto"", ""prop2"": [ { ""id"": 1, ""name"": ""item1"" }, { ""id"": 2, ""name"": ""item2"", ""teste"": { ""maisum"": [1, 2, 3] } } ] }, ""multiline"": ""Texto com\r\nMais de\r\numa linha e escapado com \\."" }"

if testLoadArray then jsonString = "[" & jsonString & "]"

dim start
start = timer()
set outputObj = jsonObj.parse(jsonString)

if testLoadArray and left(jsonString, 1) <> "[" then jsonString = "[" & jsonString & "]"
%>
<h3>Parse Input</h3>
<pre><%= jsonString %></pre>
<%
response.flush

dim start
start = timer()
set outputObj = jsonObj.parse(jsonString)
if testLoadArray then set jsonArr = outputObj

response.Write "Load time: " & (timer() - start) & " s<br>"
response.Write "Load time: " & round(timer() - start, 6) & " s<br>"
end if

if testAdd then
Expand All @@ -96,13 +98,13 @@ response.buffer = true
multArr(2, 1) = "2,1"
multArr(2, 2) = "2,2"
multArr(2, 3) = "2,3"
jsonObj.add "nome", "Jozé"

jsonObj.add "nome", "José"
jsonObj.add "ficticio", true
jsonObj.add "idade", 25
jsonObj.add "saldo", -52
jsonObj.add "bio", "Nascido em São Paulo\Brasil" & vbcrlf & "Sem filhos" & vbcrlf & vbtab & "Jogador de WoW"
jsonObj.add "data", now
jsonObj.add "data-atual", now
jsonObj.add "lista", arr
jsonObj.add "lista2", multArr

Expand All @@ -122,7 +124,9 @@ response.buffer = true
jsonObj.defaultPropertyName = "CustomName"
jsonArr.defaultPropertyName = "CustomArrName"
end if



if testValue then
%><h3>Get Values</h3><%
response.write "nome: " & jsonObj.value("nome") & "<br>"
Expand Down Expand Up @@ -174,28 +178,28 @@ response.buffer = true
dim rs
set rs = createObject("ADODB.Recordset")

' prepera an in memory recordset
' could be, and mostly, loaded from a database
' prepere an in memory recordset
' could be, and most of the time will be, loaded from a database instead
rs.CursorType = adOpenKeyset
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic

rs.Fields.Append "ID", adInteger, , adFldKeyColumn
rs.Fields.Append "Nome", adVarChar, 50, adFldMayBeNull
rs.Fields.Append "NomeCompleto", adVarChar, 50, adFldMayBeNull
rs.Fields.Append "Valor", adDecimal, 14, adFldMayBeNull
rs.Fields("Valor").NumericScale = 2

rs.Open

rs.AddNew
rs("ID") = 1
rs("Nome") = "Nome 1"
rs("NomeCompleto") = "Nome 1"
rs("Valor") = 10.99
rs.Update

rs.AddNew
rs("ID") = 2
rs("Nome") = "Nome 2"
rs("NomeCompleto") = "Nome 2"
rs("Valor") = 29.90
rs.Update

Expand All @@ -213,10 +217,10 @@ response.buffer = true
if testLoad then
start = timer()
%>
<h3>Parse Output</h3>
<h3>Parse Output<% if testLoadArray then %>: Array<% end if %></h3>
<pre><%= outputObj.write %></pre>
<%
response.write timer() - start
response.write round(timer() - start, 6)
response.write " s<br>"
response.flush
end if
Expand Down Expand Up @@ -288,7 +292,7 @@ response.buffer = true
response.write "<br>"
if i mod 100 = 0 then response.flush
next
response.write timer() - start
response.write round(timer() - start, 6)
response.write " s<br>"

set newJson = nothing
Expand Down