Skip to content

Commit

Permalink
Clarify the JSON data referenced in patterns sample is deserialized (#…
Browse files Browse the repository at this point in the history
…6422)

1. Renamed the variable json to data throughout the section.
2. Updated the text to clarify that the example works with deserialized
data (e.g., parsed from JSON) rather than raw JSON strings.
3. Ensured consistency in variable naming and explanations.

This update makes the documentation more accurate and avoids misleading
readers into thinking that the variable holds a JSON string.

Resolves #6411

---------

Co-authored-by: Parker Lougheed <parlough@gmail.com>
  • Loading branch information
MiniPiku and parlough authored Feb 24, 2025
1 parent f8cc2c9 commit 7011009
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions examples/language/lib/patterns/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

void main() {
// #docregion json-1
var json = {
var data = {
'user': ['Lily', 13],
};
var {'user': [name, age]} = json;
var {'user': [name, age]} = data;
// #enddocregion json-1

{
// #docregion json-2
if (json is Map<String, Object?> &&
json.length == 1 &&
json.containsKey('user')) {
var user = json['user'];
if (data is Map<String, Object?> &&
data.length == 1 &&
data.containsKey('user')) {
var user = data['user'];
if (user is List<Object> &&
user.length == 2 &&
user[0] is String &&
Expand All @@ -27,7 +27,7 @@ void main() {
}
{
// #docregion json-3
if (json case {'user': [String name, int age]}) {
if (data case {'user': [String name, int age]}) {
print('User $name is $age years old.');
}
// #enddocregion json-3
Expand Down
16 changes: 8 additions & 8 deletions src/content/language/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,14 @@ double calculateArea(Shape shape) => switch (shape) {
### Validating incoming JSON

[Map][] and [list][] patterns work well for destructuring key-value pairs in
JSON data:
deserialized data, such as data parsed from JSON:

<?code-excerpt "language/lib/patterns/json.dart (json-1)"?>
```dart
var json = {
var data = {
'user': ['Lily', 13],
};
var {'user': [name, age]} = json;
var {'user': [name, age]} = data;
```

If you know that the JSON data has the structure you expect,
Expand All @@ -379,10 +379,10 @@ Without patterns, validation is verbose:

<?code-excerpt "language/lib/patterns/json.dart (json-2)"?>
```dart
if (json is Map<String, Object?> &&
json.length == 1 &&
json.containsKey('user')) {
var user = json['user'];
if (data is Map<String, Object?> &&
data.length == 1 &&
data.containsKey('user')) {
var user = data['user'];
if (user is List<Object> &&
user.length == 2 &&
user[0] is String &&
Expand All @@ -402,7 +402,7 @@ method of validating JSON:

<?code-excerpt "language/lib/patterns/json.dart (json-3)"?>
```dart
if (json case {'user': [String name, int age]}) {
if (data case {'user': [String name, int age]}) {
print('User $name is $age years old.');
}
```
Expand Down

0 comments on commit 7011009

Please sign in to comment.