So, our previous example would look like so. This will essentially perform deep copying of the object. So, if you simply want to deep copy the object to another object, all you will need to do is JSON.stringify the object and parse it using JSON.parse afterward. Well, a simple solution is to use JSON.stringify and JSON.parse. The only instance where a piece of JSON text represents a different value from the same JavaScript expression is when dealing with the 'proto' key see Object literal syntax vs. So, what would you do if you deep copy this object? i.e to also copy the objects inside the object? JSON.parse () parses a JSON string according to the JSON grammar, then evaluates the string as if it's a JavaScript expression. Which is some part of the object is still connected to the original variable since they are only pointers/references. I hope those functions to be really useful for network systems using JSON data, especially for enhancing type safety.Let user1 = Īs you can tell, when changing the school.name in the user2 object, it also gets reflected back to the user1 object. Therefore, use TSON.assertStringify() and TSON.isStringify() functions enhance your program's type safety with confidence. Typescript-json supports full spec of TypeScript type. Please understand me and such circumstance. Therefore, I had to compare with validator libraries. Someone may have question, "Why are you comparing with other validator libraries? Where's the other JSON string converter library?" Unfortunately, there's not any other library that can convert JSON string safely like typescript-json. Other libraries without special skills are extremely slow, and the gap can widen by thousands of times. Only TypeBox can be competitive and TypeBox utilizes JIT (Just-in-time) compilation. In the same reason, validation functions of typescript-json are much faster than other validation libraries. Such optimized runtime code generation by analzying source code is called AOT (Ahead of Time) compilation and it is the reason why typescript-json's JSON string conversion functions are much faster than the native JSON.stringify() function despite of type validation process. Typescript-json analyzes your code (type T) in the compilation level and generates optimized runtime code for JSON string conversion. Anyway, choose one of them depending on your purpose. Of course, if input value is matched with the type T, just JSON string would be returned. Note that, TSON.assertStringify() function throws TypeGuardError exception and TSON.isStringify() function returns boolean value when parametric input value is different with the target type T. Only you need is only one line with pure TypeScript type. You don't need to define any extra dedication like fast-json-stringify which requires JSON schema definition. After that, call those functions like below. If you want to utilize those type safe JSON functions, just install and import typescript-json. You also can run the benchmark program by running npm install and npm run benchmark commands after cloning typescript-json repository. Below codes are being used in the benchmark. Therefore, to ensure the objectivity, I diclose all code used in the benchmark. Of course, as this benchmark measured by myself, someone can doubt its objectivity. One thing amazing is, those JSON string conversion functions are much faster than the native JSON.stringify() function despite of type validation process. In today, I came back with powerful JSON string conversion functions.Īs you can see from the above code, the new JSON string conversion functions throw TypedGuardError or return null value when input value is different with target type T. Hello, I'm a developer of typescript-json, which can validate instance type by only one line, what I'd introduced as "1,000x faster runtime validator library" in here dev.to. Enter fullscreen mode Exit fullscreen mode
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |