Skip to content

Add Embedded Refract serialization#14

Merged
pksunkara merged 2 commits intomasterfrom
smizell/embedded-refract
Sep 4, 2015
Merged

Add Embedded Refract serialization#14
pksunkara merged 2 commits intomasterfrom
smizell/embedded-refract

Conversation

@smizell
Copy link
Copy Markdown
Contributor

@smizell smizell commented Aug 29, 2015

Proposal for thinking about a new serialization format.

@smizell
Copy link
Copy Markdown
Contributor Author

smizell commented Aug 31, 2015

I also wanted to point out that I'm interested in this for the sake of adding hypermedia to existing JSON structures.

{
  "users": [
    {
      "id": 1,
      "name": "John Doe",
      "email": "john.doe@example.com"
    },
    {
      "id": 2,
      "name": "Jane Doe",
      "email": "jane.doe@example.com"
    }
  ]
}

becomes:

{
  "users": [
    {
      "refract": {
        "element": "resource",
        "attributes": {
          "rels": ["user"],
          "href": "/users/1"
        }
      },
      "id": 1,
      "name": "John Doe",
      "email": "john.doe@example.com"
    },
    {
      "refract": {
        "element": "resource",
        "attributes": {
          "rels": ["user"],
          "href": "/users/2"
        }
      },
      "id": 2,
      "name": "Jane Doe",
      "email": "jane.doe@example.com"
    }
  ]
}

Or without the transcluded data:

{
  "users": [
    {
      "refract": {
        "element": "resource",
        "attributes": {
          "rels": ["user"],
          "href": "/users/1"
        }
      }
    },
    {
      "refract": {
        "element": "resource",
        "attributes": {
          "rels": ["user"],
          "href": "/users/2"
        }
      }
    }
  ]
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any guidance on how to handle name collisions? What would be the expected behavior of an implementation that could not produce this serialization because of a pre-existing refract property?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure exactly. We could make it less likely to collide, like with _refract?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to propose _refract. I really don't like refract. It feels like we are taking away user's freedom. But using _refract diminishes it a bit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change to _refract then. Works for me!

@danielgtaylor
Copy link
Copy Markdown
Contributor

This is definitely an interesting idea. I like the concept, but we should make sure it isn't too confusing.

In my opinion this also raises the question of which formats should be supported by a given implementation and whether the spec can provide some guidance on that issue.

@smizell
Copy link
Copy Markdown
Contributor Author

smizell commented Sep 1, 2015

@danielgtaylor My hope would be that you could add Refract to existing JSON, especially adding hypermedia to non-hypermedia structures. It would be very similar to JSON-LD, but a lot more powerful in that you could do lots of different types and stuff.

Regarding implementations, I would like to see this kind of stuff be a plugin for minim or some added on library. So we could keep the full version as the default, then include these other formats with use. It would go through all base types and add methods for toWhatever or however we wanted to do it.

Comment thread text/0000-embedded-refract.md Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should do this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pksunkara
Copy link
Copy Markdown
Contributor

Just one issue I am having concerns about. Embedded refract is a good thing. But embedded refract in refract element? How deep does the rabbit hole go?

@smizell
Copy link
Copy Markdown
Contributor Author

smizell commented Sep 3, 2015

Comments addressed:

  • Fixed typos
  • Changed to _refract

@pksunkara
Copy link
Copy Markdown
Contributor

@smizell Continuing the discussion here.

Is your concern the recursion?

It's not about the recursion. It's not about the implementation or anything. It's about concepts. It's more about the fact that refract is a core thing and embedded refract is concept built on it. What use is there for embedded refract to be in refract? Refract already allows elements in refract. If we try to use embedded refract in refract, it does nothing different except maybe add an extra nested level of object.

@smizell
Copy link
Copy Markdown
Contributor Author

smizell commented Sep 3, 2015

It actually does add something in that you know what has or has not been refracted. It tells you more that normal refract. No need for element specific code such as we have in Minim.

See https://github.com/refractproject/minim/blob/master/lib/primitives/base-element.js and convertAttributesToElements.

I'm ok with not including it, but I do think you actually could gain a lot.

@pksunkara
Copy link
Copy Markdown
Contributor

From your example

{
  "_refract": {
    "element": "object",
    "meta": {
      "id": "bowler-103"
    },
    "attributes": {
      "foo": {
        "_refract": {
          "element": "string",
          "content": "baz"
        }
      }
    }
  },
  "first_name": "John"
}

If we don't use embedded refract in refract, this is how the example looks.

{
  "_refract": {
    "element": "object",
    "meta": {
      "id": "bowler-103"
    },
    "attributes": {
      "foo": {
        "element": "string",
        "content": "baz"
      }
    }
  },
  "first_name": "John"
}

I don't see any benefit in the first example when compared to the second one. Actually the extra _refract makes it more complex.

I do think you actually could gain a lot.

Can you give me a small example? Maybe I am having problems visualising it.

@smizell
Copy link
Copy Markdown
Contributor Author

smizell commented Sep 4, 2015

Well, let's use your example first.

{
  "_refract": {
    "element": "object",
    "meta": {
      "id": "bowler-103"
    },
    "attributes": {
      "foo": {
        "element": "string",
        "content": "baz"
      }
    }
  },
  "first_name": "John"
}

How would a client know that foo has been refracted? You would need some domain-specific, out-of-band knowledge to know it. The only other option is to say that "You MAY determine an object is refracted by looking for the presence of an element." But we don't want that.

So, we can get around it by treating the value of attributes as a normal object, and allow the already-reserved _refract property to tell the parser it has already been refracted.

{
  "_refract": {
    "element": "object",
    "meta": {
      "id": "bowler-103"
    },
    "attributes": {
      "foo": {
        "_refract": {
          "element": "string",
          "content": "baz"
        }
      }
    }
  },
  "first_name": "John"
}

In this example, the parser would see _refract and know it's already refracted without any other knowledge.


The benefit here is that you can have refracted elements mixed into the attributes tree anywhere, just like with a normal object. It would require no out-of-band knowledge or special parsing instructions like this.

@pksunkara
Copy link
Copy Markdown
Contributor

Understood now. Thanks 😄

pksunkara added a commit that referenced this pull request Sep 4, 2015
@pksunkara pksunkara merged commit 5ce2e78 into master Sep 4, 2015
@pksunkara pksunkara deleted the smizell/embedded-refract branch September 4, 2015 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants