pult / SuperObject.Delphi

Pascal (Delphi, FPC) json parser library SuperObject
The Unlicense
45 stars 22 forks source link

Add backward compatibility when serializing enums to json #7

Closed vfisher closed 10 months ago

vfisher commented 10 months ago

No backward compatibility with old version of superobject, when serializing enums. Version in this repo can handle both name and integer value of enum. Old version can handle only integer value. Can you please add {$DEFINE ...}, that will change behavior of converting enum to json to integer value for backward compatibility with old versions. Applications, that was compiled in old versions, will parse this json successfully.

What i mean:

Type TEnum = (teOne = 0, teTwo = 1, teThree = 2, teFour = 3); TRecrd = Record e: TEnum; End;

...

Var s,: String; r: TRecrd; ACTX: TSuperRttiContext; begin ACTX:= TSuperRttiContext.Create; try s:= '{"e": "teThree"}'; r:= ACTX.AsType(SO(s)); // works only in current version. Old versions will fail with 'Marshalling error' s:= '{"e": 3}'; r:= ACTX.AsType(SO(s)); // works in both old and current version finally ACTX.Free; end; end;

pult commented 10 months ago

r := ACTX.AsType<TRecord>( SO('{"e": "teThree"}') ); Serialization or deserialization? Your sample - it is deserealizatioin. Specify which old version? It is not clear what is wrong with the fact that deserialization "enum-name" works correctly?

vfisher commented 10 months ago

It's not a bug. It's feature request. :) Old version can't deserialize enums, that stored in json like, for example, {"enum_value":"etOne"}, but works ok with {"enum_value":1}.

So, if I take this version on superobject, serialize some enum to json and give it to software, that was written with obsolette version of so, it will fails.

If there be a some DEFINE in your version of superobject, that can override behavior of serializing enums, like in old version (integers), than result json with enums will became compatible with old version.

Which old version? I have no idea, we use it many years. Changelog begins with "v1.2" and no other version info in sources.

pult commented 10 months ago

Show how you serialize (generate json-text). In the example, only deserialization.

pult commented 10 months ago
SuperRttiContextDefault.ForceEnumeration := False;
ACTX :=SuperRttiContextDefault;

or 
ACTX: TSuperRttiContext;
ACTX := SuperRttiContextClassDefault.Create; // or TSuperRttiContext.Crerate;
ACTX.ForceEnumeration := False;
vfisher commented 10 months ago

Exactly what is needed! Thanks!