json-object-mapper icon indicating copy to clipboard operation
json-object-mapper copied to clipboard

NoAnnotationError while using ObjectMapper in Angular 2 app

Open prabharj opened this issue 7 years ago • 9 comments

Hi,

I'm trying to use the json-object-mapper library in my Angular2 app. I have installed it via npm and my component is defined as below:

import { ObjectMapper } from 'json-object-mapper';
import { Item } from './../../models/item.model';
import { Component, OnInit,Inject } from '@angular/core';

@Component({
  selector: 'app-results-content',
  templateUrl: './results-content.component.html',
  styleUrls: ['./results-content.component.css']
})
export class ResultsContentComponent implements OnInit {

  constructor() { }

  ngOnInit() {

   let json =  {
          "Result": null, 
          "ResultId" : 2041, 
          "AnnotationText" : "hould",       
          "Confidence" : "0.4"
        };
   let testInstance: Item = ObjectMapper.deserialize(Item, json);
      }

}

When I try to run the app, I keep getting an error like : "NoAnnotationError" Error: Cannot resolve all parameters for 'Parser'(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'Parser' is decorated with Injectable.

What can I do to resolve this?

My Item class is as below:

import {JsonProperty} from 'json-object-mapper';

export class Item {

    @JsonProperty('Result')
    result: string;

    @JsonProperty('ResultId')
    resultId: number;

    @JsonProperty('AnnotationText')
    annotationText: string;

     @JsonProperty('Confidence')
    confidence:string;
}

prabharj avatar Mar 29 '17 09:03 prabharj

Hi there,

I don't think it's an issue with this library, it's more to do with Angular and Reflect library loading - https://github.com/angular/angular/issues/13609. I have tested the library in NG versions 2.0.4, 2.2.0 and 2.4.10 - none had any issues at all.

Can u try commenting out the line let testInstance: Item = ObjectMapper.deserialize(Item, json); and see if the problem still exists?

Thanks.

shakilsiraj avatar Mar 29 '17 11:03 shakilsiraj

Yes it works if I comment out that line.

prabharj avatar Mar 30 '17 11:03 prabharj

Can u provide a sample project so I can test it out?

shakilsiraj avatar Apr 01 '17 05:04 shakilsiraj

Hi ! Do you have news about this issue ?

It's easy to reproduce, just link (with npm or yarn) an AngularCLI project to a library who use json-object-mapper.

You will have an error like :

Unexpected value 'MyModule' imported by the module 'AppModule'. Please add a @NgModule annotation.

thanks !

Maxouhell avatar Jul 04 '17 06:07 Maxouhell

@Maxouhell Hello, I just started using json-object-mapper in an Angular 4 app and I faced the same issue. It took me a while, but I eventually found the solution in this thread: https://github.com/angular/angular/issues/15890

The issue is caused by the reflect-metadata dependency, and the solution is quite simple: just add import 'reflect-metadata'; in your polyfills.ts. And that's it :)

ghost avatar Jul 11 '17 16:07 ghost

Thanks @Orhleil. This issue baffled me a lot, could never produce it.

shakilsiraj avatar Jul 11 '17 21:07 shakilsiraj

Hi ! Sadly, I already have this import :/

I will check inside Angular issues if I found something

Thanks for your help !

Maxouhell avatar Jul 12 '17 06:07 Maxouhell

@prabharj @Maxouhell See pull request #14

devpreview avatar Jul 22 '17 09:07 devpreview

Can you please try 1.5.0?

shakilsiraj avatar Jul 30 '17 12:07 shakilsiraj