quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

C++: use different integer type

Open oliver opened this issue 2 years ago • 2 comments

When using a JSON Schema to generate C++ code, the generated class always uses int64_t for integer properties. In my other code this frequently leads to compiler warnings when I want to assign an integer property value to a C++ int, because the compiler warns about casts from int64_t to int (because we build with -Wc++11-narrowing and -Wshorten-64-to-32).

Is there a way to specify the C++ data type that should be used for a property? Then I could explicitly change the generated type to int (or short or char) where necessary.

oliver avatar Feb 16 '23 09:02 oliver

You could always make a PR to add this as an option. I'm sure the contribution would be welcome and only take a couple hours max

dbrenot-pelmorex avatar Mar 06 '23 15:03 dbrenot-pelmorex

Should this be done in a "dumb way" - just replacing all int64_t with int, short, etc., regardless of the min/max constraints?

Or should we deduce a specific type int32_t, uint32_t, int16_t, etc., from the min/max constraints (and then use int32_t instead of int, int16_t instead of short, etc., where appropriate)? I prefer the latter idea, because it fosters correct interpretation of specified integral payload data.

goto40 avatar Nov 02 '24 08:11 goto40