Skip to content

Question about JSON serialization/deserialization #22

@ethindp

Description

@ethindp

I'm thinking about writing a schema crate for the Matrix specification. The problem is that the Matrix specification is quite dynamic: the JSON content contains a "content" object that changes depending on the event in question. For example, when an m.identity_server event is received, the JSON might look like this:

{
  "content": {
    "base_url": "https://example.org"
  },
  "type": "m.identity_server"
}

Whereas if the event type is m.room.member, the JSON is significantly different and has far more fields:

{
  "content": {
    "membership": "join"
  },
  "event_id": "$26RqwJMLw-yds1GAH_QxjHRC1Da9oasK0e5VLnck_45",
  "origin_server_ts": 1632489532305,
  "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  "sender": "@example:example.org",
  "state_key": "@user:example.org",
  "type": "m.room.member",
  "unsigned": {
    "age": 1567437,
    "redacted_because": {
      "content": {
        "reason": "spam"
      },
      "event_id": "$Nhl3rsgHMjk-DjMJANawr9HHAhLg4GcoTYrSiYYGqEE",
      "origin_server_ts": 1632491098485,
      "redacts": "$26RqwJMLw-yds1GAH_QxjHRC1Da9oasK0e5VLnck_45",
      "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
      "sender": "@moderator:example.org",
      "type": "m.room.redaction",
      "unsigned": {
        "age": 1257
      }
    }
  }
}

As you can see, the JSON can contain multiple events within one event classification, and I'm wondering how I could handle that. I'd use a library like JWX but I need to be able to both generate and parse JSON. (You can find many more examples of the Matrix JSON schema here.

So, I'm wondering how I might define this using utilada? I like the idea of being able to strongly type my data -- it makes things a lot easier to debug later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions