{"action":"created","issue":{"url":"https://api.github.com/repos/google/flatbuffers/issues/3786","repository_url":"https://api.github.com/repos/google/flatbuffers","labels_url":"https://api.github.com/repos/google/flatbuffers/issues/3786/labels{/name}","comments_url":"https://api.github.com/repos/google/flatbuffers/issues/3786/comments","events_url":"https://api.github.com/repos/google/flatbuffers/issues/3786/events","html_url":"https://github.com/google/flatbuffers/issues/3786","id":136612060,"number":3786,"title":"Is it possible to simply convert javascript object and flatbuffers?","user":{"login":"darklinden","id":888298,"avatar_url":"https://avatars.githubusercontent.com/u/888298?v=3","gravatar_id":"","url":"https://api.github.com/users/darklinden","html_url":"https://github.com/darklinden","followers_url":"https://api.github.com/users/darklinden/followers","following_url":"https://api.github.com/users/darklinden/following{/other_user}","gists_url":"https://api.github.com/users/darklinden/gists{/gist_id}","starred_url":"https://api.github.com/users/darklinden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/darklinden/subscriptions","organizations_url":"https://api.github.com/users/darklinden/orgs","repos_url":"https://api.github.com/users/darklinden/repos","events_url":"https://api.github.com/users/darklinden/events{/privacy}","received_events_url":"https://api.github.com/users/darklinden/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":21,"created_at":"2016-02-26T06:49:15Z","updated_at":"2016-08-09T06:02:34Z","closed_at":null,"body":"I think it is too complex for converting between js object and flatbuffers' buffer. \r\nI think there maybe a friendly interface like below.\r\n\r\n```\r\nMyTableObj = { ... };\r\nflatHelper = { ... };\r\n\r\nvar jsDataModuleObj = flatHelper.createObj(MyTableObj);\r\n\r\n... // data has simple getter and setter, so you can use it like a simple object.  \r\n... // do sth\r\n\r\n// convert between buff and js obj\r\nvar buff = flatHelper.js2flat(jsDataModuleObj);\r\nvar obj = flatHelper.flat2js(buff);\r\n```\r\n\r\nI tried to using obj.constructor.name and typeof or anything else to mark up the data 's relationship, but it dose less help.\r\n\r\nIs there any solutions?\r\n\r\nThanks."},"comment":{"url":"https://api.github.com/repos/google/flatbuffers/issues/comments/238461524","html_url":"https://github.com/google/flatbuffers/issues/3786#issuecomment-238461524","issue_url":"https://api.github.com/repos/google/flatbuffers/issues/3786","id":238461524,"user":{"login":"evanw","id":406394,"avatar_url":"https://avatars.githubusercontent.com/u/406394?v=3","gravatar_id":"","url":"https://api.github.com/users/evanw","html_url":"https://github.com/evanw","followers_url":"https://api.github.com/users/evanw/followers","following_url":"https://api.github.com/users/evanw/following{/other_user}","gists_url":"https://api.github.com/users/evanw/gists{/gist_id}","starred_url":"https://api.github.com/users/evanw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/evanw/subscriptions","organizations_url":"https://api.github.com/users/evanw/orgs","repos_url":"https://api.github.com/users/evanw/repos","events_url":"https://api.github.com/users/evanw/events{/privacy}","received_events_url":"https://api.github.com/users/evanw/received_events","type":"User","site_admin":false},"created_at":"2016-08-09T06:02:34Z","updated_at":"2016-08-09T06:02:34Z","body":"The binary layout hasn't been documented yet unfortunately. I've been meaning to do that since you asked but I haven't found the time so far. There are only a few primitive types (byte, int, float, and string) and the [serialization routines](https://github.com/evanw/kiwi/blob/c02e370582a95df2face2f4b623a7b6ef76aa98e/kiwi.h#L185) are very simple so it should be pretty easy to understand the details of the format from the code. Here's the simple version in the meantime:\r\n\r\n* Variable-length integers use [LEB128](https://en.wikipedia.org/wiki/LEB128) just like Protocol Buffers, including the part where the sign bit is the least significant bit for signed integers. 32-bit integers can take up to 5 bytes to encode this way. Kiwi doesn't support 64-bit integers because JavaScript doesn't support them.\r\n* Floats are stored as 32-bit values but their bits are rotated so the 8-bit exponent comes first. If the exponent is zero (+0, -0, and denormalized numbers), the other 24 bits (the sign and mantissa) aren't encoded. This means all floats encode to 4 bytes except for 0, which takes up 1 byte.\r\n* Strings are stored using a null-terminated UTF-8 encoding.\r\n* Arrays are stored as a single variable-length unsigned integer length followed by that many elements.\r\n* Structs are stored by writing out the value for each field in order, with no other metadata.\r\n* Messages are stored as a sequence of (field ID, value) pairs terminated by a single null byte. Each field ID is stored as a variable-length unsigned integer.\r\n\r\nThere's a live demo at http://evanw.github.io/kiwi/ where you can see which bytes get produced for different data types.\r\n"}}
{"actor":{"display_login":"evanw"}}