Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
JSON numbers
Ballerina has three numeric types; but JSON has one. The json
type allows int|float|decimal
. toJsonString()
will convert int|float|decimal
into JSON numeric syntax. fromJsonString()
converts JSON numeric syntax into int
, if possible, and otherwise decimal
.
cloneWithType()
or ensureType()
will convert from int
or decimal
into the user's chosen numeric type. The net result is that you can use JSON to exchange the full range of all three Ballerina numeric types. -0
is an edge case: it is represented as a float
.
import ballerina/io;
public function main() returns error? {
int a = 1;
float b = 2.1;
decimal c = 3.24;
// The `json` type allows `int|float|decimal`.
json[] d = [a, b, c];
// `toJsonString()` will convert `int|float|decimal` into the JSON numeric
// syntax.
string e = d.toJsonString();
io:println(e);
// `fromJsonString()` converts JSON numeric syntax into `int`, if possible
// and `decimal` otherwise.
json f = check e.fromJsonString();
io:println(f);
json[] g = <json[]>f;
io:println(typeof g[0]);
io:println(typeof g[1]);
io:println(typeof g[2]);
// `cloneWithType()` or `ensureType()` will convert from `int` or `decimal`
// into the user's chosen numeric type.
float h = check g[2].ensureType();
io:println(h);
// `-0` is an edge case: represented as `float`.
string i = "-0";
io:println(typeof check i.fromJsonString());
}
$ bal run json_numbers.bal[1, 2.1, 3.24][1,2.1,3.24]typedesc 1typedesc 2.1typedesc 3.243.24typedesc -0.0
Related links
PreviousResource method typing
NextJSON to record