Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse and create JSON with Chilkat ActiveX
#1
Setup

Download the 32-bit zip file from https://www.chilkatsoft.com/downloads_ActiveX.asp
Unzip the dll to the QM folder. Don't need other files.

Run this macro if need a manifest file for registration-free COM activation using __ComActivator.
Macro Chilkat create manifest
 
Code:
Copy      Help
;Creates manifest file for __ComActivator ca.Activate("$qm$\ChilkatAx-win32.X.manifest")

__ComActivator_CreateManifest "$qm$\ChilkatAx-win32.dll"

Examples

Macro Parse JSON
 
Code:
Copy      Help
;Uses Chilkat ActiveX.
;Docs: https://www.chilkatsoft.com/refdoc/xChilkatJsonObjectRef.html
;Download: https://www.chilkatsoft.com/downloads_ActiveX.asp
;JSONPath online evaluator and docs: https://jsonpath.com/

;__ComActivator ca.Activate("$qm$\ChilkatAx-win32.X.manifest") ;;enable this if want to use the library without COM registration, eg in exe. Note: QM anyway auto-registers when compiling.
typelib Chilkat "$qm$\ChilkatAx-win32.dll"

out

str json=
;{
;"one":"ONE",
;"two":2,
;"obj": {
;;"three":"THREE",
;;"four":"FOUR",
;;"bool":true
;},
;"arr":["A", "B", {"six":"SIX","seven":"SEVEN"}]
;}

ChilkatJsonObject j._create
j.Load(json)

out j.StringAt(0)
out j.IntAt(1)
out j.IntOf("two")
out j.StringOf(".obj.four")
out j.BoolOf(".obj.bool")
out j.FindObjectWithMember("three").StringOf("four")
out j.FindObjectWithMember("six").StringOf("seven")

;arrays:
out j.ArrayOf("arr").StringAt(1) ;;OK
;out j.StringOf(".arr[1]") ;;why fails?
out j.StringOf(".arr[1:2]") ;;OK
out j.StringOf(".arr[2:3].six") ;;OK
out j.ArrayOf("arr").ObjectAt(2).StringOf("seven") ;;OK

;if need a variable:
;IChilkatJsonArray a=j.ArrayOf("arr"); out a.StringAt(1)
;IChilkatJsonObject o=j.ArrayOf("arr").ObjectAt(2); out o.StringOf("six")

;the functions don't throw exceptions but set last error:
if(!j.LastMethodSuccess) out j.LastErrorText

Macro Create JSON
Code:
Copy      Help
;Uses Chilkat ActiveX.
;Docs: https://www.chilkatsoft.com/refdoc/xChilkatJsonObjectRef.html
;Download: https://www.chilkatsoft.com/downloads_ActiveX.asp

;__ComActivator ca.Activate("$qm$\ChilkatAx-win32.X.manifest") ;;enable this if want to use the library without COM registration, eg in exe. Note: QM anyway auto-registers when compiling.
typelib Chilkat "$qm$\ChilkatAx-win32.dll"

out

ChilkatJsonObject j._create
j.AppendString("one" "ONE")
j.AppendInt("two" 2)
IChilkatJsonObject o=j.AppendObject("obj")
o.AppendString("three" "THREE")
o.AppendBool("four" 1)
IChilkatJsonArray a=j.AppendArray("arr")
a.AddStringAt(-1 "A")
a.AddStringAt(-1 "B")
a.AddObjectAt(-1)
a.ObjectAt(a.size-1).AppendString("C" "CC")
a.ObjectAt(a.size-1).AppendBool("D" 1)

j.EmitCompact=0
str json=j.Emit()
out json
#2
Thank you for your sharing.

I didn't go through any registration process—just copied ChilkatAx-win32.dll to the QM installation directory, then ran the sample code, and it worked perfectly. The test was successful.

I remember that COM components require a certain form of registration, such as running a BAT registration script. However, I really didn't do anything, which is a bit strange.

I just tested it successfully on a brand new Windows 7 virtual machine as well, and it still didn't require any registration.
#3
QM auto-registers used COM dlls when it compiles the script. And notifies about it in the Output panel, but the script clears the output text.
To test unregistered (with or without the __ComActivator line):
1. Click Run or Compile, to make sure that next time QM will not compile again.
2. Run $qm$\unregister.bat. It's in the Chilkat zip.
3. Run the macro. Without the __ComActivator line it should fail (error). With the __ComActivator line should succeed.
#4
Thank you very much for your explanation.
If I don't package the code into an EXE, it's actually very easy to get started using DLL components in QM.


Forum Jump:


Users browsing this thread: 1 Guest(s)