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

Currency numeric type? #345

Open
GatoRat opened this issue May 28, 2015 · 3 comments
Open

Currency numeric type? #345

GatoRat opened this issue May 28, 2015 · 3 comments

Comments

@GatoRat
Copy link

GatoRat commented May 28, 2015

I'm working on a C++ application which exchanges data with a Java app using JSON. On the Java side, they used a currency class. The data is written as floating point value in the JSON. We handle this by reading it as a double and then converting it to pennies. And visa versa when going the other way. This works, but the use of double for currency annoys me.

I've been debating possible solutions. The most flexible would be to add the ability to "ReadAsString" and WriteAsString, which would treat a numeric value as a string with the restriction that the value must ultimately be numeric.

Perhaps there is already a way to do this, in which case, educate me.

@miloyip
Copy link
Collaborator

miloyip commented May 29, 2015

Currently RapidJSON does not support this feature. The feature you request may be like "validating JSON numbers but not converting them to doubles", and vice versa.

I am not sure whether this feature is a common need. If so, we can consider adding this in the coming versions. My idea for that is adding a parse flag, related SAX events and DOM APIs.

A simple solution is to serialize the currency as JSON string type instead of number type. And then you do the format validation and conversion in the application side.

I am open for discussion.

@GatoRat
Copy link
Author

GatoRat commented May 29, 2015

I agree that the currency values should have been sent as strings. Alas, the team we are interacting with can't change due to their existing clients. (Another solution, which we were doing in our own code, is to store currency as 64-bit integers and apply a denominator.)

I agree that this may not be a common need. However, I'll still ponder various solutions.

@miloyip
Copy link
Collaborator

miloyip commented Feb 14, 2016

For writing, it may be possible to use Writer::RawValue() in #543 . But there is no counterpart in Reader/Document now.

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

2 participants