diff --git a/jsonObject.class.asp b/jsonObject.class.asp index 7739a1a..78ba855 100644 --- a/jsonObject.class.asp +++ b/jsonObject.class.asp @@ -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 @@ -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 @@ -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) @@ -596,7 +608,7 @@ class JSONobject found = false i = 0 - + do while i < i_properties_count set p = i_properties(i) @@ -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 @@ -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 @@ -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") @@ -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 @@ -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 @@ -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 diff --git a/test.asp b/test.asp index e82ac05..941f708 100644 --- a/test.asp +++ b/test.asp @@ -31,9 +31,9 @@ response.buffer = true

JSON Object and Array Tests

<% 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 @@ -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 + + %>

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

<% + 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 & "]" %>

Parse Input

<%= jsonString %>
<% response.flush - dim start start = timer() set outputObj = jsonObj.parse(jsonString) if testLoadArray then set jsonArr = outputObj - response.Write "Load time: " & (timer() - start) & " s
" + response.Write "Load time: " & round(timer() - start, 6) & " s
" end if if testAdd then @@ -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 @@ -122,7 +124,9 @@ response.buffer = true jsonObj.defaultPropertyName = "CustomName" jsonArr.defaultPropertyName = "CustomArrName" end if + + if testValue then %>

Get Values

<% response.write "nome: " & jsonObj.value("nome") & "
" @@ -174,14 +178,14 @@ 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 @@ -189,13 +193,13 @@ response.buffer = true 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 @@ -213,10 +217,10 @@ response.buffer = true if testLoad then start = timer() %> -

Parse Output

+

Parse Output<% if testLoadArray then %>: Array<% end if %>

<%= outputObj.write %>
<% - response.write timer() - start + response.write round(timer() - start, 6) response.write " s
" response.flush end if @@ -288,7 +292,7 @@ response.buffer = true response.write "
" if i mod 100 = 0 then response.flush next - response.write timer() - start + response.write round(timer() - start, 6) response.write " s
" set newJson = nothing