Skip to content

Commit

Permalink
Add extra documentation for nullable (#140)
Browse files Browse the repository at this point in the history
* Add extra documentation for nullable

With smithy4s soon to support the @nullable trait
in codegen and JSON codecs, we add extra
documentation indicating that it is a trait which
@simpleRestJson supports and the expected
behaviour when it comes to serialization.

* Update docs/serialisation/json.md
  • Loading branch information
astridej authored Feb 26, 2024
1 parent 756fab3 commit 8a88372
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/protocols/SimpleRestJson.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The `alloy#simpleRestJson` protocol is a REST protocol in which all HTTP bodies
- alloy#uuidFormat
- alloy#discriminated
- alloy#untagged
- alloy#nullable

#### Protocol Behavior and Semantics

Expand Down Expand Up @@ -120,3 +121,4 @@ Furthermore, implementors of the protocol have to take into consideration additi
- `alloy#untagged`
- `alloy#discriminated`
- `alloy#uuidFormat`
- `alloy#nullable`
42 changes: 42 additions & 0 deletions docs/serialisation/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,45 @@ are encoded as such
{ "tpe": "first", "myString": "alloy" }
{ "tpe": "second", "myInt": 42 }
```

### Null values

The standard Smithy toolset does not provide any semantics for distinguishing between a JSON field being set to `null` and the same field being absent from its carrying JSON object. However, depending on the use-case, the difference can be meaningful. In order to support such use-cases, the additional trait `alloy.nullable` is provided. Annotating the member of a structure field with this indicates that a value serialised to `null` was a conscious decision (as opposed to omitting the value altogether), and that deserialisation should retain this information.

For example, assuming the following smithy structure

```smithy
use alloy#nullable
structure Foo {
@nullable
nullable: Integer
regular: Integer
}
```

The JSON objects

```json
{ "nullable": null, "regular": null }
{ "nullable": 4, "regular": 4 }
{}
```

are respectively decoded as follows in Scala (when using [smithy4s](https://disneystreaming.github.io/smithy4s/)):

```scala
Foo(Some(Nullable.Null), None)
Foo(Some(Nullable.Value(4)), Some(4))
Foo(None, None)
```

or some similar type which preserves the information that an explicit `null` was passed. These objects are in turn encoded as

```json
{ "nullable": null }
{ "nullable": 4, "regular": 4 }
{}
```

This means that `@nullable` allows round-tripping null values.

0 comments on commit 8a88372

Please sign in to comment.