Skip navigation
Your domain model is not JSON. It does not have to be.
0:00 / 1:11

Your domain model is not JSON. It does not have to be.

Giulio Canti

@GiulioCanti3.5K followers

Visit on X
200 likesDownloadOriginal post

19,126 views

Your domain model is not JSON. It does not have to be. In Effect v4, `Schema.toCodecJson` handles the bridge automatically, even for custom types. ↓

10 Comments

Comments from the original post on X (Twitter)

  • Sebastian Lorenz's profile picture

    @thefubhy

    It's weird that there is a Schema.Graph but not Schema.Cron

    11 likes

  • teej dv 🔭's profile picture

    @teej_dv

    is there a way to avoid calling json.parse in the examples? i can't remember what the "right" way is to do that there

    5 likes

  • Giulio Canti's profile picture

    @GiulioCanti

    Yes, you can compose it with Schema.fromJsonString const jsonStringCodec = Schema.fromJsonString( Schema.toCodecJson(WalletSnapshot) ) The same codec also encodes directly to a JSON string. I kept JSON.parse explicit only to distinguish JSON text from JSON safe data

    10 likes

  • teej dv 🔭's profile picture

    @teej_dv

    got it, that was the one i think i was missing. ty ty

    2 likes

  • Théo Pomies's profile picture

    @theopomies

    One day we'll have fullstack Effect and this will be one of the pillars

    3 likes

  • Stress and pressure's profile picture

    @iSnifer

    There are plenty methods that use it under the hood, so you don't need to call it manually: HttpBody.jsonSchema, HttpClientRequest.schemaBodyJson, HttpClientResponse.schemaJson, HttpClientResponse.schemaBodyJson, HttpServerRequest.schemaBodyJson, HttpServerResponse.schemaJson, HttpIncomingMessage.schemaBodyJson, etc

    2 likes

  • Andrea's profile picture

    @andreafspeziale

    Somehow recalls me the old good days of io-ts

    1 like

  • Buswe's profile picture

    @buswe_com

    Keeping the codec at the edge lets your domain model hold rich types like Date, bigint or branded ids and only flatten to JSON on the wire.

  • Patrick Roza's profile picture

    @patroza

    The one time it’s kind of a downside is when you have a document database query engine where you’re expected to pass in json values; as json codecs are untyped. Encoded in v3 worked well there. Then again, we can of course build the query api as Type or Encoded, and then use the json codec internally to translate

  • Anders Lie's profile picture

    @anderslie

    literal godsend. how does it handle optional / make sure no undefined is in the serialized form? current pain is defining Schema.optionalWith(..., { exact: true, as: "Option" }) everywhere to make sure undefined is omitted from keys entirely