How do I initialize a TypeScript object with a JSON object

前端 未结 16 702
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 08:30

I receive a JSON object from an AJAX call to a REST server. This object has property names that match my TypeScript class (this is a follow-on to this question).

Wha

相关标签:
16条回答
  • 2020-11-22 09:15
    **model.ts**
    export class Item {
        private key: JSON;
        constructor(jsonItem: any) {
            this.key = jsonItem;
        }
    }
    
    **service.ts**
    import { Item } from '../model/items';
    
    export class ItemService {
        items: Item;
        constructor() {
            this.items = new Item({
                'logo': 'Logo',
                'home': 'Home',
                'about': 'About',
                'contact': 'Contact',
            });
        }
        getItems(): Item {
            return this.items;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:16

    Maybe not actual, but simple solution:

    interface Bar{
    x:number;
    y?:string; 
    }
    
    var baz:Bar = JSON.parse(jsonString);
    alert(baz.y);
    

    work for difficult dependencies too!!!

    0 讨论(0)
  • 2020-11-22 09:17

    TLDR: TypedJSON (working proof of concept)


    The root of the complexity of this problem is that we need to deserialize JSON at runtime using type information that only exists at compile time. This requires that type-information is somehow made available at runtime.

    Fortunately, this can be solved in a very elegant and robust way with decorators and ReflectDecorators:

    1. Use property decorators on properties which are subject to serialization, to record metadata information and store that information somewhere, for example on the class prototype
    2. Feed this metadata information to a recursive initializer (deserializer)

     

    Recording Type-Information

    With a combination of ReflectDecorators and property decorators, type information can be easily recorded about a property. A rudimentary implementation of this approach would be:

    function JsonMember(target: any, propertyKey: string) {
        var metadataFieldKey = "__propertyTypes__";
    
        // Get the already recorded type-information from target, or create
        // empty object if this is the first property.
        var propertyTypes = target[metadataFieldKey] || (target[metadataFieldKey] = {});
    
        // Get the constructor reference of the current property.
        // This is provided by TypeScript, built-in (make sure to enable emit
        // decorator metadata).
        propertyTypes[propertyKey] = Reflect.getMetadata("design:type", target, propertyKey);
    }
    

    For any given property, the above snippet will add a reference of the constructor function of the property to the hidden __propertyTypes__ property on the class prototype. For example:

    class Language {
        @JsonMember // String
        name: string;
    
        @JsonMember// Number
        level: number;
    }
    
    class Person {
        @JsonMember // String
        name: string;
    
        @JsonMember// Language
        language: Language;
    }
    

    And that's it, we have the required type-information at runtime, which can now be processed.

     

    Processing Type-Information

    We first need to obtain an Object instance using JSON.parse -- after that, we can iterate over the entires in __propertyTypes__ (collected above) and instantiate the required properties accordingly. The type of the root object must be specified, so that the deserializer has a starting-point.

    Again, a dead simple implementation of this approach would be:

    function deserialize<T>(jsonObject: any, Constructor: { new (): T }): T {
        if (!Constructor || !Constructor.prototype.__propertyTypes__ || !jsonObject || typeof jsonObject !== "object") {
            // No root-type with usable type-information is available.
            return jsonObject;
        }
    
        // Create an instance of root-type.
        var instance: any = new Constructor();
    
        // For each property marked with @JsonMember, do...
        Object.keys(Constructor.prototype.__propertyTypes__).forEach(propertyKey => {
            var PropertyType = Constructor.prototype.__propertyTypes__[propertyKey];
    
            // Deserialize recursively, treat property type as root-type.
            instance[propertyKey] = deserialize(jsonObject[propertyKey], PropertyType);
        });
    
        return instance;
    }
    
    var json = '{ "name": "John Doe", "language": { "name": "en", "level": 5 } }';
    var person: Person = deserialize(JSON.parse(json), Person);
    

    The above idea has a big advantage of deserializing by expected types (for complex/object values), instead of what is present in the JSON. If a Person is expected, then it is a Person instance that is created. With some additional security measures in place for primitive types and arrays, this approach can be made secure, that resists any malicious JSON.

     

    Edge Cases

    However, if you are now happy that the solution is that simple, I have some bad news: there is a vast number of edge cases that need to be taken care of. Only some of which are:

    • Arrays and array elements (especially in nested arrays)
    • Polymorphism
    • Abstract classes and interfaces
    • ...

    If you don't want to fiddle around with all of these (I bet you don't), I'd be glad to recommend a working experimental version of a proof-of-concept utilizing this approach, TypedJSON -- which I created to tackle this exact problem, a problem I face myself daily.

    Due to how decorators are still being considered experimental, I wouldn't recommend using it for production use, but so far it served me well.

    0 讨论(0)
  • 2020-11-22 09:17

    you can do like below

    export interface Instance {
      id?:string;
      name?:string;
      type:string;
    }
    

    and

    var instance: Instance = <Instance>({
          id: null,
          name: '',
          type: ''
        });
    
    0 讨论(0)
提交回复
热议问题