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

Convert Longs to strings and ByteBuffers to Buffers #281

Closed
seishun opened this issue Jun 7, 2015 · 8 comments
Closed

Convert Longs to strings and ByteBuffers to Buffers #281

seishun opened this issue Jun 7, 2015 · 8 comments

Comments

@seishun
Copy link
Contributor

seishun commented Jun 7, 2015

My library passes decoded protobuf messages directly to users. Since I don't want to tie the library to Long.js and ByteBuffer.js, I run all objects through a function that converts any Long objects to strings and any ByteBuffer objects to Buffers. It gets a bit messy when there are nested messages.

Do you think adding options to convert Longs to strings and/or ByteBuffers to Buffers on decode would be a meaningful addition to ProtoBuf.js?

@dcodeIO
Copy link
Member

dcodeIO commented Jun 7, 2015

Message#toRaw(false, true) should do that for you (see)

@seishun
Copy link
Contributor Author

seishun commented Jun 7, 2015

Cool, that works. I think the description of that method could be a bit clearer, though. It's not obvious what "raw payload" means.

Is there perhaps also a way to remove all the null fields? Removing also fields with a default value that haven't been set would be even better, but I understand it's work-in-progress (#200).

@zhiqingchen
Copy link

Customize your toRaw method

function toRaw(obj) {
    var clone = {};
    for (var i in obj) {
        if (obj.hasOwnProperty(i)) {
            if (obj[i] === null) {

            } else if (typeof obj[i] !== 'object') {
                clone[i] = obj[i];
            } else if (ProtoBuf.ByteBuffer.isByteBuffer(obj[i])) {
                clone[i] = obj[i].toHex();
            } else if (ProtoBuf.Long.isLong(obj[i])) {
                clone[i] = obj[i].toString();
            } else {
                clone[i] = cloneRaw(obj[i]);
            }
        }
    }
    return clone;
}

@englercj
Copy link

englercj commented Aug 6, 2015

obj[i] instanceof ProtoBuf.ByteBuffer

Don't do that, use ByteBuffer.isByteBuffer()

@zhiqingchen
Copy link

@englercj

ByteBuffer.isByteBuffer = function(bb) {
     return (bb && bb instanceof ByteBuffer) === true;
};

I don't understand, they look the same

@englercj
Copy link

englercj commented Aug 7, 2015

@zhiqingchen You are using/looking at an old version of ByteBuffer. This is the latest version of that method:

https://github.com/dcodeIO/ByteBuffer.js/blob/master/src/methods/static/isByteBuffer.js

See this issue for more info: protobufjs/bytebuffer.js#52

@zhiqingchen
Copy link

@englercj
get it, thanks.

@dcodeIO
Copy link
Member

dcodeIO commented Nov 28, 2016

Closing this for now.

protobuf.js 6.0.0

Feel free to send a pull request if this is still a requirement.

@dcodeIO dcodeIO closed this as completed Nov 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants