Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Serialization Improvements #936

Merged
merged 20 commits into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/rebuild_specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs-extra');
const path = require('path');
const TypeDoc = require(path.join(__dirname, '..'));
const TypeDoc = require('..');

const app = new TypeDoc.Application({
mode: 'Modules',
Expand Down Expand Up @@ -38,7 +38,7 @@ fs.remove(path.join(__dirname, '../src/test/renderer/specs'))
const src = app.expandInputFiles([ fullPath ]);
const out = path.join(fullPath, 'specs.json');
const result = app.convert(src);
const data = JSON.stringify(result.toObject(), null, ' ')
const data = JSON.stringify(app.serializer.toObject(result), null, ' ')
.split(TypeDoc.normalizePath(base))
.join('%BASE%');

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { Renderer } from './lib/output/renderer';
export { DefaultTheme } from './lib/output/themes/DefaultTheme';
export { NavigationItem } from './lib/output/models/NavigationItem';
export { UrlMapping } from './lib/output/models/UrlMapping';
export { JSONOutput } from './lib/serialization';
10 changes: 5 additions & 5 deletions src/lib/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ export class Application extends ChildableComponent<Application, AbstractCompone
constructor(options?: Object) {
super(DUMMY_APPLICATION_OWNER);

this.logger = new ConsoleLogger();
this.logger = new ConsoleLogger();
this.serializer = new Serializer();
this.converter = this.addComponent<Converter>('converter', Converter);
this.serializer = this.addComponent<Serializer>('serializer', Serializer);
this.renderer = this.addComponent<Renderer>('renderer', Renderer);
this.plugins = this.addComponent('plugins', PluginHost);
this.options = this.addComponent('options', Options);
this.renderer = this.addComponent<Renderer>('renderer', Renderer);
this.plugins = this.addComponent('plugins', PluginHost);
this.options = this.addComponent('options', Options);

this.bootstrap(options);
}
Expand Down
21 changes: 0 additions & 21 deletions src/lib/models/ReflectionCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,4 @@ export class ReflectionCategory {

return onlyOwnDocuments;
}

/**
* Return a raw object representation of this reflection category.
* @deprecated Use serializers instead
*/
toObject(): any {
const result = {
title: this.title
};

if (this.children) {
const children: any[] = [];
this.children.forEach((child) => {
children.push(child.id);
});

result['children'] = children;
}

return result;
}
}
31 changes: 0 additions & 31 deletions src/lib/models/ReflectionGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,35 +92,4 @@ export class ReflectionGroup {

return onlyOwnDocuments;
}

/**
* Return a raw object representation of this reflection group.
* @deprecated Use serializers instead
*/
toObject(): any {
const result = {
title: this.title,
kind: this.kind
};

if (this.children) {
const children: any[] = [];
this.children.forEach((child) => {
children.push(child.id);
});

result['children'] = children;
}

if (this.categories) {
const categories: any[] = [];
this.categories.forEach((category) => {
categories.push(category.toObject());
});

result['categories'] = categories;
}

return result;
}
}
24 changes: 0 additions & 24 deletions src/lib/models/comments/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,4 @@ export class Comment {
this.returns = comment.returns;
this.tags = comment.tags ? comment.tags.map((tag) => new CommentTag(tag.tagName, tag.paramName, tag.text)) : undefined;
}

/**
* Return a raw object representation of this comment.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = {};
if (this.shortText) {
result.shortText = this.shortText;
}
if (this.text) {
result.text = this.text;
}
if (this.returns) {
result.returns = this.returns;
}

if (this.tags && this.tags.length) {
result.tags = [];
this.tags.forEach((tag) => result.tags.push(tag.toObject()));
}

return result;
}
}
17 changes: 0 additions & 17 deletions src/lib/models/comments/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,4 @@ export class CommentTag {
this.paramName = paramName || '';
this.text = text || '';
}

/**
* Return a raw object representation of this tag.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = {
tag: this.tagName,
text: this.text
};

if (this.paramName) {
result.param = this.paramName;
}

return result;
}
}
2 changes: 2 additions & 0 deletions src/lib/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './reflections/index';
export * from './types/index';
export * from './comments/index';
export * from './sources/index';
export * from './ReflectionGroup';
export * from './ReflectionCategory';
60 changes: 0 additions & 60 deletions src/lib/models/reflections/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,66 +530,6 @@ export abstract class Reflection {
*/
traverse(callback: TraverseCallback) { }

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
const result: any = {
id: this.id,
name: this.name,
kind: this.kind,
kindString: this.kindString,
flags: {}
};

if (this.originalName !== this.name) {
result.originalName = this.originalName;
}

if (this.comment) {
result.comment = this.comment.toObject();
}

Object.getOwnPropertyNames(ReflectionFlags.prototype).forEach(name => {
const descriptor = Object.getOwnPropertyDescriptor(ReflectionFlags.prototype, name)!;
if (typeof descriptor.get === 'function' && this.flags[name] === true) {
result.flags[name] = true;
}
});

if (this.decorates) {
result.decorates = this.decorates.map((type) => type.toObject());
}

if (this.decorators) {
result.decorators = this.decorators.map((decorator) => {
const result: any = { name: decorator.name };
if (decorator.type) {
result.type = decorator.type.toObject();
}
if (decorator.arguments) {
result.arguments = decorator.arguments;
}
return result;
});
}

this.traverse((child, property) => {
if (property === TraverseProperty.TypeLiteral) {
return;
}
let name = TraverseProperty[property];
name = name.substr(0, 1).toLowerCase() + name.substr(1);
if (!result[name]) {
result[name] = [];
}
result[name].push(child.toObject());
});

return result;
}

/**
* Return a string representation of this reflection.
*/
Expand Down
43 changes: 0 additions & 43 deletions src/lib/models/reflections/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,47 +45,4 @@ export class ContainerReflection extends Reflection {
}
}
}

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to stub this with a call to the real serializer? This would help those who are looking for the serializer on the ProjectReflection.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible, yes, but I'm not sure it is a good idea. To make it work we have 2 options.

  1. Construct a new serializer whenever .toObject is called
  2. Add an application: Application property to every reflection so that it can access the real serializer.

There are problems with both.

  1. The new serializer will be missing any extra serializers added by plugins
  2. Requires changing every reflection, and reflections really shouldn't know about serializers anyways (at least, that's the theory with having separate serialization...)

Since .toObject has been deprecated for nearly 2 years, I feel pretty ok about just removing the method.

const result = super.toObject();

if (this.groups) {
const groups: any[] = [];
this.groups.forEach((group) => {
groups.push(group.toObject());
});

result['groups'] = groups;
}

if (this.categories) {
const categories: any[] = [];
this.categories.forEach((category) => {
categories.push(category.toObject());
});

if (categories.length > 0) {
result['categories'] = categories;
}
}

if (this.sources) {
const sources: any[] = [];
this.sources.forEach((source) => {
sources.push({
fileName: source.fileName,
line: source.line,
character: source.character
});
});

result['sources'] = sources;
}

return result;
}
}
46 changes: 0 additions & 46 deletions src/lib/models/reflections/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,52 +192,6 @@ export class DeclarationReflection extends ContainerReflection implements Defaul
super.traverse(callback);
}

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
let result = super.toObject();

if (this.type) {
result.type = this.type.toObject();
}

if (this.defaultValue) {
result.defaultValue = this.defaultValue;
}

if (this.overwrites) {
result.overwrites = this.overwrites.toObject();
}

if (this.inheritedFrom) {
result.inheritedFrom = this.inheritedFrom.toObject();
}

if (this.extendedTypes) {
result.extendedTypes = this.extendedTypes.map((t) => t.toObject());
}

if (this.extendedBy) {
result.extendedBy = this.extendedBy.map((t) => t.toObject());
}

if (this.implementedTypes) {
result.implementedTypes = this.implementedTypes.map((t) => t.toObject());
}

if (this.implementedBy) {
result.implementedBy = this.implementedBy.map((t) => t.toObject());
}

if (this.implementationOf) {
result.implementationOf = this.implementationOf.toObject();
}

return result;
}

/**
* Return a string representation of this reflection.
*/
Expand Down
18 changes: 0 additions & 18 deletions src/lib/models/reflections/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,6 @@ export class ParameterReflection extends Reflection implements DefaultValueConta
super.traverse(callback);
}

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
const result = super.toObject();

if (this.type) {
result.type = this.type.toObject();
}

if (this.defaultValue) {
result.defaultValue = this.defaultValue;
}

return result;
}

/**
* Return a string representation of this reflection.
*/
Expand Down
26 changes: 0 additions & 26 deletions src/lib/models/reflections/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,6 @@ export class SignatureReflection extends Reflection implements TypeContainer, Ty
super.traverse(callback);
}

/**
* Return a raw object representation of this reflection.
* @deprecated Use serializers instead
*/
toObject(): any {
const result = super.toObject();

if (this.type) {
result.type = this.type.toObject();
}

if (this.overwrites) {
result.overwrites = this.overwrites.toObject();
}

if (this.inheritedFrom) {
result.inheritedFrom = this.inheritedFrom.toObject();
}

if (this.implementationOf) {
result.implementationOf = this.implementationOf.toObject();
}

return result;
}

/**
* Return a string representation of this reflection.
*/
Expand Down
Loading