Skip to content

feat(a2ui_message): make toJson compatible with fromJson#780

Open
matiasleyba wants to merge 2 commits intoflutter:mainfrom
matiasleyba:feat/a2ui-message-to-json
Open

feat(a2ui_message): make toJson compatible with fromJson#780
matiasleyba wants to merge 2 commits intoflutter:mainfrom
matiasleyba:feat/a2ui-message-to-json

Conversation

@matiasleyba
Copy link

@matiasleyba matiasleyba commented Mar 1, 2026

Description

  • Each A2uiMessage subclass’s toJson() now returns the wrapped envelope (version + one of createSurface / updateComponents / updateDataModel / deleteSurface) instead of a flat payload.
  • This matches the shape that A2uiMessage.fromJson() expects, so round-trip works.

Why

  • Use case: Persisting messages locally (e.g. saving conversation history) and reloading with A2uiMessage.fromJson(saved).
  • Previously, fromJson(message.toJson()) failed because the root map had no action key.
  • Before: To save a message you had to switch on each type (CreateSurface, UpdateComponents, etc.) and build JSON per variant.
    • Now: You can call message.toJson() on any A2uiMessage and save without type checks.

  • CreateSurface, UpdateComponents, UpdateDataModel, and DeleteSurface now put their payload under the corresponding key.

  • Round-trip tests were added

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-devrel channel on Discord.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the toJson() methods for A2uiMessage subclasses to make them compatible with A2uiMessage.fromJson(), enabling round-trip serialization. The changes are well-implemented and include corresponding updates to tests and new round-trip tests. My feedback focuses on improving maintainability by using constants for repeated string literals like the API version and JSON keys, and a minor cleanup in the new test code.

/// Converts this message to a JSON map.
@override
Map<String, dynamic> toJson() => {
'version': 'v0.9',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The version string 'v0.9' is repeated across all toJson implementations and is also checked in A2uiMessage.fromJson. To improve maintainability and prevent potential inconsistencies when updating the version, consider defining this as a constant. For example, you could add a private constant at the top of this file: const String _kApiVersion = 'v0.9'; and use it in all relevant places.

Comment on lines +164 to +169
'createSurface': {
surfaceIdKey: surfaceId,
'catalogId': catalogId,
'theme': theme,
'sendDataModel': sendDataModel,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There are several string literals used as JSON keys here and in the corresponding fromJson factory (e.g., 'createSurface', 'catalogId', 'theme', 'sendDataModel'). To prevent typos and improve maintainability, it's a good practice to define these as constants, similar to how surfaceIdKey is used. This would make the code safer and easier to refactor.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant