Forced floats into doubles #63

Closed
wants to merge 3 commits into
from

Projects

None yet

3 participants

Collaborator

I ran into problems talking to a java rpc server when expecting double's but JS 'number' primitive is serialized as an int.

Scenario 1, correct serialization:

require('./lib/serializer').serializeMethodCall('foo', [parseFloat("0.5")]);
<?xml version="1.0"?>
<methodCall>
  <methodName>foo</methodName>
  <params>
    <param><value><double>0.5</double></value></param>
  </params>
</methodCall>

Scenario 2, wrong serialization:

require('./lib/serializer').serializeMethodCall('foo', [parseFloat("0.5") + parseFloat("0.5")]);
<?xml version="1.0"?>
<methodCall>
  <methodName>foo</methodName>
  <params>
    <param><value><int>1</int></value></param>
  </params>
</methodCall>

The neatest thing I could come up with is a Float class that forces number parsing as a double, but it's 5:40am so maybe there's a better solution.

require('./lib/serializer').serializeMethodCall('foo', [new xmlrpc.Float(parseFloat("0.5") + parseFloat("0.5"))]);
<?xml version="1.0"?>
<methodCall>
  <methodName>foo</methodName>
  <params>
    <param><value><double>1</double></value></param>
  </params>
</methodCall>

< Casting from int to long type >

I've not been invoked xmlrpc method, and because argument type is miss-matched.

Current : int type

callMethod('test', [10]);

Improvement : forced casting to long type

callMethod('test', [new xmlrpc.Long(10)]);
Collaborator

It was of course only a matter of time before someone needed another type to be casted and that moment is now. I will make some time inside a commercial project to fix this matter properly and come up with a new PR because this one is not fit to merge.

I suppose the best solution would be to allow for custom types to be added to the serializer, but to not code those types inside the library like I did for float. Basically we'd need to expose 2 new functions serializer.addCustomType(constr) and serializer.removeCustomType(constr) where constr would be expected to have a toString() method and check value instanceof constr inside object serialization?

@baalexander : would that be an acceptable PR?

Owner

@patricklodder - First, apologies for the ridiculously long time of replying. I agree some sort of way to enforce a type would be nice. Maybe a class like "CustomType" which you could extend. Then we could check

if (value instanceof "CustomType") {
  // Add value to XML (toString() or something similar)
}

Thoughts?

Collaborator

@baalexander - I have opened a new pull request ( #75 ) for the solution as you proposed. Closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment