Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) Read/Write JSON with Binary Data such as JPEG FilesDemonstrates 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.
Use ChilkatAx-win32.pkg Procedure Test Variant vBd Handle hoBd Boolean iSuccess Handle hoJson1 String sJsonStr Handle hoJson2 Variant vBd2 Handle hoBd2 // 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 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.