@@ -103,7 +103,6 @@ export type ClassEncoding = 'string' | 'object';
103
103
*/
104
104
export class ClassConverter {
105
105
private toClass = new Map < string , [ IOClass , ClassEncoding ] > ( ) ;
106
- private toName = new Map < IOClass , [ string , ClassEncoding ] > ( ) ;
107
106
108
107
/**
109
108
* Create a class converter from a table of classes.
@@ -140,7 +139,6 @@ export class ClassConverter {
140
139
( class_ as StringIOClass ) [ 'fromString' ] || ( class_ as ObjIOClass ) [ 'fromJSON' ] ,
141
140
`Class ${ type } must define a fromString() OR fromJSON() static method.` ,
142
141
) ;
143
- this . toName . set ( class_ , [ type , encoding ] ) ;
144
142
this . toClass . set ( type , [ class_ , encoding ] ) ;
145
143
}
146
144
@@ -153,12 +151,13 @@ export class ClassConverter {
153
151
return this . toClass . has ( type ) ;
154
152
}
155
153
/**
156
- * Is this class object registered?
154
+ * Is this class object registered under its constructor name ?
157
155
* @param obj - The class object.
158
156
* @returns If it is a registered class.
159
157
*/
160
158
isRegisteredClass ( obj : any ) {
161
- return this . toName . has ( obj ) ;
159
+ const name = obj . prototype . constructor . name ;
160
+ return this . isRegisteredClassName ( name ) ;
162
161
}
163
162
/**
164
163
* Convert a JSON-like object to a class object.
@@ -182,10 +181,11 @@ export class ClassConverter {
182
181
* @returns The class object.
183
182
*/
184
183
toJsonObj ( classObj : any ) : JsonEncodedClass | StringEncodedClass {
185
- const result = this . toName . get ( classObj . constructor ) ;
184
+ const type = classObj . constructor . name ;
185
+ const result = this . toClass . get ( type ) ;
186
186
assert ( result , `Could not find class in lookup.` ) ;
187
- const [ type , encoding ] = result ;
187
+ const [ _class , encoding ] = result ;
188
188
const data = encoding === 'string' ? classObj . toString ( ) : classObj . toJSON ( ) ;
189
- return { type : type ! , data } ;
189
+ return { type, data } ;
190
190
}
191
191
}
0 commit comments