all files / json2typescript/ json-convert.ts

64.82% Statements 164/253
61.75% Branches 113/183
95.45% Functions 21/22
65.04% Lines 147/226
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772                                                               66×                                                                                                                                                                                                                                                                 10×             10×     10×           10×     35× 35×     10×           10×                                                                                                                                                         10×             10×     10×           10×     35× 35×     10×           10×                                                                                                                       38× 38×           38× 38× 38× 38×   38×       38×                           38× 38×                                                 38× 38×         38× 38× 38× 38×   38×       38×                           38× 38×                                                         79×     79×     79× 79× 40×       258× 39× 38×                                       78× 12×       66×     58×     12×       10×   46×                     46×     46×           46×         46×                                                                                                                                                                                                 11×   11×                                                                          
import { OperationMode, ValueCheckingMode } from "./json-convert-enums";
import { MappingOptions, Settings } from "./json-convert-options";
import { Any } from "./any";
 
/**
 * Offers a simple API for mapping JSON objects to TypeScript/JavaScript classes and vice versa.
 *
 * @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
 * @see https://www.npmjs.com/package/json2typescript full documentation on NPM
 */
export class JsonConvert {
 
    ////////////////
    // PROPERTIES //
    ////////////////
 
 
    /**
     * Determines how the JsonConvert class instance should operate.
     *
     * You may assign three different values:
     * - OperationMode.DISABLE: json2typescript will be disabled, no type checking or mapping is done
     * - OperationMode.ENABLE: json2typescript is enabled, but only errors are logged
     * - OperationMode.LOGGING: json2typescript is enabled and detailed information is logged
     */
    private _operationMode: number = OperationMode.ENABLE;
 
    /**
     * Determines how the JsonConvert class instance should operate.
     *
     * You may assign three different values:
     * - OperationMode.DISABLE: json2typescript will be disabled, no type checking or mapping is done
     * - OperationMode.ENABLE: json2typescript is enabled, but only errors are logged
     * - OperationMode.LOGGING: json2typescript is enabled and detailed information is logged
     * @returns {number}
     */
    get operationMode(): number {
        return this._operationMode;
    }
 
    /**
     * Determines how the JsonConvert class instance should operate.
     *
     * You may assign three different values:
     * - OperationMode.DISABLE: json2typescript will be disabled, no type checking or mapping is done
     * - OperationMode.ENABLE: json2typescript is enabled, but only errors are logged
     * - OperationMode.LOGGING: json2typescript is enabled and detailed information is logged
     * @param value
     */
    set operationMode(value: number) {
        Eif (value in OperationMode) this._operationMode = value;
    }
 
    /**
     * Determines which types are allowed to be null.
     *
     * You may assign three different values:
     * - ValueCheckingMode.ALLOW_NULL: all given values in the JSON are allowed to be null
     * - ValueCheckingMode.ALLOW_OBJECT_NULL: objects in the JSON are allowed to be null, primitive types are not allowed to be null
     * - ValueCheckingMode.DISALLOW_NULL: no null values are tolerated in the JSON
     */
    private _valueCheckingMode: number = ValueCheckingMode.ALLOW_OBJECT_NULL;
 
    /**
     * Determines which types are allowed to be null.
     *
     * You may assign three different values:
     * - ValueCheckingMode.ALLOW_NULL: all given values in the JSON are allowed to be null
     * - ValueCheckingMode.ALLOW_OBJECT_NULL: objects in the JSON are allowed to be null, primitive types are not allowed to be null
     * - ValueCheckingMode.DISALLOW_NULL: no null values are tolerated in the JSON
     *
     * @returns {number}
     */
    get valueCheckingMode(): number {
        return this._valueCheckingMode;
    }
 
    /**
     * Determines which types are allowed to be null.
     *
     * You may assign three different values:
     * - ValueCheckingMode.ALLOW_NULL: all given values in the JSON are allowed to be null
     * - ValueCheckingMode.ALLOW_OBJECT_NULL: objects in the JSON are allowed to be null, primitive types are not allowed to be null
     * - ValueCheckingMode.DISALLOW_NULL: no null values are tolerated in the JSON
     *
     * @param value
     */
    set valueCheckingMode(value: number) {
        Eif (value in ValueCheckingMode) this._valueCheckingMode = value;
    }
 
    /**
     * Determines whether primitive types should be checked.
     * If true, it will be allowed to assign primitive to other primitive types.
     */
    private _ignorePrimitiveChecks: boolean = false;
 
    /**
     * Determines whether primitive types should be checked.
     * If true, it will be allowed to assign primitive to other primitive types.
     *
     * @returns {boolean}
     */
    get ignorePrimitiveChecks(): boolean {
        return this._ignorePrimitiveChecks;
    }
 
    /**
     * Determines whether primitive types should be checked.
     * If true, it will be allowed to assign primitive to other primitive types.
     *
     * @param value
     */
    set ignorePrimitiveChecks(value: boolean) {
        this._ignorePrimitiveChecks = value;
    }
 
 
    /////////////////
    // CONSTRUCTOR //
    /////////////////
 
 
    /**
     * Constructor.
     *
     * To learn more about the params, check the documentation of the equally named class properties.
     *
     * @param operationMode optional param (default: OperationMode.ENABLE)
     * @param valueCheckingMode optional param (default: ValueCheckingMode.ALLOW_OBJECT_NULL)
     * @param ignorePrimitiveChecks optional param (default: false)
     */
    constructor(operationMode?: number, valueCheckingMode?: number, ignorePrimitiveChecks?: boolean) {
        Iif (operationMode in OperationMode) this.operationMode = operationMode;
        Iif (valueCheckingMode in ValueCheckingMode) this.valueCheckingMode = valueCheckingMode;
        Iif (ignorePrimitiveChecks) this.ignorePrimitiveChecks = ignorePrimitiveChecks;
    }
 
 
    ////////////////////
    // PUBLIC METHODS //
    ////////////////////
 
 
    /**
     * Tries to serialize a TypeScript object or array of objects to JSON.
     *
     * @param data object or array of objects
     *
     * @returns {any} the JSON object
     *
     * @throws an exception in case of failure
     *
     * @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
     * @see https://www.npmjs.com/package/json2typescript full documentation
     */
    serialize(data: any): any {
 
        if (data.constructor === Array) return this.serializeArray(data);
        Eif (typeof data === "object") return this.serializeObject(data); // must be last due to the fact that an array is an object in TypeScript!
 
        throw new Error(
            "Fatal error in JsonConvert. " +
            "Passed parameter json in JsonConvert.serialize() is not in valid format (object or array)."
        );
 
    }
 
    /**
     * Tries to serialize a TypeScript object to a JSON object.
     *
     * @param instance TypeScript instance
     *
     * @returns {any} the JSON object
     *
     * @throws an exception in case of failure
     *
     * @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
     * @see https://www.npmjs.com/package/json2typescript full documentation
     */
    serializeObject(instance: any): any {
 
        Iif (typeof(instance) !== "object" || instance instanceof Array) {
            throw new Error(
                "Fatal error in JsonConvert. " +
                "Passed parameter jsonArray in JsonConvert.serializeObject() is not of type object."
            );
        }
 
        Iif (this.operationMode === OperationMode.DISABLE) {
            return instance;
        }
        Iif (this.operationMode === OperationMode.LOGGING) {
            console.log("----------");
            console.log("Receiving JavaScript instance:");
            console.log(instance);
        }
 
        let jsonObject: any = {};
 
        // Loop through all initialized class properties
        for (const propertyKey of Object.keys(instance)) {
            this.serializeObject_loopProperty(instance, propertyKey, jsonObject);
        }
 
        Iif (this.operationMode === OperationMode.LOGGING) {
            console.log("Returning JSON object:");
            console.log(jsonObject);
            console.log("----------");
        }
 
        return jsonObject;
    }
 
    /**
     * Tries to serialize a TypeScript array to a JSON array.
     *
     * @param instanceArray array of TypeScript instances
     *
     * @returns {any[]} the JSON array
     *
     * @throws an exception in case of failure
     *
     * @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
     * @see https://www.npmjs.com/package/json2typescript full documentation
     */
    serializeArray(instanceArray: any[]): any[] {
 
        Iif (typeof(instanceArray) !== "object" || instanceArray instanceof Array === false) {
            throw new Error(
                "Fatal error in JsonConvert. " +
                "Passed parameter jsonArray in JsonConvert.serializeArray() is not of type array."
            );
        }
 
        Iif (this.operationMode === OperationMode.DISABLE) {
            return instanceArray;
        }
        Iif (this.operationMode === OperationMode.LOGGING) {
            console.log("----------");
            console.log("Receiving JavaScript array:");
            console.log(instanceArray);
        }
 
        let jsonArray: any[] = [];
 
        // Loop through all array elements
        for (const classInstance of instanceArray) {
            jsonArray.push(this.serializeObject(classInstance));
        }
 
        Iif (this.operationMode === OperationMode.LOGGING) {
            console.log("Returning JSON array:");
            console.log(jsonArray);
            console.log("----------");
        }
 
        return jsonArray;
 
    }
 
    /**
     * Tries to deserialize given JSON to a TypeScript object or array of objects.
     *
     * @param json the JSON as object or array
     * @param classReference the class reference
     *
     * @returns {any} the deserialized data (TypeScript instance or array of TypeScript instances)
     *
     * @throws an exception in case of failure
     *
     * @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
     * @see https://www.npmjs.com/package/json2typescript full documentation
     */
    deserialize(json: any, classReference: { new(): any }): any {
 
        if (json.constructor === Array) return this.deserializeArray(json, classReference);
        Eif (typeof json === "object") return this.deserializeObject(json, classReference); // must be last due to the fact that an array is an object in TypeScript!
 
        throw new Error(
            "Fatal error in JsonConvert. " +
            "Passed parameter json in JsonConvert.deserialize() is not in valid JSON format (object or array)."
        );
 
    };
 
    /**
     * Tries to deserialize a JSON object to a TypeScript object.
     *
     * @param jsonObject the JSON object
     * @param classReference the class reference
     *
     * @returns {any} the deserialized TypeScript instance
     *
     * @throws an exception in case of failure
     *
     * @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
     * @see https://www.npmjs.com/package/json2typescript full documentation
     */
    deserializeObject(jsonObject: any, classReference: { new(): any }): any {
 
        Iif (typeof(jsonObject) !== "object" || jsonObject instanceof Array) {
            throw new Error(
                "Fatal error in JsonConvert. " +
                "Passed parameter jsonObject in JsonConvert.deserializeObject() is not of type object."
            );
        }
 
        Iif (this.operationMode === OperationMode.DISABLE) {
            return jsonObject;
        }
        Iif (this.operationMode === OperationMode.LOGGING) {
            console.log("----------");
            console.log("Receiving JSON object:");
            console.log(jsonObject);
        }
 
        let instance = new classReference();
 
        // Loop through all initialized class properties
        for (const propertyKey of Object.keys(instance)) {
            this.deserializeObject_loopProperty(instance, propertyKey, jsonObject);
        }
 
        Iif (this.operationMode === OperationMode.LOGGING) {
            console.log("Returning CLASS instance:");
            console.log(instance);
            console.log("----------");
        }
 
        return instance;
 
    }
 
    /**
     * Tries to deserialize a JSON array to a TypeScript array.
     *
     * @param jsonArray the JSON array
     * @param classReference the object class
     *
     * @returns {any[]} the deserialized array of TypeScript instances
     *
     * @throws an exception in case of failure
     *
     * @author Andreas Aeschlimann, DHlab, University of Basel, Switzerland
     * @see https://www.npmjs.com/package/json2typescript full documentation
     */
    deserializeArray(jsonArray: any[], classReference: { new(): any }): any[] {
 
        Iif (typeof(jsonArray) !== "object" || jsonArray instanceof Array === false) {
            throw new Error(
                "Fatal error in JsonConvert. " +
                "Passed parameter jsonArray in JsonConvert.deserializeArray() is not of type array."
            );
        }
 
        Iif (this.operationMode === OperationMode.DISABLE) {
            return jsonArray;
        }
        Iif (this.operationMode === OperationMode.LOGGING) {
            console.log("----------");
            console.log("Receiving JSON array:");
            console.log(jsonArray);
        }
 
        let array: any[] = [];
 
        // Loop through all array elements
        for (const jsonObject of jsonArray) {
            array.push(this.deserializeObject(jsonObject, classReference));
        }
 
        Iif (this.operationMode === OperationMode.LOGGING) {
            console.log("Returning array of CLASS instances:");
            console.log(array);
            console.log("----------");
        }
 
        return array;
 
    }
 
 
    /////////////////////
    // PRIVATE METHODS //
    /////////////////////
 
 
    /**
     * Tries to find the JSON mapping for a given class property and finally assign the value.
     *
     * @param instance the instance of the class
     * @param classPropertyName the property name
     * @param json the JSON object
     *
     * @throws throws an expection in case of failure
     */
    private serializeObject_loopProperty(instance: any, classPropertyName: string, json: any): void {
 
        // Check if a JSON-object mapping is possible for a property
        const mappingOptions: MappingOptions = this.getClassPropertyMappingOptions(instance, classPropertyName);
        Iif (mappingOptions === null) {
            return;
        }
 
 
        // Get expected and real values
        let jsonKey: string = mappingOptions.jsonPropertyName;
        let expectedJsonType: any = mappingOptions.expectedJsonType;
        let isOptional: boolean = mappingOptions.isOptional;
        let customConverter: any = mappingOptions.customConverter;
 
        let classInstancePropertyValue: any = instance[classPropertyName];
 
 
        // Check if the json value exists
        Iif (typeof(classInstancePropertyValue) === "undefined") {
 
            if (isOptional) return;
 
            throw new Error(
                "Fatal error in JsonConvert. " +
                "Failed to map the JavaScript instance of class \"" + instance.constructor["name"] + "\" to JSON because the defined class property \"" + classPropertyName + "\" does not exist or is not defined:\n\n" +
                "\tClass property: \n\t\t" + classPropertyName + "\n\n" +
                "\tJSON property: \n\t\t" + jsonKey + "\n\n"
            );
        }
 
 
        // Map the property
        try {
            json[jsonKey] = customConverter !== null ? customConverter.serialize(classInstancePropertyValue) : this.verifyProperty(expectedJsonType, classInstancePropertyValue, true);
        } catch (e) {
            throw new Error(
                "Fatal error in JsonConvert. " +
                "Failed to map the JavaScript instance of class \"" + instance.constructor["name"] + "\" to JSON because of a type error.\n\n" +
                "\tClass property: \n\t\t" + classPropertyName + "\n\n" +
                "\tClass property value: \n\t\t" + classInstancePropertyValue + "\n\n" +
                "\tExpected type: \n\t\t" + this.getExpectedType(expectedJsonType) + "\n\n" +
                "\tRuntime type: \n\t\t" + this.getTrueType(classInstancePropertyValue) + "\n\n" +
                "\tJSON property: \n\t\t" + jsonKey + "\n\n" +
                e.message + "\n"
            );
        }
    }
 
    /**
     * Tries to find the JSON mapping for a given class property and finally assign the value.
     *
     * @param instance the instance of the class
     * @param classPropertyName the property name
     * @param json the JSON object
     *
     * @throws throws an expection in case of failure
     */
    private deserializeObject_loopProperty(instance: any, classPropertyName: string, json: any): void {
 
        const mappingOptions: MappingOptions = this.getClassPropertyMappingOptions(instance, classPropertyName);
        Iif (mappingOptions === null) {
            return;
        }
 
        // Get expected and real values
        let jsonKey: string = mappingOptions.jsonPropertyName;
        let expectedJsonType: any = mappingOptions.expectedJsonType;
        let isOptional: boolean = mappingOptions.isOptional;
        let customConverter: any = mappingOptions.customConverter;
 
        let jsonValue: any = json[jsonKey];
 
 
        // Check if the json value exists
        Iif (typeof(jsonValue) === "undefined") {
 
            if (isOptional) return;
 
            throw new Error(
                "Fatal error in JsonConvert. " +
                "Failed to map the JSON object to the class \"" + instance.constructor["name"] + "\" because the defined JSON property \"" + jsonKey + "\" does not exist:\n\n" +
                "\tClass property: \n\t\t" + classPropertyName + "\n\n" +
                "\tJSON property: \n\t\t" + jsonKey + "\n\n"
            );
        }
 
 
        // Map the property
        try {
            instance[classPropertyName] = customConverter !== null ? customConverter.deserialize(jsonValue) : this.verifyProperty(expectedJsonType, jsonValue);
        } catch (e) {
            throw new Error(
                "Fatal error in JsonConvert. " +
                "Failed to map the JSON object to the JavaScript class \"" + instance.constructor["name"]+ "\" because of a type error.\n\n" +
                "\tClass property: \n\t\t" + classPropertyName + "\n\n" +
                "\tExpected type: \n\t\t" + this.getExpectedType(expectedJsonType) + "\n\n" +
                "\tJSON property: \n\t\t" + jsonKey + "\n\n" +
                "\tJSON type: \n\t\t" + this.getJsonType(jsonValue) + "\n\n" +
                "\tJSON value: \n\t\t" + JSON.stringify(jsonValue) + "\n\n" +
                e.message + "\n\n"
            );
        }
    }
 
 
    ////////////////////
    // HELPER METHODS //
    ////////////////////
 
    /**
     * Gets the mapping options of a given class property.
     *
     * @param instance any class instance
     * @param {string} propertyName any property name
     *
     * @returns {MappingOptions}
     */
    private getClassPropertyMappingOptions(instance: any, propertyName: string): MappingOptions {
 
        let mappings: any = instance[Settings.MAPPING_PROPERTY];
 
        // Check if mapping is defined
        Iif (typeof(mappings) === "undefined") return null;
 
        // Get direct mapping if possible
        const directMappingName: string = instance.constructor.name + "." + propertyName;
        if (typeof(mappings[directMappingName]) !== "undefined") {
            return mappings[directMappingName];
        }
 
        // No mapping was found, try to find some
        const indirectMappingNames: string[] = Object.keys(mappings).filter(key => key.indexOf("." + propertyName) >= 0);
        if (indirectMappingNames.length > 0) {
            return mappings[indirectMappingNames[0]];
        }
 
        return null;
 
    }
 
    /**
     * Compares the type of a given value with an internal expected json type.
     * Either returns the resulting value or throws an exception.
     *
     * @param expectedJsonType the expected json type for the property
     * @param value the property value to verify
     * @param serialize optional param (default: false), if given, we are in serialization mode
     *
     * @returns returns the resulted mapped property
     *
     * @throws throws an expection in case of failure
     */
    private verifyProperty(expectedJsonType: any, value: any, serialize?: boolean): any {
 
        // Map immediately if we don't care about the type
        if (expectedJsonType === Any || expectedJsonType === null || expectedJsonType === Object) {
            return value;
        }
 
        // Check if attempt and expected was 1-d
        if (expectedJsonType instanceof Array === false && value instanceof Array === false) {
 
            // Check the type
            if (typeof(expectedJsonType) !== "undefined" && expectedJsonType.hasOwnProperty(Settings.MAPPING_PROPERTY)) { // only decorated custom objects have this injected property
 
                // Check if we have null value
                if (value === null) {
                    Eif (this.valueCheckingMode !== ValueCheckingMode.DISALLOW_NULL)
                        return null;
                    else throw new Error("\tReason: Given value is null.");
                }
 
                if (serialize) return this.serializeObject(value);
                else return this.deserializeObject(value, expectedJsonType);
 
            } else Iif (expectedJsonType === Any || expectedJsonType === null || expectedJsonType === Object) { // general object
 
                // Check if we have null value
                if (value === null) {
                    if (this.valueCheckingMode !== ValueCheckingMode.DISALLOW_NULL)
                        return null;
                    else throw new Error("\tReason: Given value is null.");
                }
 
                return value;
 
            } else Eif (expectedJsonType === String || expectedJsonType === Number || expectedJsonType === Boolean) { // otherwise check for a primitive type
 
                // Check if we have null value
                Iif (value === null) {
                    if (this.valueCheckingMode === ValueCheckingMode.ALLOW_NULL) return null;
                    else throw new Error("\tReason: Given value is null.");
                }
 
                // Check if the types match
                Eif ( // primitive types match
                (expectedJsonType === String && typeof(value) === "string") ||
                (expectedJsonType === Number && typeof(value) === "number") ||
                (expectedJsonType === Boolean && typeof(value) === "boolean")
                ) {
                    return value;
                } else { // primitive types mismatch
                    if (this.ignorePrimitiveChecks) return value;
                    throw new Error("\tReason: Given object does not match the expected primitive type.");
                }
 
            } else { // other weird types
 
                throw new Error("\tReason: Expected type is unknown. There might be multiple reasons for this:\n\t- You are missing the decorator @JsonObject (for object mapping)\n\t- You are missing the decorator @JsonConverter (for custom mapping) before your class definition\n\t- Your given class is undefined in the decorator because of circular dependencies");
 
            }
 
        }
 
        // Check if attempt and expected was n-d
        Eif (expectedJsonType instanceof Array && value instanceof Array) {
 
            let array: any[] = [];
 
            // No data given, so return empty value
            if (value.length === 0) {
                return array;
            }
 
            // We obviously don't care about the type, so return the value as is
            Iif (expectedJsonType.length === 0) {
                return value;
            }
 
            // Loop through the data. Both type and value are at least of length 1
            let autofillType: boolean = expectedJsonType.length < value.length;
            for (let i = 0; i < value.length; i++) {
 
                if (autofillType && i >= expectedJsonType.length) expectedJsonType[i] = expectedJsonType[i - 1];
 
                array[i] = this.verifyProperty(expectedJsonType[i], value[i], serialize);
 
            }
 
            return array;
 
        }
 
        // Check if attempt was 1-d and expected was n-d
        if (expectedJsonType instanceof Array && value instanceof Object) {
 
            let array: any[] = [];
 
            // No data given, so return empty value
            if (value.length === 0) {
                return array;
            }
 
            // We obviously don't care about the type, so return the json value as is
            if (expectedJsonType.length === 0) {
                return value;
            }
 
            // Loop through the data. Both type and value are at least of length 1
            let autofillType: boolean = expectedJsonType.length < Object.keys(value).length;
            let i = 0;
            for (let key in value) {
 
                if (autofillType && i >= expectedJsonType.length) expectedJsonType[i] = expectedJsonType[i - 1];
 
                array[key as any] = this.verifyProperty(expectedJsonType[i], value[key]);
 
                i++;
            }
 
            return array;
 
        }
 
        // Check if attempt was 1-d and expected was n-d
        if (expectedJsonType instanceof Array) {
            if (value === null) {
                if (this.valueCheckingMode !== ValueCheckingMode.DISALLOW_NULL) return null;
                else throw new Error("\tReason: Given value is null.");
            }
            throw new Error("\tReason: Expected type is array, but given value is non-array.");
        }
 
        // Check if attempt was n-d and expected as 1-d
        if (value instanceof Array) {
            throw new Error("\tReason: Given value is array, but expected a non-array type.");
        }
 
        // All other attempts are fatal
        throw new Error("\tReason: Mapping failed because of an unknown error.");
 
    }
 
 
    ///////////////////////////
    // JSON2TYPESCRIPT TYPES //
    ///////////////////////////
    
 
    /**
     * Returns a string representation of the expected json type.
     *
     * @param expectedJsonType the expected type given from the decorator
     *
     * @returns {string} the string representation
     */
    private getExpectedType(expectedJsonType: any): string {
 
        let type: string = "";
 
        if (expectedJsonType instanceof Array) {
            type = "[";
            for (let i = 0; i < expectedJsonType.length; i++) {
                if (i > 0) type += ",";
                type += this.getExpectedType(expectedJsonType[i]);
            }
            type += "]";
            return type;
        } else {
            if (expectedJsonType === Any || expectedJsonType === null || expectedJsonType === Object) {
                return "any";
            } else if (expectedJsonType === String || expectedJsonType == Boolean || expectedJsonType == Number) {
                return (new expectedJsonType()).constructor.name.toLowerCase();
            } else Eif (typeof expectedJsonType === 'function') {
                return (new expectedJsonType()).constructor.name;
            } else if (expectedJsonType === undefined) {
                return "undefined"
            } else {
                return "?????";
            }
        }
 
    }
 
    /**
     * Returns a string representation of the JSON value type.
     *
     * @param jsonValue the JSON value
     *
     * @returns {string} the string representation
     */
    private getJsonType(jsonValue: any): string {
 
        if (jsonValue === null) return "null";
 
        let type: string = "";
 
        if (jsonValue instanceof Array) {
            type = "[";
            for (let i = 0; i < jsonValue.length; i++) {
                if (i > 0) type += ",";
                type += this.getJsonType(jsonValue[i]);
            }
            type += "]";
            return type;
        } else {
            return typeof(jsonValue);
        }
 
    }
 
    /**
     * Returns a string representation of the true TypeScript type.
     *
     * @param trueValue the true value
     *
     * @returns {string} the string representation
     */
    private getTrueType(trueValue: any): string {
        return typeof(trueValue);
    }
 
}