I am looking for a library to help me parsed JSON data. I want to ensure they conform to certain type I have defined and extract those values.
There are two libraries that I found allow me to do this: tink_validation and dataclass, bot having their drawbacks.
tink_validation was a perfect fit, but it doesn’t work with types defined beforehand, only inlined. So this does not work:
typedef IncomingMessage = {
final event:String;
final context:String;
final payload:{
final coordinates:{
final column:Int;
final row:Int;
};
final isInMultiAction:Bool;
final settings:{
final id:String;
final remoteServer:String;
};
};
};
function parseMessage(message:String):IncomingMessage {
final parsed = hammerspoon.Json.decode(message);
final value:IncomingMessage = Validation.extract(parsed);
}
On the other Hand, dataclass requires you to define specific classes to deal with the validation, and I don’t want that, I just want simple data structures.
Is there anything like tink_validation that works well with types defined with typedef?
I think tink_json and json2object does that. json2object example from my code:
// better to create once
final jsonParser = new JsonParser<IncomingMessage>();
...
final msg = jsonParser.fromJson(dataString);
if (jsonParser.errors.length > 0) {...}
Yes, they do, and I starting looking at them. However, they come with their own JSON parsing library, which makes my output file to grow from ~800 to more than 2k
But, because I can’t use Haxe Serialization classes (they force a dep I can’t afford), and they make my output to grow that much anyway I’m moving to json2object for now.
I really wish jsonn2object forced me to check a Result output, rather than manually having to check for errors.
Is it possible to make them just validate already parsed objects?
/usr/local/lib/haxe/lib/tink_validation/0,2,0/src/tink/validation/macro/Macro.hx:44: characters 35-47 : Class<tink.validation.macro.GenExtractor> should be tink.typecrawler.GeneratorProvider
/usr/local/lib/haxe/lib/tink_validation/0,2,0/src/tink/validation/macro/Macro.hx:44: characters 35-47 : ... For function argument 'gen'
/usr/local/lib/haxe/lib/tink_validation/0,2,0/src/tink/validation/macro/Macro.hx:78: characters 35-47 : Class<tink.validation.macro.GenValidator> should be tink.typecrawler.GeneratorProvider
/usr/local/lib/haxe/lib/tink_validation/0,2,0/src/tink/validation/macro/Macro.hx:78: characters 35-47 : ... For function argument 'gen'
src/streamDeckButton/Messages.hx:63: characters 52-58 : Unknown<0> cannot be constructed