• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and Functions
    • Text Templates
    • Templated Reporting System
    • HTML, Markdown, and JavaScript Integration
    • Action Invoker
    • Appendix
      • Appendix A - Installation
      • Appendix B - Keys Summary
      • Appendix C - Menu Summary
      • Appendix D - Toolbar Summary
      • Appendix E - Application Properties
      • Appendix F - Converting Within IMPS or ISSA
      • Appendix G - Errors in Censuses and Surveys
      • Appendix H - File Types
      • Appendix I - JSON Representations
        • Symbols
        • Case
        • JSON Serialization Options
        • CSPro ⇄ JSON Conversions: Binary Data
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataManager>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

CSPro ⇄ JSON Conversions: Binary Data

The value of defined binary logic object or dictionary item data is represented as a JSON object with "metadata" and "content" properties. If the object does not contain any data, its value is represented as null.
Serialization
Based on JSON serialization options—defined as an application property or dynamically—the value is serialized in one of two ways:
Localhost URL: The binary data is serialized as a localhost URL. Localhost URLs are only valid while the CSPro application is running. This is the default option, as it is an efficient way to write the data for use in web views. This value, serialized in the object "content" with the property "url", may look like:
{
 
"metadata": {
   
"path": "C:/texts/pangram.txt",
   
"filename": "pangram.txt",
   
"mime": "text/plain"
  },
 
"content": {
   
"url": "http://localhost:59421/vf/4/pangram.txt"
  }
}
Data URL: The binary data is serialized as a data URL encoded in Base64. Unlike a localhost URL, a data URL is not temporary so it is a good choice if you need a URL that can permanently refer to the data. Data URLs are approximately 33% larger than the content itself, so using data URLs for large files is not advised. This value, serialized in the object "content" with the property "url", may look like:
{
 
"metadata": {
   
"path": "C:/texts/pangram.txt",
   
"filename": "pangram.txt",
   
"mime": "text/plain"
  },
 
"content": {
   
"url": "data:text/plain;base64,VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZyAxMjM0NTY3ODkw"
  }
}
Copying and pasting that URL into a web browser's address bar will show the English pangram: "The quick brown fox jumps over the lazy dog 1234567890".
Deserialization
There are several ways to set binary data using JSON. If you want to clear a value, specifying the value as null will reset binary data to its default state. To set binary data to a specific value, you must supply an object with a "content" property and, optionally, a "metadata" property. Metadata is specified as an object with key/value pairs. The content itself can be specified in a couple ways:
Data URL: The binary data can be specified as a data URL using the "url" property. CSPro can parse data URLs encoded using Base64 or percent-encoding. For example:
string data_url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAACCAIAAAAW4yFwAAAAEElEQVR4nGP4z/CfgSH+BAANggMmwP+nGgAAAABJRU5ErkJggg==";

Image tiny_image;
tiny_image.
setValueFromJson(maketext("{ \"content\": { \"url\": \"%s\" } }", data_url));

// width = 1, height = 2
errmsg("width = %d, height = %d", tiny_image.width(), tiny_image.height());
File on Disk: The binary data can be specified as a file using the "path" property. The path is evaluated relative to the location of the application. For example:
string file_path = "european-robin-chirps.m4a";

Audio bird_sounds;
bird_sounds.
setValueFromJson(maketext("{ \"content\": { \"path\": \"%s\" } }", file_path));
Binary Objects
Some binary objects have additional ways that data can be supplied for deserialization. For more information about the serialization of binary data, see the documentation for each object:
  • CSPro ⇄ JSON Conversions: Audio Object
  • CSPro ⇄ JSON Conversions: Document Object
  • CSPro ⇄ JSON Conversions: Geometry Object
  • CSPro ⇄ JSON Conversions: Image Object
See also: JSON Representation of Symbols Overview