1

There is a method 'toJson' on the Google Realtime model object, as returned from Document.getModel()

doc.getModel().toJson()

Seems to take two parameters, but there is no documentation available from Google developers or elsewhere. How should it be used?

HeyHeyJC
  • 2,627
  • 1
  • 18
  • 27
  • can you look at the function's source code to try to divine the arity? – dandavis Jan 22 '15 at 19:47
  • Hmm, I can't, but maybe someone else can? toJson: function (a,b){var c=a||null,e,k=b||0;e=(Qb(),new zn);var f=db(this),g,p;g=new wo;p=bc(f.e);e=wt(e,p,f,g);null!=c?(f={},f.appId=c,f.revision=k,f.data=e,c=pc(f)):c=pc(e);return c} – HeyHeyJC Jan 22 '15 at 20:36
  • 1
    Looks like the first arg is the appId and the second arg is the doc revision. That being said, it looks like it runs fine without any args. – Grant Watters Jan 23 '15 at 04:20
  • @GrantWatters, yep, you're right. Make it an answer? It'd be nice if that API allowed setting the JSON too... – HeyHeyJC Jan 23 '15 at 04:47

2 Answers2

4

We are working on updating our documentation, but I can provide a short summary:

toJson(opt_appId, opt_revision) converts a Realtime model to a JSON representation in the same format as returned by the export REST endpoint (https://developers.google.com/drive/v2/reference/realtime/get). You can upload that JSON to the import REST endpoint (https://developers.google.com/drive/v2/reference/realtime/update) to update an existing Realtime document or create a new document. If you supply opt_appId or opt_revision, they are passed through to the returned JSON. The export REST endpoint supplies these values automatically, but the import endpoint ignores them, so they are optional and purely for your reference.

gapi.drive.realtime.loadFromJson(json) creates a in-memory Realtime document from JSON. The JSON can come either from toJson() or from the export REST endpoint. An in-memory document never communicates with the Realtime server (isInGoogleDrive is always false) so you are responsible for persisting the contents using toJson() and any storage you want (e.g. HTML5 local storage, storage on your server, or anything else which can store JSON).

gapi.drive.realtime.newInMemoryDocument() creates a new blank in-memory document. As with loadFromJson() this in-memory document is not synchronized to Google Drive, so you are responsible for persisting it.

In-memory documents fire event listeners like any other Realtime document and most of th API works normally, including undo/redo. However, there is no collaboration, data is not automatically stored, and some features (such as getCollaborators()) return generic data instead of user-specific data.

We will be releasing additional documentation soon, but this is a supported feature, not an internal API.

Brian Cairns
  • 411
  • 2
  • 4
  • Many thanks. To clarify slightly, am I reading correctly that currently the only way to feed JSON back into a Drive-backed, collaborative, Realtime document is still the import REST endpoint? – HeyHeyJC Jan 23 '15 at 20:22
  • @CherylSimon ive used the REST endpoint to update the realtime model with json. However it doesnt seem to do anything? The api doesnt respond with anything informative, and also my realtime model doesnt update? Is there anything I should know? – samsamm777 Jun 19 '15 at 14:03
  • @samsamm777 I suggest you post a new question with the details of your problem. – Cheryl Simon Jun 19 '15 at 17:51
  • Thanks @CherylSimon, i have now posted a new question. http://stackoverflow.com/questions/31052768/google-realtime-api-update-model-from-json – samsamm777 Jun 25 '15 at 14:15
1

So your last comment jogged my memory. While undocumented, I think this is actually the end point for the following Drive API calls: https://developers.google.com/drive/v2/reference/realtime/get. It's a bit weird because its not under the Realtime API and instead documented under the Drive API, but you can both export as JSON as well as import a JSON structure. Presumably the toJson function will spit out compliant JSON to be used with these functions, but I have not tested this.

Edit: Also - it looks like they have more fun undocumented features that go hand-in-hand with this. In particular gapi.drive.realtime.loadFromJson is available, which presumably is the actual loading counterpart to toJson. There is also the gapi.drive.realtime.newInMemoryDocument fn that is exposed which is likely an internal function to initialize the document loaded from 'loadFromJson'. Additionally there is 'Document.isinGoogleDrive' which very likely determines if you are using an in-memory document vs a Drive backed one. Fun stuff :).

Grant Watters
  • 660
  • 4
  • 16
  • I've been concerned about those two realtime orphans sitting there all alone in the Drive API. Does the lack of documentation imply "subject to change and/or removal", I wonder? Either way, thanks, and agreed, fun stuff, I'm gonna spend today playing with those. (Also interested in how you tracked down the two parameters above?) – HeyHeyJC Jan 23 '15 at 18:05
  • I don't see the two functions over in the Drive API going anywhere. They were an announced fully supported feature [ https://www.youtube.com/watch?v=bi9AUGr7qus ]. I'm much less sure about toJson and loadFromJson, though I have a feeling these were unintentionally leaked in the public api and will be documented at some point in the future. – Grant Watters Jan 23 '15 at 18:33