-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Support for Decimal (monetary) data #1519
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
Comments
In that example, |
I would rather say it is mapped to a string because there is no Decimal type in JSON in the same way as there is no DateTime type either. W3C's Payment Request uses the same approach: AFAICT, this is the de-facto standard for JSON and "money"/Decimal. |
And the actual issue with the OpenAPI specification is? |
Adding support for the Decimal/monetary data type. My current implementation: /**
* Read a BigDecimal property.<p>
* Note: Since JSON does not support a native BigDecimal type,
* this method builds on <i>mapping</i>.</p>
* @param name Property
* @param decimals Required number of fractional digits or <b>null</b> if unspecified
* @return Java <code>BigDecimal</code>
* @throws IOException For syntax errors
* @see JSONObjectWriter#setBigDecimal(String, BigDecimal, Integer)
*/
public BigDecimal getBigDecimal(String name, Integer decimals) throws IOException {
return parseBigDecimal(getString(name), decimals);
} The Decimal data type usually do not support "Scientific" notation. |
Do you mean as a new type (which would not be supported by JSON or JSON schema) or as a new format? |
I mean something along the lines of |
Your use of the words "I guess" does worry me slightly. Can I ask you to read the sections of the spec related to data types and formats if you have not already done so? |
Sure, I'm unarguable NOT an expert on OpenAPI and JSON Schema. I did though read the table of data types: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#data-types I would reuse that idea for Decimal unless there is something fundamentally wrong with doing that. The W3C folks' format specification would probably suffice. |
@cyberphone Yes, the specification suggests to use the |
@cyberphone suport for decimals as strings, which would be implemented as It is literally waiting for someone who has knowledge of this area to be willing to write a PR. No one objects to it, but I'm sure there are subtleties here so I'm not comfortable writing a PR for it. Sadly, very few other people volunteer to write PRs for JSON Schema. I would love to also have |
@darrelmiller For me who is neither an expert on OpenAPI nor on JSON Schema, but rather "fluent" in JSON, do you mean that you would consider using JSON Number as wire format? I would check that with tool vendors before putting that in concrete. The three (individually created) examples I provided, all took the JSON String path. There is probably a reason for that. |
@cyberphone Yes. Here is the ANBF grammar for how to serialize a JSON Number over the wire in UTF-8 text. And your last comment gets to the heart of the matter. OpenAPI is not in the business of telling people how to serialize data. That is the job of media type specifications. We are in the business of trying to describe in a platform independent manner what people chose to do. |
@darrelmiller Your colleges at the ".NET division" serializes a BigInteger as: "myBigInteger":{"_bits":[1156841472,1164153218],"_sign":1} |
Although maybe of limited interest to you, there is a JSON clear text signature specification in the workings which (for portability reasons), builds on I-JSON and ECMAScript serialization of JSON primitives: The alternative to that is dressing the entire JSON object in Base64 which is making the "API" rather indirect. |
Just to see that we are on the same page with respect to tool support. Standard integers like implemented in for example Java and C# have:
That is, Integer and Decimal cannot readily make use of the same decoder/parser. To make this feasible Decimal must be identified as a distinct data type. I.e. "mapped". Do you agree with the above? If you do, we are talking about saving 2 bytes (the quotes) and keeping "purity" (aka data model). Since purity will only exist on the wire, we are effectively down to the 2 byte issue. However, saving 2 bytes comes at a cost: limited compatibility with JavaScript as well as with existing practices for open systems. The decision is yours. |
Because we support the JSON data types only, this proposal is being tracked for adding a new format to the proposed OAS/JSON-Schema formats registry. See json-schema-org/json-schema-spec#845 |
Related to: #1517
Here is a printout from Open Banking in the UK. "Amount" is the field of interest.
The text was updated successfully, but these errors were encountered: