import test from 'ava' ; import { JsonGetter } from '../src/decorators/JsonGetter' ; import { JsonSetter , JsonSetterNulls } from '../src/decorators/JsonSetter' ; import { ObjectMapper } from '../src/databind/ObjectMapper' ; import { JsonProperty } from '../src/decorators/JsonProperty' ; import { JsonClassType } from '../src/decorators/JsonClassType' ; import { JacksonError } from '../src/core/JacksonError' ; test ( '@JsonGetter and @JsonSetter' , t => { class User { @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Number ] } ) id : number ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ String ] } ) firstname : string ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ String ] } ) lastname : string ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Array , [ String ] ] } ) fullname : string [ ] ; constructor ( id , firstname , lastname ) { this . id = id ; this . firstname = firstname ; this . lastname = lastname ; } @ JsonGetter ( ) @ JsonClassType ( { type : ( ) => [ String ] } ) getFullname ( ) { return this . firstname + ' ' + this . lastname ; } @ JsonSetter ( ) setFullname ( fullname ) { this . fullname = fullname . split ( ' ' ) ; } } const user = new User ( 1 , 'John' , 'Alfa' ) ; const objectMapper = new ObjectMapper ( ) ; const jsonData = objectMapper . stringify < User > ( user ) ; t . deepEqual ( JSON . parse ( jsonData ) , JSON . parse ( '{"id":1,"firstname":"John","lastname":"Alfa","fullname":"John Alfa"}' ) ) ; const userParsed = objectMapper . parse < User > ( jsonData , { mainCreator : ( ) => [ User ] } ) ; t . assert ( userParsed instanceof User ) ; t . is ( userParsed . id , 1 ) ; t . is ( userParsed . firstname , 'John' ) ; t . is ( userParsed . lastname , 'Alfa' ) ; t . assert ( userParsed . fullname instanceof Array ) ; t . deepEqual ( userParsed . fullname , [ 'John' , 'Alfa' ] ) ; } ) ; test ( '@JsonGetter and @JsonSetter with value' , t => { class User { @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Number ] } ) id : number ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ String ] } ) firstname : string ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ String ] } ) lastname : string ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Array , [ String ] ] } ) fullname : string [ ] ; constructor ( id , firstname , lastname ) { this . id = id ; this . firstname = firstname ; this . lastname = lastname ; } @ JsonGetter ( { value : 'fullname' } ) @ JsonClassType ( { type : ( ) => [ String ] } ) getMyFullname ( ) { return this . firstname + ' ' + this . lastname ; } @ JsonSetter ( { value : 'fullname' } ) setMyFullname ( fullname ) { this . fullname = fullname . split ( ' ' ) ; } } const user = new User ( 1 , 'John' , 'Alfa' ) ; const objectMapper = new ObjectMapper ( ) ; const jsonData = objectMapper . stringify < User > ( user ) ; t . deepEqual ( JSON . parse ( jsonData ) , JSON . parse ( '{"id":1,"firstname":"John","lastname":"Alfa","fullname":"John Alfa"}' ) ) ; const userParsed = objectMapper . parse < User > ( jsonData , { mainCreator : ( ) => [ User ] } ) ; t . assert ( userParsed instanceof User ) ; t . is ( userParsed . id , 1 ) ; t . is ( userParsed . firstname , 'John' ) ; t . is ( userParsed . lastname , 'Alfa' ) ; t . assert ( userParsed . fullname instanceof Array ) ; t . deepEqual ( userParsed . fullname , [ 'John' , 'Alfa' ] ) ; } ) ; test ( '@JsonSetter with nulls and contentNulls options set to JsonSetterNulls.FAIL' , t => { class User { @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Number ] } ) id : number ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ String ] } ) firstname : string ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ String ] } ) lastname : string ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Array , [ String ] ] } ) otherInfoArray : string [ ] = [ ] ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Map , [ String , String ] ] } ) otherInfoMap : Map < string , string > = new Map ( ) ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Object , [ String , String ] ] } ) otherInfoObjLiteral = { } ; constructor ( id , firstname , lastname ) { this . id = id ; this . firstname = firstname ; this . lastname = lastname ; } @ JsonSetter ( { nulls : JsonSetterNulls . FAIL , contentNulls : JsonSetterNulls . FAIL } ) setOtherInfoArray ( @ JsonClassType ( { type : ( ) => [ Array ] } ) otherInfoArray ) { this . otherInfoArray = otherInfoArray ; } @ JsonSetter ( { nulls : JsonSetterNulls . FAIL , contentNulls : JsonSetterNulls . FAIL } ) setOtherInfoMap ( @ JsonClassType ( { type : ( ) => [ Map ] } ) otherInfoMap ) { this . otherInfoMap = otherInfoMap ; } @ JsonSetter ( { nulls : JsonSetterNulls . FAIL , contentNulls : JsonSetterNulls . FAIL } ) setOtherInfoObjLiteral ( otherInfoObjLiteral ) { this . otherInfoObjLiteral = otherInfoObjLiteral ; } } const objectMapper = new ObjectMapper ( ) ; let err = t . throws < JacksonError > ( ( ) => { objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":null,"otherInfoMap":{},"otherInfoObjLiteral":{}}' , { mainCreator : ( ) => [ User ] } ) ; } ) ; t . assert ( err instanceof JacksonError ) ; err = t . throws < JacksonError > ( ( ) => { objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":["info 1", null, "info 3"],"otherInfoMap":{},"otherInfoObjLiteral":{}}' , { mainCreator : ( ) => [ User ] } ) ; } ) ; t . assert ( err instanceof JacksonError ) ; err = t . throws < JacksonError > ( ( ) => { objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":[],"otherInfoMap":null,"otherInfoObjLiteral":{}}' , { mainCreator : ( ) => [ User ] } ) ; } ) ; t . assert ( err instanceof JacksonError ) ; err = t . throws < JacksonError > ( ( ) => { objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":[],"otherInfoMap":{"mapKey1": "mapValue1","mapKey2":null,"mapKey3":"mapValue3"},"otherInfoObjLiteral":{}}' , { mainCreator : ( ) => [ User ] } ) ; } ) ; t . assert ( err instanceof JacksonError ) ; err = t . throws < JacksonError > ( ( ) => { objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":[],"otherInfoMap":{},"otherInfoObjLiteral":null}' , { mainCreator : ( ) => [ User ] } ) ; } ) ; t . assert ( err instanceof JacksonError ) ; err = t . throws < JacksonError > ( ( ) => { objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":[],"otherInfoMap":{},"otherInfoObjLiteral":{"objLiteralKey1": "objLiteralValue1","objLiteralKey2":null,"objLiteralKey3":"objLiteralValue3"}}' , { mainCreator : ( ) => [ User ] } ) ; } ) ; t . assert ( err instanceof JacksonError ) ; } ) ; test ( '@JsonSetter with nulls and contentNulls options set to JsonSetterNulls.SKIP' , t => { class User { @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Number ] } ) id : number ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ String ] } ) firstname : string ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ String ] } ) lastname : string ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Array , [ String ] ] } ) otherInfoArray : string [ ] = [ ] ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Map , [ String , String ] ] } ) otherInfoMap : Map < string , string > = new Map ( ) ; @ JsonProperty ( ) @ JsonClassType ( { type : ( ) => [ Object , [ String , String ] ] } ) otherInfoObjLiteral = { } ; constructor ( id , firstname , lastname ) { this . id = id ; this . firstname = firstname ; this . lastname = lastname ; } @ JsonSetter ( { nulls : JsonSetterNulls . SKIP , contentNulls : JsonSetterNulls . SKIP } ) setOtherInfoArray ( @ JsonClassType ( { type : ( ) => [ Array ] } ) otherInfoArray ) { this . otherInfoArray = otherInfoArray ; } @ JsonSetter ( { nulls : JsonSetterNulls . SKIP , contentNulls : JsonSetterNulls . SKIP } ) setOtherInfoMap ( @ JsonClassType ( { type : ( ) => [ Map ] } ) otherInfoMap ) { this . otherInfoMap = otherInfoMap ; } @ JsonSetter ( { nulls : JsonSetterNulls . SKIP , contentNulls : JsonSetterNulls . SKIP } ) setOtherInfoObjLiteral ( otherInfoObjLiteral ) { this . otherInfoObjLiteral = otherInfoObjLiteral ; } } const objectMapper = new ObjectMapper ( ) ; let userParsed = objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":null,"otherInfoMap":{},"otherInfoObjLiteral":{}}' , { mainCreator : ( ) => [ User ] } ) ; t . assert ( userParsed instanceof User ) ; t . is ( userParsed . id , 1 ) ; t . is ( userParsed . firstname , 'John' ) ; t . is ( userParsed . lastname , 'Alfa' ) ; t . assert ( userParsed . otherInfoArray instanceof Array ) ; t . deepEqual ( userParsed . otherInfoArray , [ ] ) ; t . assert ( userParsed . otherInfoMap instanceof Map ) ; t . deepEqual ( userParsed . otherInfoMap , new Map ( ) ) ; userParsed = objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":["info 1", null, "info 3"],"otherInfoMap":{},"otherInfoObjLiteral":{}}' , { mainCreator : ( ) => [ User ] } ) ; t . assert ( userParsed instanceof User ) ; t . is ( userParsed . id , 1 ) ; t . is ( userParsed . firstname , 'John' ) ; t . is ( userParsed . lastname , 'Alfa' ) ; t . assert ( userParsed . otherInfoArray instanceof Array ) ; t . deepEqual ( userParsed . otherInfoArray , [ 'info 1' , 'info 3' ] ) ; t . assert ( userParsed . otherInfoMap instanceof Map ) ; t . deepEqual ( userParsed . otherInfoMap , new Map ( ) ) ; t . assert ( userParsed . otherInfoObjLiteral instanceof Object ) ; t . deepEqual ( userParsed . otherInfoObjLiteral , { } ) ; userParsed = objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":[],"otherInfoMap":null,"otherInfoObjLiteral":{}}' , { mainCreator : ( ) => [ User ] } ) ; t . assert ( userParsed instanceof User ) ; t . is ( userParsed . id , 1 ) ; t . is ( userParsed . firstname , 'John' ) ; t . is ( userParsed . lastname , 'Alfa' ) ; t . assert ( userParsed . otherInfoArray instanceof Array ) ; t . deepEqual ( userParsed . otherInfoArray , [ ] ) ; t . assert ( userParsed . otherInfoMap instanceof Map ) ; t . deepEqual ( userParsed . otherInfoMap , new Map ( ) ) ; t . assert ( userParsed . otherInfoObjLiteral instanceof Object ) ; t . deepEqual ( userParsed . otherInfoObjLiteral , { } ) ; userParsed = objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":[],"otherInfoMap":{"mapKey1": "mapValue1","mapKey2":null,"mapKey3":"mapValue3"},"otherInfoObjLiteral":{}}' , { mainCreator : ( ) => [ User ] } ) ; t . assert ( userParsed instanceof User ) ; t . is ( userParsed . id , 1 ) ; t . is ( userParsed . firstname , 'John' ) ; t . is ( userParsed . lastname , 'Alfa' ) ; t . assert ( userParsed . otherInfoArray instanceof Array ) ; t . deepEqual ( userParsed . otherInfoArray , [ ] ) ; t . assert ( userParsed . otherInfoMap instanceof Map ) ; t . deepEqual ( userParsed . otherInfoMap , new Map ( [ [ 'mapKey1' , 'mapValue1' ] , [ 'mapKey3' , 'mapValue3' ] ] ) ) ; t . assert ( userParsed . otherInfoObjLiteral instanceof Object ) ; t . deepEqual ( userParsed . otherInfoObjLiteral , { } ) ; userParsed = objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":[],"otherInfoMap":{},"otherInfoObjLiteral":null}' , { mainCreator : ( ) => [ User ] } ) ; t . assert ( userParsed instanceof User ) ; t . is ( userParsed . id , 1 ) ; t . is ( userParsed . firstname , 'John' ) ; t . is ( userParsed . lastname , 'Alfa' ) ; t . assert ( userParsed . otherInfoArray instanceof Array ) ; t . deepEqual ( userParsed . otherInfoArray , [ ] ) ; t . assert ( userParsed . otherInfoMap instanceof Map ) ; t . deepEqual ( userParsed . otherInfoMap , new Map ( ) ) ; t . assert ( userParsed . otherInfoObjLiteral instanceof Object ) ; t . deepEqual ( userParsed . otherInfoObjLiteral , { } ) ; userParsed = objectMapper . parse < User > ( '{"id":1,"firstname":"John","lastname":"Alfa","otherInfoArray":[],"otherInfoMap":{},"otherInfoObjLiteral":{"objLiteralKey1": "objLiteralValue1","objLiteralKey2":null,"objLiteralKey3":"objLiteralValue3"}}' , { mainCreator : ( ) => [ User ] } ) ; t . assert ( userParsed instanceof User ) ; t . is ( userParsed . id , 1 ) ; t . is ( userParsed . firstname , 'John' ) ; t . is ( userParsed . lastname , 'Alfa' ) ; t . assert ( userParsed . otherInfoArray instanceof Array ) ; t . deepEqual ( userParsed . otherInfoArray , [ ] ) ; t . assert ( userParsed . otherInfoMap instanceof Map ) ; t . deepEqual ( userParsed . otherInfoMap , new Map ( ) ) ; t . assert ( userParsed . otherInfoObjLiteral instanceof Object ) ; t . deepEqual ( userParsed . otherInfoObjLiteral , { objLiteralKey1 : 'objLiteralValue1' , objLiteralKey3 : 'objLiteralValue3' } ) ; } ) ;