diff --git a/JSONItem_MTC Harness/JSONItem_MTC Harness.xojo_project b/JSONItem_MTC Harness/JSONItem_MTC Harness.xojo_project index 7123aaf..06048c4 100644 --- a/JSONItem_MTC Harness/JSONItem_MTC Harness.xojo_project +++ b/JSONItem_MTC Harness/JSONItem_MTC Harness.xojo_project @@ -1,5 +1,5 @@ Type=Desktop -RBProjectVersion=2018.02 +RBProjectVersion=2018.04 MinIDEVersion=20070100 Class=App;App.xojo_code;&h0000000014C611D3;&h0000000000000000;false MenuBar=MenuBar1;MenuBar1.xojo_menu;&h0000000018745784;&h0000000000000000;false diff --git a/JSONItem_MTC Harness/M_JSON.xojo_code b/JSONItem_MTC Harness/M_JSON.xojo_code index 835c84d..f40fc2f 100644 --- a/JSONItem_MTC Harness/M_JSON.xojo_code +++ b/JSONItem_MTC Harness/M_JSON.xojo_code @@ -1310,7 +1310,7 @@ Protected Module M_JSON #tag Constant, Name = kTab, Type = Double, Dynamic = False, Default = \"9", Scope = Private #tag EndConstant - #tag Constant, Name = kVersion, Type = Double, Dynamic = False, Default = \"4.0", Scope = Protected + #tag Constant, Name = kVersion, Type = Double, Dynamic = False, Default = \"4.1", Scope = Protected #tag EndConstant diff --git a/JSONItem_MTC Harness/M_JSON/JSONDictionary.xojo_code b/JSONItem_MTC Harness/M_JSON/JSONDictionary.xojo_code index 6d0ffc9..46b26cb 100644 --- a/JSONItem_MTC Harness/M_JSON/JSONDictionary.xojo_code +++ b/JSONItem_MTC Harness/M_JSON/JSONDictionary.xojo_code @@ -2,23 +2,17 @@ Protected Class JSONDictionary Inherits Dictionary #tag Method, Flags = &h0 - Function HasKey(key As Variant) As Boolean - if key.Type = Variant.TypeString then - key = EncodeHex( key.StringValue ) - end if - + Function HasKey(name As Variant) As Boolean + dim key as variant = NameToKey( name ) return super.HasKey( key ) End Function #tag EndMethod #tag Method, Flags = &h0 Function Key(index As Integer) As Variant - dim k as variant = super.Key( index ) - if k.Type = Variant.TypeString then - k = DecodeHex( k.StringValue ) - end if - - return k + dim key as variant = super.Key( index ) + dim name as string = KeyToName( key ) + return name End Function #tag EndMethod @@ -29,31 +23,42 @@ Inherits Dictionary for i as integer = 0 to rawKeys.Ubound dim thisKey as variant = rawKeys( i ) - if thisKey.Type = Variant.TypeString then - rawKeys( i ) = DecodeHex( thisKey.StringValue ) - end if + rawKeys( i ) = KeyToName( thisKey ) next return rawKeys End Function #tag EndMethod - #tag Method, Flags = &h0 - Function Lookup(key As Variant, defaultValue As Variant) As Variant - if key.Type = Variant.TypeString then - key = EncodeHex( key.StringValue ) - end if + #tag Method, Flags = &h21 + Private Function KeyToName(key As Variant) As String + dim s as string = key.StringValue + dim hex as string = s.NthField( "-", s.CountFields( "-" ) ) + dim name as string = s.LeftB( s.LenB - hex.LenB - 1 ) + return name - return super.Lookup( key, defaultValue ) End Function #tag EndMethod #tag Method, Flags = &h0 - Sub Remove(key As Variant) - if key.Type = Variant.TypeString then - key = EncodeHex( key.StringValue ) - end if + Function Lookup(name As Variant, defaultValue As Variant) As Variant + dim key as variant = NameToKey( name ) + return super.Lookup( key, defaultValue ) + End Function + #tag EndMethod + + #tag Method, Flags = &h21 + Private Function NameToKey(name As String) As Variant + name = name.ConvertEncoding( Encodings.UTF8 ) + dim key as variant = name + "-" + EncodeHex( name ) + return key + End Function + #tag EndMethod + + #tag Method, Flags = &h0 + Sub Remove(name As Variant) + dim key as variant = NameToKey( name ) super.Remove( key ) End Sub @@ -75,76 +80,20 @@ Inherits Dictionary #tag EndMethod #tag Method, Flags = &h0 - Function Value(key As Variant) As Variant - if key.Type = Variant.TypeString then - key = EncodeHex( key.StringValue ) - end if - + Function Value(name As Variant) As Variant + dim key as variant = NameToKey( name ) return super.Value( key ) End Function #tag EndMethod #tag Method, Flags = &h0 - Sub Value(key As Variant, Assigns v As Variant) - if key.Type = Variant.TypeString then - key = EncodeHex( key.StringValue ) - end if - + Sub Value(name As Variant, Assigns v As Variant) + dim key as variant = NameToKey( name ) super.Value( key ) = v End Sub #tag EndMethod - #tag ComputedProperty, Flags = &h21 - #tag Getter - Get - dim rawKeys() as variant = super.Keys - dim rawKeysString() as string - - // - // Remove the non-strings and get some stats - // - dim longest as integer - dim shortest as integer = &h7FFFFFFF - - for i as integer = 0 to rawKeys.Ubound - dim thisKey as variant = rawKeys( i ) - - if thisKey.Type = Variant.TypeString then - rawKeysString.Append thisKey.StringValue - - dim thisLen as integer = thisKey.StringValue.LenB - longest = max( longest, thisLen ) - shortest = min( shortest, thisLen ) - end if - next - - rawKeysString.Sort - - dim padder as string = " " - while padder.LenB < longest - padder = padder + padder - wend - - dim builder() as string - - for i as integer = 0 to rawKeysString.Ubound - dim thisKey as string = rawKeysString( i ) - - dim row as string = padder + thisKey + " = " - row = row.Right( longest + 3 ) - row = row + DecodeHex( thisKey ) - builder.Append row - next - - dim r as string = join( builder, EndOfLine ) - return r - End Get - #tag EndGetter - Private DebugKeyMap As String - #tag EndComputedProperty - - #tag ViewBehavior #tag ViewProperty Name="BinCount" diff --git a/JSONItem_MTC Harness/Tests/BasicTests_JSONItem.xojo_code b/JSONItem_MTC Harness/Tests/BasicTests_JSONItem.xojo_code index d3e7dae..bcc778c 100644 --- a/JSONItem_MTC Harness/Tests/BasicTests_JSONItem.xojo_code +++ b/JSONItem_MTC Harness/Tests/BasicTests_JSONItem.xojo_code @@ -4,13 +4,40 @@ Inherits TestGroup #tag Method, Flags = &h21 Private Sub CaseSensitiveKeyTest() dim j as new JSONItem - j.Value( "a" ) = 1 - j.Value( "A" ) = 2 - Assert.AreEqual( 2, j.Count, "Should be 2 objects" ) + dim storedKeys() as string = array( _ + "a", _ + "A", _ + "a" + &u200B + "A", _ + "A" + &u200B + "A", _ + "a" + &u200B + "a" _ + ) + + for i as integer = 0 to storedKeys.Ubound + dim key as string = storedKeys( i ) + j.Value( key ) = i + 1 + next + + Assert.AreEqual( CType( storedKeys.Ubound, integer ) + 1, j.Count, "Should be 5 objects" ) Assert.AreEqual( 1, j.Value( "a" ).IntegerValue ) - j.Value( "Man" ) = 3 + dim keys() as string = j.Names + + for each storedKey as string in storedKeys + dim startingUb as integer = keys.Ubound + for i as integer = keys.Ubound downto 0 + if StrComp( keys( i ), storedKey, 0 ) = 0 then + keys.Remove i + exit for i + end if + next + dim endingUb as integer = keys.Ubound + Assert.AreEqual( startingUb - 1, endingUb, storedKey.ToText + " was not found" ) + next + + Assert.AreEqual( -1, CType( keys.Ubound, integer ), "keys should be empty" ) + + j.Value( "Man" ) = 6 Assert.IsFalse( j.HasName( "MaT" ), "Keys with same Base64 encoding return incorrect results" ) End Sub @@ -206,6 +233,16 @@ Inherits TestGroup #tag ViewBehavior + #tag ViewProperty + Name="IsRunning" + Group="Behavior" + Type="Boolean" + #tag EndViewProperty + #tag ViewProperty + Name="StopTestOnFail" + Group="Behavior" + Type="Boolean" + #tag EndViewProperty #tag ViewProperty Name="Duration" Group="Behavior" diff --git a/JSONItem_MTC Harness/Tests/BasicTests_JSONItem_MTC.xojo_code b/JSONItem_MTC Harness/Tests/BasicTests_JSONItem_MTC.xojo_code index afe8357..c128e0a 100644 --- a/JSONItem_MTC Harness/Tests/BasicTests_JSONItem_MTC.xojo_code +++ b/JSONItem_MTC Harness/Tests/BasicTests_JSONItem_MTC.xojo_code @@ -67,15 +67,43 @@ Inherits TestGroup #tag Method, Flags = &h21 Private Sub CaseSensitiveKeyTest() dim j as new JSONItem_MTC - j.Value( "a" ) = 1 - j.Value( "A" ) = 2 - Assert.AreEqual( 2, j.Count, "Should be 2 objects" ) + dim storedKeys() as string = array( _ + "a", _ + "A", _ + "a" + &u200B + "A", _ + "A" + &u200B + "A", _ + "a" + &u200B + "a" _ + ) + + for i as integer = 0 to storedKeys.Ubound + dim key as string = storedKeys( i ) + j.Value( key ) = i + 1 + next + + Assert.AreEqual( CType( storedKeys.Ubound, integer ) + 1, j.Count, "Should be 5 objects" ) Assert.AreEqual( 1, j.Value( "a" ).IntegerValue ) - j.Value( "Man" ) = 3 + dim keys() as string = j.Names + + for each storedKey as string in storedKeys + dim startingUb as integer = keys.Ubound + for i as integer = keys.Ubound downto 0 + if StrComp( keys( i ), storedKey, 0 ) = 0 then + keys.Remove i + exit for i + end if + next + dim endingUb as integer = keys.Ubound + Assert.AreEqual( startingUb - 1, endingUb, storedKey.ToText + " was not found" ) + next + + Assert.AreEqual( -1, CType( keys.Ubound, integer ), "keys should be empty" ) + + j.Value( "Man" ) = 6 Assert.IsFalse( j.HasName( "MaT" ), "Keys with same Base64 encoding return incorrect results" ) + End Sub #tag EndMethod @@ -615,6 +643,16 @@ Inherits TestGroup #tag ViewBehavior + #tag ViewProperty + Name="IsRunning" + Group="Behavior" + Type="Boolean" + #tag EndViewProperty + #tag ViewProperty + Name="StopTestOnFail" + Group="Behavior" + Type="Boolean" + #tag EndViewProperty #tag ViewProperty Name="Duration" Group="Behavior" diff --git a/JSONItem_MTC Harness/Tests/M_JSONTests.xojo_code b/JSONItem_MTC Harness/Tests/M_JSONTests.xojo_code index a4f599d..2b126c7 100644 --- a/JSONItem_MTC Harness/Tests/M_JSONTests.xojo_code +++ b/JSONItem_MTC Harness/Tests/M_JSONTests.xojo_code @@ -137,6 +137,22 @@ Inherits TestGroup End Sub #tag EndMethod + #tag Method, Flags = &h0 + Sub CaseSensitiveNamesTest() + dim s as string = "{""Abc"":1, ""ABC"":2, ""abc"":3}" + + dim d as Dictionary = ParseJSON_MTC( s ) + Assert.IsTrue( Introspection.GetType( d ) = GetTypeInfo( M_JSON.JSONDictionary ), "Is not JSONDictionary" ) + + Assert.AreEqual( 3, d.Count, "Dictionary.Count does not match" ) + Assert.AreEqual( 1, d.Value( "Abc" ).IntegerValue, "Abc" ) + Assert.AreEqual( 2, d.Value( "ABC" ).IntegerValue, "ABC" ) + Assert.AreEqual( 3, d.Value( "abc" ).IntegerValue, "abc" ) + + + End Sub + #tag EndMethod + #tag Method, Flags = &h0 Sub DifferentEncodingsTest() dim json as string = "[1,2,""©""]" @@ -283,6 +299,18 @@ Inherits TestGroup End Sub #tag EndMethod + #tag Method, Flags = &h0 + Sub JSONDictionaryTextKeyTest() + dim t as text = "someTextKey" + dim s as string = t + + dim d as new M_JSON.JSONDictionary + + d.Value( t ) = 1 + Assert.AreEqual( 1, d.Value( s ).IntegerValue, "Text key does not match" ) + End Sub + #tag EndMethod + #tag Method, Flags = &h0 Sub JSONDIctionaryToDictionaryTest() dim jd as M_JSON.JSONDictionary = ParseJSON_MTC( "{""aa"" : 1, ""AA"" : 2, ""Aa"" : 3}" ) diff --git a/JSONItem_MTC.xojo_xml_code b/JSONItem_MTC.xojo_xml_code index 26cd7fd..3fd1d58 100644 --- a/JSONItem_MTC.xojo_xml_code +++ b/JSONItem_MTC.xojo_xml_code @@ -1,5 +1,5 @@ - + JSONItem_MTC 0 @@ -1758,9 +1758,9 @@ 134217984 Function NameToKey(name As String) As String - dim r as string = name.ConvertEncoding( Encodings.UTF8 ) - r = EncodeHex( r ) - return r + name = name.ConvertEncoding( Encodings.UTF8 ) + dim key as string = name + "-" + EncodeHex( name ) + return key End Function @@ -1778,17 +1778,17 @@ 826953727 134217984 - Function KeyToName(name As String) As String - dim r as string = DecodeHex( name ) - r = r.DefineEncoding( Encodings.UTF8 ) - return r + Function KeyToName(key As String) As String + dim hex as string = key.NthField( "-", key.CountFields( "-" ) ) + dim name as string = key.LeftB( key.LenB - hex.LenB - 1 ) + return name End Function 134217984 33 0 - name As String + key As String String @@ -1971,7 +1971,7 @@ end if value = DecodeValue( inMB, pos, current, outMB ) - key = EncodeHex( name ) + key = NameToKey( name ) current.ObjectValues.Value( key ) = value if value IsA JSONItem_MTC then @@ -2862,7 +2862,7 @@ 1097842687 134217984 2 - 4.0 + 4.1 0 diff --git a/README.md b/README.md index a43ecd8..dbf627f 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,10 @@ This project was created by and is maintained by Kem Tekinay (ktekinay@mactechno # Release Notes +**4.1** (Jan. 23, 2019) + +- JSON objects in both JSONItem\_MTC and the JSONDictionary will store keys internally as key-HEX instead of just hex for easier debugging. + **4.0** (Sept. 12, 2018) - Added M\_JSON module.