import ballerina/io;

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

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

    // Argument is a typedesc value.
    // Static return type depends on argument.
    Coord c = check j.cloneWithType(Coord);
    io:println(c.x);

    // Argument defaulted from context.
    Coord d = check j.cloneWithType();
    io:println(d.x);
}

Converting to User-Defined Type

The cloneWithType langlib function in the lang.value module can be used to convert a value to a user-defined type. Result recursively uses specified type as inherent type of new value. Automatically performs numeric conversions as necessary. Every part of value is cloned, including immutable structural values. Graph structure is not preserved. Variant fromJsonWithType also does reverse of conversions done by toJson.

import ballerina/io;
type Coord record {
    float x;
    float y;
};
public function main() returns error? {
    json j = {x: 1.0, y: 2.0};
    Coord c = check j.cloneWithType(Coord);
    io:println(c.x);

Argument is a typedesc value. Static return type depends on argument.

    Coord d = check j.cloneWithType();
    io:println(d.x);
}

Argument defaulted from context.

bal run converting_to_user_defined_type.bal
1.0
1.0