quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

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

Open d-uzlov opened this issue 3 years ago • 1 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"));
}

d-uzlov avatar Apr 13 '22 10:04 d-uzlov

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"));
    }
}

sarkstephan avatar Apr 19 '22 08:04 sarkstephan