Finally, we have damage, which is another number. Uh, we're going to make this a float 32. So we can have decimal points as well. And that is the full schema now defined.
So what can we do without MarioKart schema? Um, we can serialize data. So if I take, uh, if, if I have some, you know, JavaScript object defined, uh, that fulfills this data, well, that fulfills this type, I can create a serializer from my MarioKart schema. Um, apologies. This is from a previous example. Oh, gosh. So here's our MarioKart serializer, we can now pass in as much data as we like, and it will serialize 10 times faster than, uh, by using this type aligned serializer that we've created. We can also of course, parse, excuse me, here's a string with a JSON string of MarioKart character data. We can compile a parser using our schema as well. And the best part of this is that it knows the type that we're expecting from this. Uh, when we use this parser to actually parse this string, the resulting type is going to be either MarioKart character or undefined. Undefined if it's, uh, it basically is going to be the case if the data is in any way invalid. And so if the data isn't invalid and we have a result, then we know that this is Mario Kart data. We have all of our typings set up. We can start, we get our type ahead. Um, we can be sure of the data that's been defined here and it just makes all of our code from that point on so much easier. And of course, if it's undefined, then we can refer to the error message via the property on the pass function.
So, uh, yeah, so I think that's it for the code example. This is, this is really cool and it's exciting. So I actually didn't create, I created a such type utility for Jason schema and, uh, equivalent type utility that Jason just presented by was created by some exceptionally bright JavaScript engineer. He contributes to the talk on his own. But that gives you like huge powers in TypeScript because this approach of parsing Jason directly to the application type rather than into some generic data structure is, or, and the opposite serializing a specific type rather than serializing a generic data structure, uh, is used in all languages, almost all languages, right? So haskell, Go, Rust, like a generic Jason data structure is usually used in scripted languages, like JavaScript and Python, but it fundamentally undermines performance and reliability and using parsers which are type aligned, uh, which are, uh, results in high security and high performance at the same time. So as Jason said, compile serialize. So compile serializer actually generates JavaScript codes under the hood. So if it's used repeatedly, it gives you a 10 times performance boost compared to Jason serialize and parser. Uh, if their data is valid, then it will parse in pretty much the same time as Jason parse, but it will validate it as it goes. But for example, if somebody sends array instead of object, it will fail on the first character.
Comments