JSON Recursion – Designing a Self-Describing Document System

jsonrecursion

I'm trying to devise a document system for a specific domain involving simple objects (People, Companies, Invoices, etc.) that would also be able to completely describe itself. This self-description ability would ideally be recursive. The document system will be based on JSON, but I believe the principle in question applies to any structured notation.

To illustrate what I mean, let's say I have the following JSON document that describes a person (company, invoice, etc. are basically all the same, only with different properties):

{
  "Name": "John Doe",
  "Email": "john.doe@example.org",
  "BirthDate": "1976-07-04"
}

Then, I have a second JSON document that describes the structure of the first one. Let's call this a "Level 1 Meta-document":

{
  "Type": "Person",
  "Properties": {
    "Name": { "Type": "String", "Required": true },
    "Email": { "Type": "String" },
    "BirthDate": { "Type": "Date" }
  }
}

This would be simple enough, if not for the requirement, that system should also be able to fully describe this meta-document.

To put it in more general terms: I'm looking for a way to define a self-sufficient "Level N Meta-document", that would be able to describe a structure of the "Level N-1 Meta-documents".

NOTE: I'd be willing to go with a solution for N = 2, but instinct tells me that a true solution for N = 2 would also work for any N. Now that I think of it, this may be more of a math puzzle than programming one. 🙂

Is this even possible? If yes, can you give me some examples? If not, what are my other options?

EDIT: I've included a naive example of how a "Level 2 Meta-Document" would look, based on the above:

{
  "Type": "MetaLevel2",
  "Properties": {
    "Properties": { "Type": "Hash", "Required": true }
  }
}

The problem with this is that it doesn't describe the object that describes the property details (i.e. the one with the "Type" and "Required" attributes).

If I were to include description of those, I'd have to add another attribute to the very same object I'm trying to describe:

{
  "Type": "MetaLevel2",
  "Properties": {
    "Properties": {
      "Type": "Hash",
      "Required": true,
      "ValueProperties": { "Type": "String", "Required": "Boolean" }
    }
  }
}

Unfortunately, this throws me into recursive problem, because I now lack the description for "ValueProperties". In fact, for every new attribute I invent on level N, I have a problem describing it on level N+1 without introducing yet another attribute that needs description.

What I'm looking for is a solution that wouldn't suffer from this problem.

To be clear: I'm aware of XSD, but I'm not sure how to apply its principles to my case. Unless I'm missing something, XSD would suffer from the same recursive problem. This gives me reason to believe I have a problem with the approach itself.

Best Answer

There's IETF draft for JSON Schema. That sounds like what you want to use. Also check it out at Wikipedia.

Related Topic