Swan Lake Beta2 is here.
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 JSON 
    // numeric syntax.
    string e = d.toJsonString();

    io:println(e);

    // `fromJsonString` converts JSON numeric syntax into `int`, 
    // if possible, and otherwise `decimal`.
    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());
}

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 user’s chosen numeric type. Net result is that you can use json to exchange full range of all three Ballerina numeric types. -0 is an edge case: represented as float.

import ballerina/io;
public function main() returns error? {
    int a = 1;
    float b = 2.1;
    decimal c = 3.24;
    json[] d = [a, b, c];

The json type allows int|float|decimal.

    string e = d.toJsonString();

toJsonString will convert int|float|decimal into JSON numeric syntax.

    io:println(e);
    json f = check e.fromJsonString();

fromJsonString converts JSON numeric syntax into int, if possible, and otherwise decimal.

    io:println(f);
    json[] g = <json[]> f;
    io:println(typeof g[0]);
    io:println(typeof g[1]);
    io:println(typeof g[2]);
    float h = check g[2].ensureType();

cloneWithType or ensureType will convert from int or decimal into the user’s chosen numeric type.

    io:println(h);
    string i = "-0";

-0 is an edge case: represented as float.

    io:println(typeof check i.fromJsonString());
}
bal run json_numbers.bal
[1, 2.1, 3.24]
[1,2.1,3.24]
typedesc int
typedesc decimal
typedesc decimal
3.24
typedesc 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 JSON 
    // numeric syntax.
    string e = d.toJsonString();

    io:println(e);

    // `fromJsonString` converts JSON numeric syntax into `int`, 
    // if possible, and otherwise `decimal`.
    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());
}

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 user’s chosen numeric type. Net result is that you can use json to exchange full range of all three Ballerina numeric types. -0 is an edge case: represented as float.

import ballerina/io;
public function main() returns error? {
    int a = 1;
    float b = 2.1;
    decimal c = 3.24;
    json[] d = [a, b, c];

The json type allows int|float|decimal.

    string e = d.toJsonString();

toJsonString will convert int|float|decimal into JSON numeric syntax.

    io:println(e);
    json f = check e.fromJsonString();

fromJsonString converts JSON numeric syntax into int, if possible, and otherwise decimal.

    io:println(f);
    json[] g = <json[]> f;
    io:println(typeof g[0]);
    io:println(typeof g[1]);
    io:println(typeof g[2]);
    float h = check g[2].ensureType();

cloneWithType or ensureType will convert from int or decimal into the user’s chosen numeric type.

    io:println(h);
    string i = "-0";

-0 is an edge case: represented as float.

    io:println(typeof check i.fromJsonString());
}
bal run json_numbers.bal
[1, 2.1, 3.24]
[1,2.1,3.24]
typedesc int
typedesc decimal
typedesc decimal
3.24
typedesc float

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand