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

Interop between JS Objects and JSONObjects #1500

Closed
triniwiz opened this issue Oct 9, 2019 · 1 comment
Closed

Interop between JS Objects and JSONObjects #1500

triniwiz opened this issue Oct 9, 2019 · 1 comment
Assignees
Labels
Milestone

Comments

@triniwiz
Copy link
Member

triniwiz commented Oct 9, 2019

At the moment on iOS it is possible to use a JS Object where NSDictionary is need so it would be handy if it were possible to do something similar for Android .

For e.g there are a couple instances where we would need to do something like the following

export function serialize(data: any): any {
    let store;
    switch (typeof data) {
        case 'string':
        case 'boolean':
        case 'number': {
            return data;
        }

        case 'object': {
            if (!data) {
                return null;
            }

            if (data instanceof Date) {
                return data.toJSON();
            }
            if (Array.isArray(data)) {
                store = new org.json.JSONArray();
                data.forEach((item) => store.put(serialize(item)));
                return store;
            }
            store = new org.json.JSONObject();
            Object.keys(data).forEach((key) => store.put(key, serialize(data[key])));
            return store;
        }

        default:
            return null;
    }
@darind darind added the feature label Oct 9, 2019
@vmutafov
Copy link
Contributor

vmutafov commented Oct 9, 2019

Yeah, I've thought about that before but I'm concerned to what type should such JSON data be converted?
In your case, you use JSONArray and JSONObject but if we want to achieve similar behaviour as in iOS, we should probably map the JSON to a Map<T>.
This may be a good feature but I probably don't see all the use cases. I'm not sure how common it is for Java/Kotlin APIs to expect an argument of type org.json.JSONObject or org.json.JSONArray. Personally, there are quite a few such APIs I've seen. If you can shed some light on these questions, it would be great. :)

Btw, there is a similar issue about serializing the JS Date object to the Java Date objects (both the new and the old Java APIs). If you find it useful, you can vote on it, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants