Swan Lake Beta3 is here.
import ballerina/io;

type Coord record {
    float x;
    float y;
};

public function main() {
    json j = {x: 1.0, y: 2.0};

    // With mutable values, it would not be type-safe to allow a cast.
    // Cast to `T` will work on the mutable structure `s` only if the inherent type
    // of `s` is a subtype of `T`.
    // Casting of immutable value will work.
    json k = j.cloneReadOnly();
    Coord c = <Coord> k;

    io:println(c.x);
    io:println(c.y);
}

Converting From JSON to User-Defined Type

With mutable values, would not be type-safe to allow a cast. Mutable structures have the inherent type that limits mutation. Cast to T will work on mutable structure s only if the inherent type of s is a subtype of T. Casting of immutable value will work but it does not do numeric conversions.

import ballerina/io;
type Coord record {
    float x;
    float y;
};
public function main() {
    json j = {x: 1.0, y: 2.0};
    json k = j.cloneReadOnly();
    Coord c = <Coord> k;

With mutable values, it would not be type-safe to allow a cast. Cast to T will work on the mutable structure s only if the inherent type of s is a subtype of T. Casting of immutable value will work.

    io:println(c.x);
    io:println(c.y);
}
bal run converting_from_json_to_user_defined_type.bal
1.0
2.0
import ballerina/io;

type Coord record {
    float x;
    float y;
};

public function main() {
    json j = {x: 1.0, y: 2.0};

    // With mutable values, it would not be type-safe to allow a cast.
    // Cast to `T` will work on the mutable structure `s` only if the inherent type
    // of `s` is a subtype of `T`.
    // Casting of immutable value will work.
    json k = j.cloneReadOnly();
    Coord c = <Coord> k;

    io:println(c.x);
    io:println(c.y);
}

Converting From JSON to User-Defined Type

With mutable values, would not be type-safe to allow a cast. Mutable structures have the inherent type that limits mutation. Cast to T will work on mutable structure s only if the inherent type of s is a subtype of T. Casting of immutable value will work but it does not do numeric conversions.

import ballerina/io;
type Coord record {
    float x;
    float y;
};
public function main() {
    json j = {x: 1.0, y: 2.0};
    json k = j.cloneReadOnly();
    Coord c = <Coord> k;

With mutable values, it would not be type-safe to allow a cast. Cast to T will work on the mutable structure s only if the inherent type of s is a subtype of T. Casting of immutable value will work.

    io:println(c.x);
    io:println(c.y);
}
bal run converting_from_json_to_user_defined_type.bal
1.0
2.0
Subscribe to the newsletter

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