News for hackers. Stuff that matters.
Re:Page Weight Matters (2012) by kuschku on Thursday October 15, 2015 @02:54PM (#10395169)
> Want to download all the comments with the user info for every user? You have a choice between repeating the user info per comment, requesting the comments first then the users by id (two requests) or using a serialisation mechanism that supports object references (isn't pure JSON)

Or you send it like

    {
      "users": [
        {
          "id": "de305d54-75b4-431b-adb2-eb6b9e546014",
          "name": "Max Mustermann",
          "image": "https://news.ycombinator.com/y18.gif"
        },
        ...
      ],
      "comments": [
        {
          "user": "de305d54-75b4-431b-adb2-eb6b9e546014",
          "time": "2015-10-15T18:51:04.782Z",
          "text": "Or you can do this"
        },
        ...
      ]
    }
Which is a standard way to do it that works here, too. Because you can have references in JSON, you just have to do them yourself.
Re:Page Weight Matters (2012) by danneu on Thursday October 15, 2015 @03:28PM (#10395373)
Alternatively, I would think that gzip does a good job of factoring out repeatedly embedded user objects.
Re:Page Weight Matters (2012) by spion on Thursday October 15, 2015 @03:56PM (#10395547)
Yes I agree that gzip will probably fix this single-handedly. The main cost here is very likely network transfer, so gzip will do great.
Re:Page Weight Matters (2012) by joevandyk on Thursday October 15, 2015 @05:33PM (#10396158)
Don't forget it takes time to parse large json blobs.
Re:Page Weight Matters (2012) by sanderjd on Friday October 16, 2015 @01:56PM (#10400963)
And to serialize it from whatever the in-memory representation is.
Re:Page Weight Matters (2012) by kuschku on Thursday October 15, 2015 @03:42PM (#10395467)
yes, obviously – gzip even does that explicitly, replacing repeated text with references.

But it still takes more power for your server to go through gzip every time. And it will take more RAM on the client to store those objects.

Re:Page Weight Matters (2012) by nubs on Thursday October 15, 2015 @03:42PM (#10395468)
Something similar has been standardized as well with the JSON API specification (although it adds its own weight to the message as well, it does address this problem): http://jsonapi.org/format/#document-compound-documents
Re:Page Weight Matters (2012) by spion on Thursday October 15, 2015 @03:54PM (#10395534)
Its a great solution! I'd use dictionaries for faster lookups though.

But not exactly pure JSON. Client-side, you need to attach methods (or getters) that fetch the user object to the comment. I suppose you could just attach get(reference) that takes this[reference + '_id'] and looks it up inside the `result[reference]`. m:n relations will be harder though.

Otherwise you can't e.g. simply pass each comment to e.g.a react "Comment" component that renders it. You would also have to pass the user list (dictionary) to it.

Re:Page Weight Matters (2012) by kuschku on Thursday October 15, 2015 @04:37PM (#10395828)
Well, you could process the JSON on client side.

    response.comments.forEach(comment =>  comment.author = response.user[comment.author]);
Preferably, even, you could do it during rendering so it can be garbage collected afterwards.
Re:Page Weight Matters (2012) by spion on Thursday October 15, 2015 @04:59PM (#10395950)
Well, that counts as further deserialisation in my book. At least if you set up a convention to automate it for all kinds of objects. Otherwise you'd have to do it manually for every type of request