Lotusscript : Json formatter… enjoy


Hello, the following class allows you to format easily your return to your AJAX requests in JSON.


'--------------------------------------------------
'Calling code
'--------------------------------------------------

Dim sJson As New cJSON

Call sJson.addItem("myParameter1", True)
Call sJson.addItem("myParameter2", "this is a value")
Call sJson.addItem("myNumericPara", 2324)
Call sJson.addItem("myJSONStruct", sJson)

Msgbox sJson.getJSONWithHeader()

————————————————–

Calling code

————————————————–

Dim sJson As New cJSON

Call sJson.addItem(“myParameter1”, True)
Call sJson.addItem(“myParameter2”, “this is a value”)
Call sJson.addItem(“myNumericPara”, 2324)
Call sJson.addItem(“myJSONStruct”, sJson)

Msgbox sJson.getJSONWithHeader()

————————————————–

class code

————————————————–
‘/**——————————————————————————————
‘ * Object         cJSON
‘ * @author         Pierre Koerber
‘ * @version         1.0 – 24.07.2008
‘ * History :
‘ */——————————————————————————————
Class cJSON
Private lstItem List As String
Private newLine As String

Sub new
newLine = Chr(13) & Chr(10)
End Sub

Public Function getRandomString() As String
getRandomString = Replace(Cstr(Rnd(1)), “.”, “”)
End Function

Public Function addItem(sName As Variant, vValue As Variant) As Variant
Dim sJson As String

sJson = |%1:%2|
sJson = Replace(sJson, “%1”, sName)

Select Case Typename(vValue)
Case “CJSON”:
sJson = Replace(sJson, “%2”, vValue.getJSON())
Case “STRING”
sJson = Replace(sJson, “%2”, |”| + Cstr(vValue) + |”|)
Case “BOOLEAN”
If vValue = True Then
sJson = Replace(sJson, “%2”, “true”)
Else
sJson = Replace(sJson, “%2”, “false”)
End If
Case Else
sJson = Replace(sJson, “%2”, Cstr(vValue))
End Select

lstItem(getRandomString) = sJson
End Function

Public Function getJSON() As String
Dim i As Integer
Dim sRes As String
i = 0
sRes = “{“
Forall x In  lstItem
If i=0 Then
sRes = sRes + x
Else
sRes = sRes + “,” + x
End If
i = i + 1
End Forall

getJSON = sRes + “}”
End Function

Public Function getJSONWithHeader() As String
Dim sRet As String
sRet = |Content-Type: text/javascript; charset=UTF-8| + newLine
sRet = sRet + |Cache-Control: no-cache| + newLine
sRet = sRet + getJSON()
getJSONWithHeader = sRet
End Function

End Class

Advertisements

About pierrekoerber
Hey, pour faire connaissance, laisse-moi un message... drop me a line.

9 Responses to Lotusscript : Json formatter… enjoy

  1. Pingback: Lotusscript : Json formatter… enjoy « Lotus Notes — рабочий инструмент программиста.

  2. Pingback: Lotusscript : Json formatter… enjoy « Lotus Notes — рабочий инструмент программиста.

  3. Pingback: Lotusscript : Json formatter… enjoy « Lotus Notes — рабочий инструмент программиста.

  4. Pingback: Lotusscript : Json formatter… enjoy « Lotus Notes — рабочий инструмент программиста.

  5. Pingback: Lotusscript : Json formatter… enjoy « Lotus Notes — рабочий инструмент программиста.

  6. в итоге: отлично..

  7. Markus says:

    Great code! But some points: I think, that the JSON property name should be quoted, so I suggest to change the line

    sJson = Replace(sJson, “%1″, sName)

    to

    sJson = Replace(sJson, “%1”, |”| + sName + |”|)

    Also a string property value should be escaped. This makes line

    sJson = Replace(sJson, “%2″, |”| + Cstr(vValue) + |”|)

    to

    sJson = Replace(sJson, “%2”, |”| + EscapeJSONString(CStr(vValue)) + |”|)

    Here is the escape routine according to http://json.org:

    Function EscapeJSONString(ByVal aString as String) As String
    Dim i As Integer
    Dim c As String
    Dim a As Long
    Dim h As String

    ‘Escape special characters
    aString = Replace(aString, |\|, |\\|) ‘Escape backslash first(!)
    aString = Replace(aString, |”|, |\”|) ‘Escape quotes
    aString = Replace(aString, |/|, |\/|) ‘Escape slash
    aString = Replace(aString, Chr( 8), |\b|) ‘Escape backspace
    aString = Replace(aString, Chr(12), |\f|) ‘Escape formfeed
    aString = Replace(aString, Chr(10), |\l|) ‘Escape newline (linefeed)
    aString = Replace(aString, Chr(13), |\r|) ‘Escape carriage return
    aString = Replace(aString, Chr( 9), |\t|) ‘Escape horizontal tab

    ‘Escape any other control characters (0..31)
    EscapeJSONString = “”

    For i = 1 To Len(aString)
    c = Mid$(aString, i, 1)
    a = Asc(c)

    If a < 32 Then
    h = Hex$(a)

    'Make 4 digits
    While Len (h) < 4
    h = "0" + h
    Wend

    EscapeJSONString = EscapeJSONString + |\u| + h
    Else
    EscapeJSONString = EscapeJSONString + c
    End If
    Next i

    End Function

    As a last point I would like to mention, that parameter sName could be of type string.

    Thanks for the code so far.
    Regards, Markus

  8. Pierre Koerber says:

    Hello, you’re right… I’ve already improved the class with the quote in the identifier. Thank you for the
    EscapeJSONString() function it’s perfect !

    Greeting from Switzerland

  9. Birkmaier says:

    Understanding with regard to genuinely getting truly considerate plus with regard to deciding on certain splendid instructions many people are definitely wanting to learn.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: