Currency: Behavior change for to_json for demurrage currencies.

Currency#to_json should output a format that can be successfully converted back
to a Currency object via from_json. So for demurrage currencies it should output
the full hex currency code.

Currency#to_human on the other hand should always print something a human will
understand. For demurrage currencies we'll print the three-letter code for now.
This commit is contained in:
Stefan Thomas
2014-02-09 23:05:30 -08:00
parent 37e6d95cbc
commit 0772ffb6ed
2 changed files with 24 additions and 4 deletions

View File

@@ -193,11 +193,13 @@ Currency.prototype.get_interest_at = function (referenceDate) {
Currency.prototype.to_json = function () {
if (!this.is_valid()) {
// XXX This backwards compatible behavior, but probably not very good.
// XXX This is backwards compatible behavior, but probably not very good.
return "XRP";
}
if (/^[A-Z0-9]{3}$/.test(this._iso_code)) {
// Any currency with standard properties and a valid code can be abbreviated
// in the JSON wire format as the three character code.
if (/^[A-Z0-9]{3}$/.test(this._iso_code) && !this.has_interest()) {
return this._iso_code;
}
@@ -215,6 +217,11 @@ Currency.prototype.to_json = function () {
};
Currency.prototype.to_human = function () {
// to_human() will always print the human-readable currency code if available.
if (/^[A-Z0-9]{3}$/.test(this._iso_code)) {
return this._iso_code;
}
return this.to_json();
};