Unable to add a currency value to a JSONItem?

just found this one :
used the JSON xojo example, and modified it to add a currency to the json

Var Person As New JSONItem
Dim c As Currency = 27.32
// This object is manipulated like a dictionary
Person.Value("Name") = "John Doe"
Person.Value("Age") = 32
Person.Value("Married") = True
Person.Value("curr") = c
Person.Value("Spouse") = "Jane Doe"

Var s As String = Person.ToString

it fails with a “invalidargumentexception” …

if I change the “c” with “c.toString” everything is ok …
is it normal you can’t add a currency to a JSONItem ? it should be converted to a double ?

It is with Xojo. You need to convert to double or to string.
You can vote Issue #74424 if you like.

There was an exhaustive thread on this forum some months ago concerning this very issue. Many solutions were proposed, which would be better than using a Double.

1 Like

or Integer

You may also assume ints with 4 fixed decimals in some of your values as

“money1” : 1234567,
“money2” : 1234500

means currency 123.4567 and 123.45

Public Function BoxedAsInteger(extends value As Currency) As Int64
  // Read a Currency as an Int64 with the lower 4 digits being the decimal part
  Var mb As New MemoryBlock(8)
  mb.CurrencyValue(0) = value
  Return mb.Int64Value(0)
End Function


Public Function UnboxedAsCurrency(extends value As Int64) As Currency
  // Read an Int64 as a 4 decimal fixed point money value
  Var mb As New MemoryBlock(8)
  mb.Int64Value(0) = value
  Return mb.CurrencyValue(0)
End Function

:point_up:t2: Helper conversion methods

1 Like

why not convert the value to cents as integer so that there is never an issue?
Having floats/doubles, strings etc would only make things harder.
Most banking apis use cents so that it’s easy to convert and present.

2 Likes

I think I will make a currency.toJson(jsonValue) method, and a fromJson
more strict with the variable types…

What this method will accomplish? The name does not help the understanding.

To short-circuit this conversation, here is the prior (very comprehensive) thread:

1 Like

convert like your’s to an accepted json type, I think it will be easier to convert to and back to a string.

1 Like

ok, for that you just need the already existing CurrencyVar.ToString(), and a StringVar.ToCurrency() as

Public Function ToCurrency(extends value As String) As Currency
  Return value.Val()
End Function

Sadly Xojo will use Doubles as intermediaries so your math gets limited to 15 digits (Currency uses Int64 and should allow 18)

this is for an accounting app (actually for multiple VAT storage) so it should be plenty…

1 Like

Have in mind that those 15 digits are 99,999,999,999.9999 (99… billions) and it is too small for accounting in Brazil, as gov numbers reaches Trillions. For small businesses it is ok even here. :rofl:

But doubles are not enough anymore for gov numbers.