使用 'constructFrom' 方法将 json 转换为记录时出错
Error in converting a json to a Record using 'constructFrom' method
尝试使用 'constructFrom' 方法将 json 转换为记录时出现以下错误。
error {ballerina/lang.typedesc}ConversionError message='map<json>' value cannot be converted to 'news_impl:RolelessUser'
我的方法有什么问题?我在下面 post 编辑了我的示例(修改为 post)。
import model;
import ballerina/io;
import ballerina/jsonutils;
type AuthDAO object {
public function getUserInfo(string userEmail) returns (model:User) {
// This is defined to compare the the errorneous result with this successful one
RolelessUser x1 = {
userId: "qqqwww",
emailAddress: "sam@gmail.com",
firstName: "sammm",
lastName: "rrr",
age: "33",
nic: "1212121",
dateAdded: "2020-04-19",
publicKey: "ssddff"
};
RolelessUser|error rolelessUserRecord1 = RolelessUser.constructFrom(x1);
io:println("rolelessUserRecord:", rolelessUserRecord1);
model:User user = {};
json[] userInfoJson = [];
io:println("in DAO: email:", userEmail);
var userInfoTable = testDB->select(GET_USER_INFO, RolelessUser, userEmail);
io:println("userInfoTable:" + userInfoTable.toString());
if (userInfoTable is table<model:RolelessUser>) {
userInfoJson = <json[]>jsonutils:fromTable(userInfoTable);
io:println("JSON is josn 1: ", userInfoJson[0]);
io:println("Test JSON is josn 2: ", x1);
io:println("Roleless User Info: ", userInfoJson[0]);
// this works
RolelessUser|error rolelessUserRecordTest = RolelessUser.constructFrom(x1);
io:println("Test rolelessUser Record:", rolelessUserRecordTest);
// this FAILS
RolelessUser|error rolelessUserRecord = RolelessUser.constructFrom(userInfoJson[0]);
io:println("rolelessUser Record:", rolelessUserRecord);
model:Role[]|error rolesArray = [];
var userRolesTable = testDB->select(GET_USER_ROLES, model:Role, userEmail);
json[] rolesJsonArray = [];
if (userRolesTable is table<model:Role>) {
rolesJsonArray = <json[]>jsonutils:fromTable(userRolesTable);
rolesArray = model:Role[].constructFrom(rolesJsonArray);
if (rolesArray is model:Role[]) {
io:println("User Roles:", rolesArray);
}
}
if (rolelessUserRecord is model:RolelessUser) {
io:println("YYY");
user.firstName = rolelessUserRecord.firstName;
user.lastName = rolelessUserRecord.lastName;
user.emailAddress = rolelessUserRecord.emailAddress;
user.age = rolelessUserRecord.age;
user.nic = rolelessUserRecord.nic;
io:println("reolseArray:", rolesArray);
if (rolesArray is model:Role[]) {
user.roles = rolesArray;
}
}
return user;
}
return user;
}
};
type RolelessUser record {
string userId = "";
string emailAddress = "";
string firstName = "";
string lastName = "";
string age = "";
string nic = "";
string dateAdded = "";
string publicKey = "";
};
调用 getUserInfo() 方法时出现上述错误的输出如下。
rolelessUserRecord:userId=qqqwww emailAddress=sam@gmail.com firstName=sammm lastName=rrr age=33 nic=1212121 dateAdded=2020-04-19 publicKey=ssddff
in DAO: email:sam@gmail.com
userInfoTable:
JSON is josn 1: userId=qqqwww emailAddress=sam@gmail.com firstName=sammm lastName=dddk age=23 nic=1212121 dateAdded=2020-04-19 publicKey=ssddff
Test JSON is josn 2: userId=qqqwww emailAddress=sam@gmail.com firstName=sammm lastName=rrr age=33 nic=1212121 dateAdded=2020-04-19 publicKey=ssddff
Roleless User Info: userId=qqqwww emailAddress=sam@gmail.com firstName=sammm lastName=dddk age=23 nic=1212121 dateAdded=2020-04-19 publicKey=ssddff
Test rolelessUser Record:userId=qqqwww emailAddress=sam@gmail.com firstName=sammm lastName=rrr age=33 nic=1212121 dateAdded=2020-04-19 publicKey=ssddff
rolelessUser Record:error {ballerina/lang.typedesc}ConversionError message='map<json>' value cannot be converted to 'news_impl:RolelessUser'
User Roles:role_name=writer role_name=reader
Got userr info:roles= userId= emailAddress= firstName= lastName= age= nic= dateAdded= publicKey=
这通常是由于预期的字段类型与 JSON 对象或 missing/additional 字段中条目的实际类型不兼容。由于所有字段似乎都出现在 JSON 中,在这种情况下可能是前者。
为了缩小问题范围,您可以从 JSON 对象访问各个字段并检查每个字段的类型是否为预期类型。
例如,
json jsonObj = userInfoJson[0];
io:println(jsonObj.userId is string); // and so on
您也可以打印出 userInfoJson[0].toJsonString()
以获得格式正确的 JSON,这也可能有助于查明问题。
有一个 issue 可以跟踪改进此类 .constructFrom()
失败的错误。
尝试使用 'constructFrom' 方法将 json 转换为记录时出现以下错误。
error {ballerina/lang.typedesc}ConversionError message='map<json>' value cannot be converted to 'news_impl:RolelessUser'
我的方法有什么问题?我在下面 post 编辑了我的示例(修改为 post)。
import model;
import ballerina/io;
import ballerina/jsonutils;
type AuthDAO object {
public function getUserInfo(string userEmail) returns (model:User) {
// This is defined to compare the the errorneous result with this successful one
RolelessUser x1 = {
userId: "qqqwww",
emailAddress: "sam@gmail.com",
firstName: "sammm",
lastName: "rrr",
age: "33",
nic: "1212121",
dateAdded: "2020-04-19",
publicKey: "ssddff"
};
RolelessUser|error rolelessUserRecord1 = RolelessUser.constructFrom(x1);
io:println("rolelessUserRecord:", rolelessUserRecord1);
model:User user = {};
json[] userInfoJson = [];
io:println("in DAO: email:", userEmail);
var userInfoTable = testDB->select(GET_USER_INFO, RolelessUser, userEmail);
io:println("userInfoTable:" + userInfoTable.toString());
if (userInfoTable is table<model:RolelessUser>) {
userInfoJson = <json[]>jsonutils:fromTable(userInfoTable);
io:println("JSON is josn 1: ", userInfoJson[0]);
io:println("Test JSON is josn 2: ", x1);
io:println("Roleless User Info: ", userInfoJson[0]);
// this works
RolelessUser|error rolelessUserRecordTest = RolelessUser.constructFrom(x1);
io:println("Test rolelessUser Record:", rolelessUserRecordTest);
// this FAILS
RolelessUser|error rolelessUserRecord = RolelessUser.constructFrom(userInfoJson[0]);
io:println("rolelessUser Record:", rolelessUserRecord);
model:Role[]|error rolesArray = [];
var userRolesTable = testDB->select(GET_USER_ROLES, model:Role, userEmail);
json[] rolesJsonArray = [];
if (userRolesTable is table<model:Role>) {
rolesJsonArray = <json[]>jsonutils:fromTable(userRolesTable);
rolesArray = model:Role[].constructFrom(rolesJsonArray);
if (rolesArray is model:Role[]) {
io:println("User Roles:", rolesArray);
}
}
if (rolelessUserRecord is model:RolelessUser) {
io:println("YYY");
user.firstName = rolelessUserRecord.firstName;
user.lastName = rolelessUserRecord.lastName;
user.emailAddress = rolelessUserRecord.emailAddress;
user.age = rolelessUserRecord.age;
user.nic = rolelessUserRecord.nic;
io:println("reolseArray:", rolesArray);
if (rolesArray is model:Role[]) {
user.roles = rolesArray;
}
}
return user;
}
return user;
}
};
type RolelessUser record {
string userId = "";
string emailAddress = "";
string firstName = "";
string lastName = "";
string age = "";
string nic = "";
string dateAdded = "";
string publicKey = "";
};
调用 getUserInfo() 方法时出现上述错误的输出如下。
rolelessUserRecord:userId=qqqwww emailAddress=sam@gmail.com firstName=sammm lastName=rrr age=33 nic=1212121 dateAdded=2020-04-19 publicKey=ssddff
in DAO: email:sam@gmail.com
userInfoTable:
JSON is josn 1: userId=qqqwww emailAddress=sam@gmail.com firstName=sammm lastName=dddk age=23 nic=1212121 dateAdded=2020-04-19 publicKey=ssddff
Test JSON is josn 2: userId=qqqwww emailAddress=sam@gmail.com firstName=sammm lastName=rrr age=33 nic=1212121 dateAdded=2020-04-19 publicKey=ssddff
Roleless User Info: userId=qqqwww emailAddress=sam@gmail.com firstName=sammm lastName=dddk age=23 nic=1212121 dateAdded=2020-04-19 publicKey=ssddff
Test rolelessUser Record:userId=qqqwww emailAddress=sam@gmail.com firstName=sammm lastName=rrr age=33 nic=1212121 dateAdded=2020-04-19 publicKey=ssddff
rolelessUser Record:error {ballerina/lang.typedesc}ConversionError message='map<json>' value cannot be converted to 'news_impl:RolelessUser'
User Roles:role_name=writer role_name=reader
Got userr info:roles= userId= emailAddress= firstName= lastName= age= nic= dateAdded= publicKey=
这通常是由于预期的字段类型与 JSON 对象或 missing/additional 字段中条目的实际类型不兼容。由于所有字段似乎都出现在 JSON 中,在这种情况下可能是前者。
为了缩小问题范围,您可以从 JSON 对象访问各个字段并检查每个字段的类型是否为预期类型。
例如,
json jsonObj = userInfoJson[0];
io:println(jsonObj.userId is string); // and so on
您也可以打印出 userInfoJson[0].toJsonString()
以获得格式正确的 JSON,这也可能有助于查明问题。
有一个 issue 可以跟踪改进此类 .constructFrom()
失败的错误。