Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
aim-json-mapper
Advanced tools
deserialize(Class, json, isArray) Class: 目标类 json: jsonObject或者jsonArray isArray: 表明json是数组或者object returns: 如果json为array,返回对应类的数组,如果json为object,返回对应类的实例。
serialize(data, isArray) data: 待序列化的数据(可以为数组) returns: data为class的instance时返回json object, data 为array时,返回json array
import { JsonProperty, deserialize, serialize } from '_/lang/jsonMapper';
//json object
const j = {
a: 1,
b: "",
c: { d: 3, e: -1 },
cs: [{ d: 4 }, { d: 5 }],
d: 33
}
//json array
const jArray = [{
a: 1,
b: "",
c: { d: 3 }
cs: [{ d: 4, e: -2 }, { d: 5, e: 21 }],
d: 33
}, {
a: 11,
b: "22",
c: { d: 33 },
cs: [{ d: 333 }, { d: 533 }],
d: 44
}]
//对应类定义
//在需要进行序列化和反序列化的字段上加上注解。
class A {
@JsonProperty()
a: number;
@JsonProperty()
b: string;
@JsonProperty({ excludeToJson: true }) //这个字段不会被序列化到json中
c: B;
@JsonProperty({ class: B }) // 必须写明类型,
cs: B[]
@JsonProperty('d') //将json的映射为 md
private md: number;
}
class B {
@JsonProperty()
d: number;
@JsonProperty({ default: 0 }) //因为允许json中不存在这个值,所以声明默认值,若不声明解析将会被认为类型不匹配,因此会导致解析失败
e: number;
@JsonProperty({ class: Number, default: null }) //形如 A | B这种的类型声明也必须写明类型,暂时不支持多类型匹配,最好不要写这种。
f: number | null;
}
//反序列化
const a = deserialize(A, j); //a is A
const t = deserialize(A, jArray, true); // t is A[], 注意:第三个参数为true仅仅为了ts的类型推导不出错,是否反序列化成数组由第二个参数决定,所以纯js使用时,只要第二个参数是数组,返回值就会是一个数组,为true时会让ts将返回值推导为A[]
//序列化
const aj = serialize(a); // aj is an object
const tj = serialize(t, true); // tj an array, 第二个参数同理;
FAQs
数据序列化/反序列化
We found that aim-json-mapper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.