glideapps / quicktype

Generate types and converters from JSON, Schema, and GraphQL

Home Page:https://app.quicktype.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature request] TypeScript: allow to use already parsed JSON

d-uzlov opened this issue · comments

Currently quicktype generates converter function in the following form:

public static toMyClass(json: string): MyClass {
  return cast(JSON.parse(json), r("MyClass"));
}

If I already have a JSON object from somewhere I have no way to cast it to MyClass.
I have to use toMyClass(JSON.stringify(alreadyParsedObject)), which is a bit ridiculous.

It would be nice to have a function like this:

public static castToMyClass(json: any): MyClass {
  return cast(json, r("MyClass"));
}

Try --raw-type any flag

generated code:

// Converts JSON types to/from your types
// and asserts the results at runtime
export class Convert {
    public static toMockEventSchema(json: any): MockEventSchema {
        return cast(json, r("MockEventSchema"));
    }
}