Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit d47a9d9

Browse files
committed
Remove jsog specific functionality
1 parent c21e136 commit d47a9d9

File tree

11 files changed

+607
-395
lines changed

11 files changed

+607
-395
lines changed

package-lock.json

Lines changed: 581 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import the module
2-
export * from './module/Jsog';
2+
export * from './service/JsonTypescriptService';
33

44
// Import the decorator used by the module
55
export * from './decorators/JsonProperty';

src/main/model/IdentifiedObject.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/model/JsogEntry.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/model/JsogReference.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* Object in a JavaScript Object Graph
33
*/
4-
export interface JsogObject {
5-
'@id': string;
4+
export interface JsonObject {
65
[property: string]: any;
76
}
Lines changed: 16 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import { Class } from '../support/Class';
44
import { Map } from '../support/Map';
55

66
// Model
7-
import { IdentifiedObject } from '../model/IdentifiedObject';
8-
import { JsogObject } from '../model/JsogObject';
9-
import { JsogReference } from '../model/JsogReference';
10-
import { JsogEntry } from '../model/JsogEntry';
7+
import { JsonObject } from '../model/JsonObject';
118

129
// Logger
1310
import { Logger } from '../log/Logger';
@@ -18,7 +15,7 @@ import { Logger } from '../log/Logger';
1815
* Instances of this class can be used to serialize and deserialize Objects
1916
* Graphs to and from Json.
2017
*/
21-
export class JsogService {
18+
export class JsonTypescriptService {
2219

2320
// ------------------------------------------------------------------------
2421
// Private fields
@@ -29,21 +26,6 @@ export class JsogService {
2926
// Public fields
3027
// ------------------------------------------------------------------------
3128

32-
/**
33-
* Property Key for JavaScript Object Graph References.
34-
*/
35-
public refKey: string = '@ref';
36-
/**
37-
* Property key where JavaScript Object Graph Identifieres.
38-
*/
39-
public idKey: string = '@id';
40-
41-
/**
42-
* Temporary added property key to store JavaScript Object Graph
43-
* Identifiers while serializing.
44-
*/
45-
private identifiedObjectKey: string = '__jsogObjectId';
46-
4729
/**
4830
* Flag to enable debug output.
4931
*/
@@ -93,65 +75,17 @@ export class JsogService {
9375
return Reflect.getMetadata('design:type', target, propertyKey);
9476
}
9577

96-
/**
97-
* Get the JavaScript Object Graph Identifiere of object.
98-
*/
99-
private getJsogId(object: JsogObject): string {
100-
let id = object[this.idKey];
101-
// be defensive if someone uses numbers in violation of the spec
102-
if (id) {
103-
id = id.toString();
104-
}
105-
return id;
106-
}
107-
108-
/**
109-
* Get the JavaScript Object Reference of object.
110-
*/
111-
// TODO: Find a better way to type JsogReference.
112-
private getJsogRef(object: JsogReference | any): string {
113-
let ref = object[this.refKey];
114-
// Be defensive if someone uses numbers in violation of the spec
115-
if (ref) {
116-
ref = ref.toString();
117-
}
118-
return ref;
119-
}
120-
12178
/**
12279
* Serialize an Object to a JavaScript Object Graph.
12380
*
12481
* If an Object in the Graph has a custom toJSON function this is used for serialization.
12582
*
12683
* @param object to serialize.
12784
*/
128-
public serialize<T>(object: T): JsogObject & T {
85+
public serialize<T>(object: T): T {
12986
this.log.debug('serialize:');
13087
this.log.debug(object);
13188

132-
/**
133-
* Reset nextId to one.
134-
*/
135-
let nextId = 1;
136-
137-
// Serialized objects by id.
138-
const serializedById: JsogEntry[] = [];
139-
// Holds references to all touched objects.
140-
const allOriginal: IdentifiedObject[] = [];
141-
142-
/**
143-
* Get the JSOG id of one object. Set the id if neccesarry.
144-
*
145-
* @param obj Object in object graph.
146-
*/
147-
const getIdOf = (obj: IdentifiedObject): number => {
148-
if (!obj.__jsogObjectId) {
149-
obj.__jsogObjectId = nextId++;
150-
allOriginal.push(obj);
151-
}
152-
return obj.__jsogObjectId;
153-
};
154-
15589
/**
15690
* Recursive Serialization function.
15791
*
@@ -164,38 +98,17 @@ export class JsogService {
16498
*
16599
* @param original An Object to Serialize.
166100
*/
167-
const serializeObject = <T extends IdentifiedObject>(original: T): JsogEntry => {
168-
const result: any = {};
169-
const id = getIdOf(original);
170-
171-
// If this object was already serialized
172-
// we return an JsogReference.
173-
if (serializedById[id]) {
174-
result[this.refKey] = id.toString();
175-
return result;
176-
}
177-
178-
// ... and hold a reference locally to find it later.
179-
serializedById[id] = result;
180-
// Serialize the object...
181-
for (const key of Object.keys(original)) {
182-
if (key !== this.identifiedObjectKey) {
183-
result[key] = serializeRecursive(original[key]);
184-
}
185-
}
186-
// ... add the JSOG key ...
187-
result[this.idKey] = id.toString();
188-
189-
return result;
101+
const serializeObject = (original: T): JsonObject => {
102+
return original;
190103
};
191104

192105
/**
193106
* Serialize every element of an array.
194107
*
195108
* @param array to serialize
196109
*/
197-
const serializeArray = (array: IdentifiedObject[]): JsogEntry[] => {
198-
const allEncoded: JsogEntry[] = [];
110+
const serializeArray = (array: T[]): JsonObject[] => {
111+
const allEncoded: JsonObject[] = [];
199112
for (const entry of array) {
200113
allEncoded.push(serializeRecursive(entry));
201114
}
@@ -216,15 +129,7 @@ export class JsogService {
216129
};
217130

218131
// Call recursive serialization.
219-
const result = serializeRecursive(object);
220-
221-
// Remove temporary object identifieres.
222-
allOriginal.forEach((element) => {
223-
delete element.__jsogObjectId;
224-
});
225-
this.clearArray(serializedById);
226-
227-
return result;
132+
return serializeRecursive(object);
228133
}
229134

230135
/**
@@ -233,7 +138,7 @@ export class JsogService {
233138
* @param jsogObject Array of JsogObjects.
234139
* @param clazz Class to instantiant the array entries with.
235140
*/
236-
public deserializeArray<T extends object>(jsogObject: JsogObject[], clazz?: Class<T>): T[] {
141+
public deserializeArray<T extends object>(jsogObject: T[], clazz?: Class<T>): T[] {
237142
return <T[]>this.deserialize(jsogObject, clazz);
238143
}
239144

@@ -243,11 +148,11 @@ export class JsogService {
243148
* @param jsogObject JavaScript Object Graph root.
244149
* @param clazz Class to intantiate the root with.
245150
*/
246-
public deserializeObject<T extends object>(jsogObject: JsogObject, clazz?: Class<T>): T {
151+
public deserializeObject<T extends object>(jsogObject: T, clazz?: Class<T>): T {
247152
return <T>this.deserialize(jsogObject, clazz);
248153
}
249154

250-
public deserialize<T extends object>(jsogObject: JsogObject | JsogObject[], classObject?: Class<T>): T | T[] {
155+
public deserialize<T extends object>(jsogObject: T | T[], classObject?: Class<T>): T | T[] {
251156
this.log.debug('deserialize:');
252157
this.log.debug(jsogObject);
253158
this.log.debug('as class:');
@@ -256,42 +161,27 @@ export class JsogService {
256161
// Map of found objects by identifier.
257162
const found: Map<object> = {};
258163

259-
const deserializeRecursive = <T extends any>(jsogObject: JsogObject | JsogObject[],
164+
const deserializeRecursive = <T extends any>(jsogObject: T | T[],
260165
classObject?: Class<T>): T | T[] => {
261166
this.log.debug('deserializeRecursive:');
262167
this.log.debug(jsogObject);
263168
this.log.debug('as class:');
264169
this.log.debug(classObject);
265170

266-
const deserializeObject = <T extends object>(jsogObject: JsogObject, classObject?: Class<T>): T => {
171+
const deserializeObject = <T extends object>(jsogObject: JsonObject, classObject?: Class<T>): T => {
267172
this.log.debug('deserializeObject:');
268173
this.log.debug(jsogObject);
269174
this.log.debug('as class:');
270175
this.log.debug(classObject);
271176

272-
const ref = this.getJsogRef(jsogObject);
273-
if (ref) {
274-
this.log.debug('Reference to ' + ref + ' found.');
275-
return <T>found[ref];
276-
}
277-
278177
let result: any;
279178
if (classObject) {
280179
result = new classObject();
281180
} else {
282181
result = {};
283182
}
284183

285-
const id = this.getJsogId(jsogObject);
286-
if (id) {
287-
found[id] = result;
288-
}
289-
290184
for (const key of Object.keys(jsogObject)) {
291-
if (key === this.idKey) {
292-
continue;
293-
}
294-
295185
if (Reflect.hasMetadata('JsonProperty', result, key)) {
296186
let clazz = Reflect.getMetadata('JsonProperty', result, key);
297187
if (!clazz) {
@@ -307,7 +197,7 @@ export class JsogService {
307197
return result;
308198
};
309199

310-
const deserializeArray = <T extends object>(jsogObject: JsogObject[], classObject?: Class<T>): T[] => {
200+
const deserializeArray = <T extends object>(jsogObject: JsonObject[], classObject?: Class<T>): T[] => {
311201
const result: T[] = [];
312202
for (const value of jsogObject) {
313203
// We now this cast is true.
@@ -323,13 +213,13 @@ export class JsogService {
323213
// Deserialize Arrays.
324214
if (this.isArray(jsogObject)) {
325215
// We know this cast ist ture.
326-
jsogObject = <JsogObject[]>jsogObject;
216+
jsogObject = <T[]>jsogObject;
327217
return deserializeArray(jsogObject, classObject);
328218
}
329219
// Deserialize Objets
330220
if (typeof jsogObject === 'object') {
331221
// We know this cast ist ture.
332-
jsogObject = <JsogObject>jsogObject;
222+
jsogObject = <T>jsogObject;
333223
return deserializeObject(jsogObject, classObject);
334224
}
335225

0 commit comments

Comments
 (0)