1.4.2 • Published 8 days ago

@inertiagroup/apex v1.4.2

Weekly downloads
-
License
ISC
Repository
-
Last release
8 days ago

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);
1.4.2

8 days ago

1.4.1

28 days ago

1.4.0

28 days ago

1.3.9

2 months ago

1.3.8

4 months ago

1.3.7

4 months ago

1.3.6

4 months ago

1.3.5

4 months ago

1.3.4

4 months ago

1.3.3

6 months ago

1.3.2

6 months ago

1.3.1

6 months ago

1.3.0

6 months ago

1.2.2

6 months ago

1.2.1

6 months ago

1.2.0

7 months ago

1.1.3

7 months ago

1.1.2

7 months ago

1.1.1

7 months ago

1.1.0

7 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago