π€ This blog post was created with AI assistance to help developers understand Uniface's websave functionality.
π― What is websave?
The websave
statement in Uniface 10.4 is a powerful tool that converts data from a component into a JSON stream. Think of it as a data export function that takes all your component's information and packages it into JSON format for web applications.
JSON (JavaScript Object Notation) is a lightweight data format that's easy for both humans and computers to read. It's commonly used for transferring data between web applications.
βοΈ Basic Syntax
websave{/mod | /one}
The statement has two optional qualifiers (special options that modify behavior):
-
/mod
- Only includes data that has been changed π -
/one
- Only includes the current main record and its related data π
π How It Works
When you run websave
, it creates a complete snapshot of your component's data. This includes:
- All records in the hitlist (the complete set of data records)
- Both database and non-database fields
- Records marked for deletion
- Special metadata like CRC (data integrity check), ID, and STATUS
Occurrences are individual data records in Uniface - think of them as rows in a database table.
π Return Values
The websave
statement returns different values in $status
:
- Negative number: An error occurred β
- 0: Success! β
- Positive number: Partial success - shows how many image files couldn't be created πΈ
π Triggers and Customization
Triggers are special code blocks that run automatically during the websave process:
-
preSerialize
- Runs before each record is converted π -
postSerialize
- Runs after each record is converted β
These triggers let you customize the data conversion process, such as calculating derived fields or excluding certain records.
π‘ Practical Example
Here's a simple example that retrieves order data and converts it to JSON:
clear
retrieve/e "ORDER.INOUTER"
websave
putmess $webinfo("data")
return
This code:
- Clears existing data π§Ή
- Retrieves all ORDER records π
- Converts the data to JSON format π
- Displays the JSON in the message area π¬
πΌοΈ Handling Images
When your component contains images from a database, websave
creates temporary image files. The Uniface Server needs write permissions to the project directory for this to work properly. If it can't create these files, you'll see image error icons instead of the actual images.
π οΈ Development Tips
For easier debugging, you can use these settings to make your JSON more readable:
-
$JSON_INDENT
- Adds proper indentation π -
$JSON_SHOWNAMES
- Shows field names clearly π·οΈ
π± Usage Context
The websave
statement is primarily used in Dynamic Server Page components - these are Uniface components designed for web applications that generate content dynamically.
π Conclusion
The websave
statement is an essential tool for Uniface web developers. It provides a clean, efficient way to export component data as JSON, making it easy to integrate with modern web technologies and APIs. Whether you're building REST services or need to transfer data between different parts of your application, websave
offers the flexibility and control you need.
Remember to handle errors properly by checking the $status
value and ensure your server has proper file permissions when working with images! π
Top comments (0)