Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

feature request: toJSON() #19

Closed
ronkorving opened this Issue · 3 comments

3 participants

@ronkorving

It would be really nice if msgpack could directly turn its binary representation into a JSON string, avoiding parse from msgpack -> stringify to JSON. Besides the potential performance benefit, not having this step would avoid a lot of object creation, which could really help the garbage collector to take it easy.

@download13

toJSON is normally a function used for returning JSON serializable objects from an object that doesn't allow enumeration, or for when you want JSON to only serialize a portion of the object.
For example:

function A() {
  this.t = 4;
  this.prop = "test";
}
A.prototype.toJSON = function() {
  return {prop: this.prop}
}

var a = new A;

If a is ever stringifyed with JSON, you will end up with {"prop": "test"} instead of {"t": 4, "prop": "test"}.

tl;dr You might want to change this request to use a different function name.

@godsflaw
Collaborator

Can you help clarify? If I have an array:

var myArray = [ "one", "two" ];

and I pack that with msgpack.pack:

var binaryData = msgpack.pack(myArray);

You want a function that let's you say:

var jsonString = msgpack.unpack(binaryData, 'JSON');

I tend to agree with download13, we should not call it msgpack.toJSON(). I think making something like this an option to unpack is more desirable.

Is this what you mean?

@mikepb mikepb referenced this issue from a commit in mikepb/node-msgpack
@mikepb mikepb [#19, #21] Implement toJSON compatibility 44f322b
@mikepb mikepb referenced this issue from a commit in mikepb/node-msgpack
@mikepb mikepb [#19, #21] Implement recursive toJSON compatibility 514705a
@godsflaw
Collaborator

closing because of #50

@godsflaw godsflaw closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.