Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When converting the JS objects to JSON get associative arrays for ObjectIds #64

Closed
jhreis opened this issue Mar 21, 2017 · 8 comments
Closed

Comments

@jhreis
Copy link

@jhreis jhreis commented Mar 21, 2017

Since mobile clients don't natively support JS I am converting the JS objects returned, to JSON (via JSON.stringify()), and then converting them to native objects. But the object IDs get encoded as associative arrays like:

"objectId":{"0":158,"1":193,"2":153,"3":122,"4":79,"5":13,"6":42,"7":227,"8":172,"9":46,"10":104,"11":111,"12":120,"13":165,"14":32,"15":191}

This leads to some odd behavior, as native conversion does not guarantee retained order. Also, makes passing objectIds into the library more difficult.

@bridiver
Copy link
Contributor

@bridiver bridiver commented Mar 21, 2017

what js data type are they?

@jhreis
Copy link
Author

@jhreis jhreis commented Mar 21, 2017

Sync docs say Array of Objects (Array.<Object>), but it is actually an arguments object. I am planning on dealing with it natively, just kind of an odd result, so created ticket.

@diracdeltas
Copy link
Member

@diracdeltas diracdeltas commented Mar 21, 2017

object IDs in brave/sync are Uint8Arrays. to avoid the weird conversion and preserve order, you can serialize them with JSON.stringify(Array.from(objectID)), and then convert array back to Uint8Array when deserializing.

we have the same issue in browser-laptop, so i think it is a general issue with JSON.stringify.

@jhreis
Copy link
Author

@jhreis jhreis commented Mar 21, 2017

Yeah, the main issue is that the ids are just child properties in the data structure, and it is by far better to just JSON.stringify(<any result from sync>), rather than trying to parse through the data structure in JS, get a handle on the objectID itself, and then call JSON.stringify on it. Especially, when I need additional data besides just the objectIDs

Also, it is JS's stringify that is leading to the dictionary JSON, so this isn't an iOS thing, this is all native javascript. I use JS to first convert the returned result to JSON (which is where this is happening), and then later convert it to the native language.

The current solution is workable, just a bit hacky as I need to stitch the ID together from the JSON associative array.

I am not expecting or recommending any changes at this point, but wanted to get a discussion / bring awareness to this.

@diracdeltas
Copy link
Member

@diracdeltas diracdeltas commented Mar 21, 2017

I use JS to first convert the returned result to JSON

I just remembered that JSON.stringify takes a replacer function as the second argument: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Parameters. So you could use this to convert any Uint8Arrays to Arrays.

@jhreis
Copy link
Author

@jhreis jhreis commented Mar 21, 2017

Oh, beautiful. This could help bunches. Appreciate it!

This is what I quickly came up with. Any thoughts / recommendations are welcome 😄

function(k,v) {
    if (Object.prototype.toString.call(v) === '[object Uint8Array]') {
        return Array.prototype.slice.call(v)
    }
    return v
}
@diracdeltas
Copy link
Member

@diracdeltas diracdeltas commented Mar 21, 2017

i would probably do

if (v instanceof Uint8Array) {
  return Array.from(v)
}
return v
@jhreis
Copy link
Author

@jhreis jhreis commented Mar 21, 2017

Yeah, me too. Haha. Thanks, much nicer.

@jhreis jhreis closed this Mar 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.