Angular HttpClient无法正确反序列化Scala类

时间:2019-04-13 20:49:24

标签: angular scala typescript traits angular-httpclient

我在trait中有以下classesScala

sealed trait Algorithm {
  val name: String
  val formula: String
  val parameters: Seq[AlgorithmParameter[Any]]

  def enhanceAlgorithm[T](algorithm: T): Unit
}

case class LinearRegressionAlgorithm(override val name: String, override val formula: String) extends Algorithm {}
case class GeneralizedLinearRegressionAlgorithm(override val name: String, override val formula: String) extends Algorithm {}


sealed abstract class AlgorithmParameter[+T](val name: String, val value: T, val selectOptions: Seq[T]) extends EnumEntry

object AlgorithmParameter extends Enum[AlgorithmParameter[AnyVal]] with CirceEnum[AlgorithmParameter[AnyVal]] {

  case object MaxIter extends AlgorithmParameter[Int]("maxIter", 100, Seq())

}

我在TypeScript中创建了相应的类,如下所示:

export abstract class Algorithm {

  readonly name: string;
  readonly formula: string;
  readonly parameters: AlgorithmParameter<any>[];

  protected constructor(name: string, formula: string, parameters: AlgorithmParameter<any>[]) {
    this.name = name;
    this.formula = formula;
    this.parameters = parameters;
  }
}

export class LinearRegressionAlgorithm extends Algorithm {}
export class GeneralizedLinearRegressionAlgorithm extends Algorithm {}

export class AlgorithmParameter<T> {

  readonly name: string;
  readonly value: T;
  readonly selectOptions: T[];

 constructor(name: string, value: T, selectOptions: T[]) {
   this.name = name;
   this.value = value;
   this.selectOptions = selectOptions;
 }
}

我正在做的是向后端(Scala部分)发出REST请求,该请求返回一个Seq[Algorithm]类型的序列,我希望响应将被正确地转换为Typescript类,但是它不会't

REST方法如下:

recommend<T extends Algorithm>(body: RecommenderRequest): Observable<T[]> {

 return this.http.post<T[]>(environment.baseUrl + this.recommenderPath + this.algoRecommendationsPath, JSON.stringify(body))
  .catch((error: any) => Observable.throw(error.json().error || 'Server error'))
}

响应被转换为如下所示的数组:

enter image description here

TS algorithm-objects似乎已经创建,但是它们封装在另一个对象中,我无法轻松访问

为了获得第一个算法的name,该调用如下所示: response[0].LinearRegressionAlgorithm.name,但我想以一种可以简单编写response[0].name

的方式创建Http-response数组

1 个答案:

答案 0 :(得分:4)

Angular不会创建这些类型的对象。只是被告知这就是键入。您的Scala程序正在返回显示的数据。您将需要对每个结果进行映射操作以获取正确的对象。

下面是一个示例:

recommend<T extends Algorithm>(body: RecommenderRequest): Observable<T[]> {
  return this.http.post<T[]>(environment.baseUrl + this.recommenderPath + this.algoRecommendationsPath, JSON.stringify(body)).pipe(
    map(algorithms => algorithms.map(algorithm => this.toAlgorithm(algorithm)),
    catchError(error => Observable.throw(error.json().error || 'Server error'))
  );
}

private toAlgorithm(algorithm: { [type: string]: Partial<Algorithm> }): Algorithm {
  const type = Object.keys(algorithm)[0];
  const name = algorithm[type].name;
  const formula = algorithm[type].formula;
  const parameters = (algorithm[type].parameters || [])
    .map(parameter => this.toParameter(parameter));

  switch (type) {
    case 'LinearRegressionAlgorithm':
      return new LinearRegressionAlgorithm(name, formula, parameters);

    case 'GeneralizedLinearRegressionAlgorithm':
      return new GeneralizedLinearRegressionAlgorithm(name, formula, parameters);

    default:
      throw new Error('Unsupported algorithm');
  }
}

private toParameter(paramerter: Partial<AlgorithmParameter<any>>): AlgorithmParameter<any> {
  return new AlgorithmParameter<any>(parameter.name, parameter.value, parameter.selectedOptions);
}

如果您不需要这些算法的实际实例,而只需要这些算法中的值,则可以将Algorithm类更改为接口,这要简单得多:

recommend<T extends Algorithm>(body: RecommenderRequest): Observable<T[]> {
  return this.http.post<T[]>(environment.baseUrl + this.recommenderPath + this.algoRecommendationsPath, JSON.stringify(body)).pipe(
    map(algorithms => algorithms.map(algorithm => Object.values(algorithm)[0]),
    catchError(error => Observable.throw(error.json().error || 'Server error'))
  );
}

通过这种方式进行操作,您将丢失有关算法类型的任何信息,因此您需要在该类型的算法接口上添加另一个属性,并在上述映射过程中进行设置。取决于您的需求。