Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement for record deserialization/serialization from/to json #528

Open
Bhashinee opened this issue May 25, 2020 · 2 comments
Open

Improvement for record deserialization/serialization from/to json #528

Bhashinee opened this issue May 25, 2020 · 2 comments
Assignees
Labels
Area/Langext Relates to annotations providing language extras Type/Improvement Enhancement to language design
Milestone

Comments

@Bhashinee
Copy link
Member

Bhashinee commented May 25, 2020

Description:
In the current JSON to record conversion with .constructFrom requires exact matches in field names. Because of this restriction at certain times, we need to manually go through the JSON, retrieve and update the record. It would be good if we can configure the mapping json key for a particular field in the record beforehand and do the conversion using 'constructFrom'. 
For example, in the following record, I need to map JSON's unit_amount field to record's 'unitAmount' field and unit_amount_decimal to 'unitAmountDecimal'. 

Ex: 

public type PriceData record { 
      int? unitAmount?; // `json:"unit_amount"` 
     float? unitAmountDecimal?; // `json:"unit_amount_decimal"`
};

This will be useful when dealing with json payloads coming from third-party servers because some JSON responses have fields with underscores.

@jclark
Copy link
Collaborator

jclark commented May 26, 2020

This would be useful functionality, which should probably be combined with #239, but it doesn't belong in constructFrom: it should be driven by annotations, so it needs to be a stdlib function not a langlib.

@jclark jclark added the Area/Langext Relates to annotations providing language extras label May 26, 2020
@jclark jclark added this to the 2020R4 milestone May 26, 2020
@jclark jclark self-assigned this May 26, 2020
@jclark jclark added the Type/Improvement Enhancement to language design label May 26, 2020
@daneshk
Copy link
Member

daneshk commented May 26, 2020

@jclark I am trying to understand how we could support this as a stdlib function. Yes, we can pass this additional data using annotations, something like,

public type PriceData record {
    @jsonElement { name = "unit_amount" }; 
    int? unitAmount?; // `json:"unit_amount"`

    @jsonElement { name = "unit_amount_decimal" };
    float? unitAmountDecimal?; // `json:"unit_amount_decimal"`
};

Is the stdlib function, a util function which accepts record type[may be we can infer it from return type] and JSON value as input params and returns an instance of the record with populated values. something like,

PriceData priceData = check jsonutils:toAnyData(jsonValue, type = PriceData);

json value = check jsonutils:fromAnyData(priceData)

We are also trying to get rid of jsonutils, xmlutils, stringutils modules. I wonder where we should place these functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Langext Relates to annotations providing language extras Type/Improvement Enhancement to language design
Projects
None yet
Development

No branches or pull requests

3 participants