@inertiagroup/apex
TypeScript icon, indicating that this package has built-in type declarations

1.4.4 • Public • Published

The apex packages allows you do variuos stuff with JSON data and some IOC concepts

You can convert a POJO into a JSON liek this

@JsonObject()
class JustObject { @JsonMember() friends: JustObject2[]; }

@JsonObject()
class JustObject2 { @JsonMember() name: string; }

const object = new JustObject();
const friend2 = new JustObject2();
friend2.name = "Mike";
object.friends = [friend2];

const expected = { 
    friends: [{ 
        name: "Mike" 
    }] 
};

const json = ApexObjectMapper.write<JustObject>(object);

You can also take a JSON and convert to a POJO as follows:

@JsonObject()
class JustObject2 { @JsonMember() name: string; }

@JsonObject()
class JustObject {
    @JsonMember({
        decoder: (value: any[]) => value.map(v => ApexObjectMapper.read<JustObject2>(v, JustObject2)),
        encoder: (value: JustObject2[]) => value.map(v => ApexObjectMapper.write(v)),
    })
    friends: JustObject2[];
}

const json = { friends: [{ name: "Mike" }, { name: "John" }] };

const expected = new JustObject();
const friend2 = new JustObject2();
friend2.name = "Mike";
const friend3 = new JustObject2();
friend3.name = "John";
expected.friends = [friend2, friend3];

const object = ApexObjectMapper.read<JustObject>(json, JustObject);

expect(object).toEqual(expected);
@JsonObject()
class JustObject3 { @JsonMember() name: string; }

@JsonObject()
class JustObject2 { @JsonMember({ type: JustObject3 }) friend: JustObject3; }

@JsonObject()
class JustObject { @JsonMember({ type: JustObject2 }) friend: JustObject2; }

const json = { friend: { friend: { name: "Mike" } } };

const expected = new JustObject();
const friend2 = new JustObject2();
const friend3 = new JustObject3();
friend3.name = "Mike";
friend2.friend = friend3;
expected.friend = friend2;

const object = ApexObjectMapper.read<JustObject>(json, JustObject);

expect(object).toEqual(expected);

Package Sidebar

Install

npm i @inertiagroup/apex

Weekly Downloads

15

Version

1.4.4

License

ISC

Unpacked Size

62.1 kB

Total Files

80

Last publish

Collaborators

  • mzincube161