Skip to content

Commit 5313b64

Browse files
committed
Merge pull request #11 from timhall/json-parse-duplicates
Handle duplicate keys when parsing json
2 parents 640b3e1 + 666eecd commit 5313b64

12 files changed

+29
-15
lines changed

Excel-REST - Blank.xlsm

-16.7 KB
Binary file not shown.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ For more details, check out the [Wiki](https://github.com/timhall/Excel-REST/wik
106106
- Add RestClientBase for future use with extension for single-client applications
107107
- Add build scripts for import/export
108108
- New specs and bugfixes
109+
- __v2.0.1__ Handle duplicate keys when parsing json
109110

110111
#### v1.1.0
111112

build/import.vbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Else
1919
End If
2020

2121
' Include all standard Excel-REST modules
22-
Modules = Array("RestHelpers.bas", "IAuthenticator.cls", "RestClient.cls", "RestRequest.cls", "RestResponse.cls")
22+
Modules = Array("RestHelpers.bas", "IAuthenticator.cls", "RestClient.cls", "RestRequest.cls", "RestResponse.cls", "RestClientBase.bas")
2323

2424
' Open Excel
2525
KeepExcelOpen = OpenExcel(Excel)

examples/Excel-REST - Example.xlsm

-15 KB
Binary file not shown.

specs/Excel-REST - Specs.xlsm

38.6 KB
Binary file not shown.

specs/RestHelpersSpecs.bas

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ Public Function Specs() As SpecSuite
5151
End If
5252
End With
5353

54+
With Specs.It("should overwrite parsed json for duplicate keys")
55+
json = "{""a"":1,""a"":2,""a"":3}"
56+
Set Parsed = RestHelpers.ParseJSON(json)
57+
58+
.Expect(Parsed).ToBeDefined
59+
If Not Parsed Is Nothing Then
60+
.Expect(Parsed("a")).ToEqual 3
61+
End If
62+
End With
63+
5464
With Specs.It("should convert to json")
5565
Set Obj = CreateObject("Scripting.Dictionary")
5666
Obj.Add "a", 1

src/IAuthenticator.cls

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Attribute VB_Creatable = False
88
Attribute VB_PredeclaredId = False
99
Attribute VB_Exposed = True
1010
''
11-
' IAuthenticator v2.0.0
11+
' IAuthenticator v2.0.1
1212
' (c) Tim Hall - https://github.com/timhall/Excel-REST
1313
'
1414
' Interface for creating authenticators for rest client

src/RestClient.cls

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Attribute VB_Creatable = False
88
Attribute VB_PredeclaredId = False
99
Attribute VB_Exposed = True
1010
''
11-
' RestClient v2.0.0
11+
' RestClient v2.0.1
1212
' (c) Tim Hall - https://github.com/timhall/Excel-REST
1313
'
1414
' Interact with REST web services from Excel
@@ -19,7 +19,7 @@ Attribute VB_Exposed = True
1919
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '
2020
Option Explicit
2121

22-
Private Const UserAgent As String = "Excel Client v2.0.0 (https://github.com/timhall/Excel-REST)"
22+
Private Const UserAgent As String = "Excel Client v2.0.1 (https://github.com/timhall/Excel-REST)"
2323
Private Const DefaultTimeoutMS As Integer = 5000
2424

2525

src/RestClientBase.bas

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Attribute VB_Name = "RestClientBase"
22
''
3-
' RestClientBase v2.0.0
3+
' RestClientBase v2.0.1
44
' (c) Tim Hall - https://github.com/timhall/Excel-REST
55
'
66
' Extendable RestClientBase for developing custom client classes
@@ -15,7 +15,7 @@ Attribute VB_Name = "RestClientBase"
1515
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '
1616
Option Explicit
1717

18-
Private Const UserAgent As String = "Excel Client v2.0.0 (https://github.com/timhall/Excel-REST)"
18+
Private Const UserAgent As String = "Excel Client v2.0.1 (https://github.com/timhall/Excel-REST)"
1919
Private Const TimeoutMS As Integer = 5000
2020
Private Initialized As Boolean
2121

src/RestHelpers.bas

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Attribute VB_Name = "RestHelpers"
22
''
3-
' RestHelpers v2.0.0
3+
' RestHelpers v2.0.1
44
' (c) Tim Hall - https://github.com/timhall/Excel-REST
55
'
66
' Common helpers RestClient
@@ -398,6 +398,7 @@ End Function
398398
' - Updated json_parseNumber to reduce chance of overflow
399399
' - Swapped Mid for Mid$
400400
' - Handle colon in object key
401+
' - Handle duplicate keys in object parsing
401402
' - Change methods to Private and prefix with json_
402403
'
403404
' ======================================================================================== '
@@ -442,8 +443,9 @@ Private Function json_parseObject(ByRef str As String, ByRef index As Long) As O
442443
If Mid$(str, index, 1) <> "{" Then Err.Raise vbObjectError + INVALID_OBJECT, Description:="char " & index & " : " & Mid$(str, index)
443444
index = index + 1
444445

445-
Do
446+
Dim Key As String
446447

448+
Do
447449
Call json_skipChar(str, index)
448450
If "}" = Mid$(str, index, 1) Then
449451
index = index + 1
@@ -453,11 +455,12 @@ Private Function json_parseObject(ByRef str As String, ByRef index As Long) As O
453455
Call json_skipChar(str, index)
454456
End If
455457

456-
Dim Key As String
457-
458-
' add key/value pair
459-
json_parseObject.Add Key:=json_parseKey(str, index), Item:=json_parseValue(str, index)
460-
458+
Key = json_parseKey(str, index)
459+
If Not json_parseObject.Exists(Key) Then
460+
json_parseObject.Add Key, json_parseValue(str, index)
461+
Else
462+
json_parseObject.Item(Key) = json_parseValue(str, index)
463+
End If
461464
Loop
462465

463466
End Function

src/RestRequest.cls

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Attribute VB_Creatable = False
88
Attribute VB_PredeclaredId = False
99
Attribute VB_Exposed = True
1010
''
11-
' RestRequest v2.0.0
11+
' RestRequest v2.0.1
1212
' (c) Tim Hall - https://github.com/timhall/Excel-REST
1313
'
1414
' Create a request for use with a rest client

src/RestResponse.cls

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Attribute VB_Creatable = False
88
Attribute VB_PredeclaredId = False
99
Attribute VB_Exposed = True
1010
''
11-
' RestResponse v2.0.0
11+
' RestResponse v2.0.1
1212
' (c) Tim Hall - https://github.com/timhall/Excel-REST
1313
'
1414
' Wrapper for http responses

0 commit comments

Comments
 (0)