DEV Community

Peter + AI
Peter + AI

Posted on

πŸ“š Understanding Uniface 10.4's websave Statement: Converting Component Data to JSON

πŸ€– 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}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This code:

  1. Clears existing data 🧹
  2. Retrieves all ORDER records πŸ“‹
  3. Converts the data to JSON format πŸ”„
  4. 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)