Proposal: Add tagged union to JSON Schema

742 views
Skip to first unread message

Ari Okkonen

unread,
Sep 1, 2020, 11:01:49 AM9/1/20
to JSON Schema
Tagged union is a way to define a data type that can have different but rigid structure for different purposes. It is useful e.g. in command lists to devices, and event reports from devices. Different commands for different actions require different kind but rigidly defined parameters, and different kinds of events produce different kind of data. Many programming languages support tagged unions (with different names e.g. discriminated unions). The Wikipedia article Tagged union describes the subject more thoroughly.

This is an example of a possible definition for variants:

{
  "type": "object",
  "tag": { 
    "myTag": {
      "type": "string", 
      "enum": ["move", "msg"] 
    }, 
  }, 
  "properties":{ 
    "id": { "type": "integer" } 
  }, 
  "variants":[ 
    { 
      "tagValue": "move", 
      "properties": {
        "x": { "type": "number" }, 
        "y": { "type": "number" } 
      } 
    }, 
    { 
      "tagValue": "msg", 
      "properties": { 
        "data": { "type": "string" } 
      } 
    } 
  ], 
  "additionalProperties": false 
}

This would pass the following records:
  { "myTag": "move", "id": "2780", "x": 758.32, "y": 334.28 } 
  { "myTag": "msg", "id": "2780", "data": "2A7F4A42B9022" }
but not:
  { "myTag": "move", "id": "2780", "data": "2A7F4A42B9022" } 


Ari Okkonen

Jason Desrosiers

unread,
Sep 4, 2020, 11:13:30 AM9/4/20
to JSON Schema
This google group isn't used any more. Head over to our github (https://github.com/json-schema-org/json-schema-spec) or slack (json-schema.slack.com) if you want to pursue this. But, the short answer is that this this has been proposed and rejected multiple times. The main reason is that the pattern is already achievable with `if`/`then`.

Ari Okkonen

unread,
Sep 8, 2020, 8:48:29 AM9/8/20
to json-...@googlegroups.com
Thank you. Anyway, I'm glad you answered about the status of the group. Maybe a message with a title "GROUP RETIRED" and content directing to the new forum would help others.

BR
Ari Okkonen

--
You received this message because you are subscribed to a topic in the Google Groups "JSON Schema" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/json-schema/xTAEsG8L0lA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to json-schema...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/json-schema/8eb7e789-a374-4234-8041-1c4a76c9df26n%40googlegroups.com.

Ben Hutton (@Relequestual)

unread,
Sep 8, 2020, 8:49:32 AM9/8/20
to JSON Schema
Hey Ari,

It isn't that the group is retired... just very rarely used. =]
Reply all
Reply to author
Forward
0 new messages