简体   繁体   English

NodeJS中的Marshall JSON

[英]Marshall JSON in NodeJS

I have huge JSON as part of the REST request and want to marshall/unmarshall the JSON in NodeJS. 我将大量JSON作为REST请求的一部分,并想在NodeJS中封送/解组JSON。 I see that I can use JSON.parse to parse the JSON. 我看到我可以使用JSON.parse来解析JSON。

If I want to write the JSON, JSON.stringfy is available. 如果要编写JSON,可以使用JSON.stringfy。 But for huge JSON, this becomes very complicate. 但是对于巨大的JSON,这变得非常复杂。

I am looking for something like jackson-json in nodejs, where I can represent JSON schema/structure and store/manipulate the JSON in memory. 我正在寻找nodejs中的jackson-json之类的东西,在这里我可以表示JSON模式/结构并在内存中存储/操纵JSON。

After I searched for long time, I found json-typescript-mapper . 经过长时间的搜索,我发现了json-typescript-mapper Is this the only way, where I can marshall/unmarshall json into equivalent objects in memory ? 这是唯一可以将json编组/解编为内存中等效对象的唯一方法吗? Is this the best practice to for JSON marshalling/unmarshalling in nodejs ? 这是在nodejs中进行JSON编组/解组的最佳实践吗?

Regards, Albie Morken 问候,阿尔比·莫肯

The best practice for marshall/unmarshall JSON in Javascript is JSON.stringify / JSON.parse as you mentioned. 正如您所提到的,在Javascript中使用marshall / unmarshall JSON的最佳实践是JSON.stringify / JSON.parse Javascript is untyped language so there is sadly/luckily no need for a class definition the JSON is mapped onto like it's in Jackson. Javascript是非类型化语言,因此不幸/幸运的是,无需像杰克逊一样将JSON映射到的类定义。 You can still use Typescript or Flow for type checking. 您仍然可以使用TypescriptFlow进行类型检查。

If the JSON is huge and you're afraid that parsing will consume a lot of resources you can work with it like a node stream https://nodejs.org/api/stream.html . 如果JSON很大,并且您担心解析会消耗大量资源,则可以像节点流https://nodejs.org/api/stream.html一样使用它。 Node 10 will have support for async iteration over streams http://2ality.com/2018/04/async-iter-nodejs.html 节点10将支持流上的异步迭代http://2ality.com/2018/04/async-iter-nodejs.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM