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

Seeing a bunch more weirdness in asJSON #620

Closed
robin-anil opened this issue Jan 4, 2017 · 16 comments
Closed

Seeing a bunch more weirdness in asJSON #620

robin-anil opened this issue Jan 4, 2017 · 16 comments

Comments

@robin-anil
Copy link
Contributor

This is converted with

{ fieldsOnly: true, longs: Number, enums: String, defaults: false, arrays: true }

Array entry 0 is missing color, secondary state and orderings.
screen shot 2017-01-04 at 2 24 49 pm

Array entry 1 is missing all the fields.
screen shot 2017-01-04 at 2 25 03 pm

I also have a case where enum is not converted to String.

@yoni-tock
Copy link

It also looks like the enum value 0 isn't being converted to a string, but anything greater than zero is being converted. For example, before toJSON index 0 has icon 0 and index 1 has icon 3 but after toJSON index 0 has icon 0 and index 1 has icon "TRANSFER".

screen shot 2017-01-04 at 2 46 53 pm

@robin-anil
Copy link
Contributor Author

Also including #616 There needs to be a better differentiation between 0 and unset.

@dcodeIO
Copy link
Member

dcodeIO commented Jan 4, 2017

The initial thing is probably due to asJSON removing any default values when { defaults: true } isn't set. Hrm.

There, asJSON doesn't really know which property is on the message itself and which one is on the prototype - hence it handles both the same. Testing this through hasOwnProperty would have a significant performance impact.

I'll probably throw the current converter stuff away again and try something that iterates over the source's properties instead.

@robin-anil
Copy link
Contributor Author

Thank you! Once this is done, we will be in a really amazing pace, can't wait to get all the big performance improvements with this release.

All these are painful because these are breaking changes and we have a large code base with hundreds of messages.

@dcodeIO
Copy link
Member

dcodeIO commented Jan 5, 2017

May I ask what's the exact format of the json data you are using? Does it somewhat correspond to the official proto3 json mapping?

The reason why I'm asking is that I'd like to start with the official JSON mapping and then build onto that, ideally eliminating as many of the options as possible.

For example, the official json mapping always converts:

  • enums to strings
  • longs to strings
  • bytes to base64 strings
  • and it always removes default values to safe space

@robin-anil
Copy link
Contributor Author

Yes, official mapping will work with few options for exceptions.
we use number for long,(which we can change over in a different pass) and add defaults for arrays(which again is too daunting for this pass).
The mappings don't specify extensions which we use heavily. I would love to keep the current format for that where key is the type.
We can't remove default values if present because that is a proto2 thing and that will be a severe breakage. The only rule we care is that specified values need to be present in JSON form

@robin-anil
Copy link
Contributor Author

@dcodeIO just checking to see if you have made some progress on this.

@dcodeIO
Copy link
Member

dcodeIO commented Jan 10, 2017

Not yet, unfortunately. I really want 6.4.X to become stable now, before going for a 6.5.X with new converters.

@dcodeIO
Copy link
Member

dcodeIO commented Jan 12, 2017

Additional notes:

  • asJSON is now toObject to mimic official protobuf APIs. This is available both statically (with an additional message parameter) and on instances.
  • .from is now just an alias of .fromObject.
  • There is now toJSON on message instances (utilized by JSON.stringify) that uses default options (enums=longs=bytes=String) to be somewhat compatible to official json conversion.
  • Currently, there is no more bytes: Buffer option. This has been an undocumented gRPC thing, but I'd rather like to drop it than to keep this around forever as I don't see a benefit of having plain objects populated with buffers now that default values are on prototypes anyway. I assume that this has been used solely to work around not having proper default values.
  • There is no more fieldsOnly: true|false option.
  • There is now objects: true that does for maps what arrays: true does for repeated fields.
  • There are no more conversion options for object -> message conversion because unnecessary.

Also worth mentioning: This generates somewhat longish code now, but it is 2-3 times faster than what we had before iirc.

Let me know what you think. It's probably still buggy somehow, but it passes the slightly extended test case (which failed for maps btw. with the old converters).

@robin-anil
Copy link
Contributor Author

Sweet! Let me try it

@robin-anil
Copy link
Contributor Author

from is throwing an error. I have a proto which has a message as a nested field

message MyProto { optional MyNestedProto proto = 1 }
where MyNestedProto has a long id in it.

MyProto.from({}) is crashing, its trying to parse MyNestedProto

@robin-anil
Copy link
Contributor Author

still throwing...

@dcodeIO
Copy link
Member

dcodeIO commented Jan 12, 2017

This is working for me:

var root = protobuf.Root.fromJSON({
    nested: {
        MyProto: {
            fields: {
                proto: {
                    type: "MyNestedProto",
                    id: 1
                }
            }
        },
        MyNestedProto: {
            fields: {
                aLong: {
                    type: "uint64",
                    id: 1
                }
            }
        }
    }
}).resolveAll();

var myProto = root.MyProto.from({});
console.log(myProto);

What's the difference?

@robin-anil
Copy link
Contributor Author

Hmm. I don't know...
I am seeing this

screen shot 2017-01-11 at 11 25 51 pm

@robin-anil
Copy link
Contributor Author

robin-anil commented Jan 12, 2017

Fixed it. Looks like a version skew. I am working off git+https://... in package.json It does not really work well on branches.
All good, All our tests are passing!

@robin-anil
Copy link
Contributor Author

I have gone through several features so far in my giant protobuf conversion branch and I have yet to find anything obviously broken with the entire slew of apps we have.

I will have more eyes during code-review and QA. I will post if there are more issues.

Closing the bug. please push 6.5.0 when you get a chance, we would prefer not to build production assets directly from git master.

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

3 participants