DataFlex
DataFlex
Read/Write JSON with Binary Data such as JPEG Files
See more JSON Examples
Demonstrates how binary files could be stored in JSON in base64 format. Creates JSON containing the contents of a JPG file, and then reads the JSON to extract the JPEG image.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Variant vBd
Handle hoBd
Handle hoJson1
String sJsonStr
Handle hoJson2
Variant vBd2
Handle hoBd2
Move False To iSuccess
// First load a small JPG file..
Get Create (RefClass(cComChilkatBinData)) To hoBd
If (Not(IsComObjectCreated(hoBd))) Begin
Send CreateComObject of hoBd
End
Get ComLoadFile Of hoBd "qa_data/jpg/starfish20.jpg" To iSuccess
// Assume success, but your code should check for success..
// Create JSON containing the binary data in base64 format.
Get Create (RefClass(cComChilkatJsonObject)) To hoJson1
If (Not(IsComObjectCreated(hoJson1))) Begin
Send CreateComObject of hoJson1
End
Get pvComObject of hoBd to vBd
Get ComUpdateBd Of hoJson1 "starfish" "base64" vBd To iSuccess
Get ComEmit Of hoJson1 To sJsonStr
Showln sJsonStr
// Here's the output:
// {"starfish":"/9j/4AAQSkZJRgA ... cN2iuLFsCEbDGxQkI6RO/n//2Q=="}
// Let's create a new JSON object, load it with the above JSON, and extract the JPG image..
Get Create (RefClass(cComChilkatJsonObject)) To hoJson2
If (Not(IsComObjectCreated(hoJson2))) Begin
Send CreateComObject of hoJson2
End
Get ComLoad Of hoJson2 sJsonStr To iSuccess
// Get the binary bytes.
Get Create (RefClass(cComChilkatBinData)) To hoBd2
If (Not(IsComObjectCreated(hoBd2))) Begin
Send CreateComObject of hoBd2
End
Get pvComObject of hoBd2 to vBd2
Get ComBytesOf Of hoJson2 "starfish" "base64" vBd2 To iSuccess
// Save to a file.
Get ComWriteFile Of hoBd2 "qa_output/starfish20.jpg" To iSuccess
Showln "Success."
End_Procedure