Skip to content

Option to convert dates (also convert back to ISO in ParseJson) #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 11 additions & 2 deletions JsonConverter.bas
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ Private Type json_Options

' The solidus (/) is not required to be escaped, use this option to escape them as \/ in ConvertToJson
EscapeSolidus As Boolean

'before version 2.3.1 dates where converted to UTC in ConvertToJson method, but not when json was parsed.
'Convert datetime values to UTC/ISO (false, slower) or dont change local <-> global times (true, faster)
DontConvertDates As Boolean
End Type
Public JsonOptions As json_Options

Expand Down Expand Up @@ -230,8 +234,11 @@ Public Function ConvertToJson(ByVal JsonValue As Variant, Optional ByVal Whitesp
ConvertToJson = "null"
Case VBA.vbDate
' Date
json_DateStr = ConvertToIso(VBA.CDate(JsonValue))

If Not DontConvertDates Then
json_DateStr = ConvertToIso(VBA.CDate(JsonValue))
Else
json_DateStr = VBA.CDate(JsonValue)
End If
ConvertToJson = """" & json_DateStr & """"
Case VBA.vbString
' String (or large number encoded as string)
Expand Down Expand Up @@ -592,6 +599,8 @@ Private Function json_ParseString(json_String As String, ByRef json_Index As Lon
End Select
Case json_Quote
json_ParseString = json_BufferToString(json_Buffer, json_BufferPosition)
'only test for same ISO format in ConvertToIso method
If Not DontConvertDates Then If json_ParseString Like "####-##-##T##:##:##.###Z" Then json_ParseString = ParseIso(json_ParseString)
json_Index = json_Index + 1
Exit Function
Case Else
Expand Down