简体   繁体   English

读取 Javascript 中的本地 BSON 文件

[英]Reading local BSON file in Javascript

I have an HTML 5 game.我有一个 HTML 5 游戏。 I'm using webpack/typescript for development.我正在使用 webpack/typescript 进行开发。

There is some data I have which I was including by using require like the following我有一些数据,我通过使用require包括了如下

    const dataJson = require('scripts/data/data.json');

I would like to do the equivalent, except with bson.我想做同样的事情,除了 bson。 I tried the naive approach of doing something like this我尝试了做这样的天真的方法

    const dataJson = require('scripts/data/data.bson');

but this of course fails since there is no loader (won't compile with currently no loaders are configured to process this file. ).但这当然会失败,因为没有加载器(不会编译, currently no loaders are configured to process this file. )。

I'd like to then include the file locally, load the file and then deserialize the bson.然后我想在本地包含文件,加载文件,然后反序列化 bson。 Or I'd like to embed the bson like when using require .或者我想在使用require时嵌入bson This is some tool generated data, so it will be in a data file.这是一些工具生成的数据,所以它会在一个数据文件中。

I haven't been able to figure it out.我一直无法弄清楚。 I've tried the following.我试过以下。 But this results in the result containing either the bits of File or what looks like the content type (if done as readAsDataURL ).但这会result包含Filebits或看起来像内容类型的内容(如果作为readAsDataURL完成)。

What I have tried我试过的

    const file = new File(['data'], 'assets/data.bson', { type: 'application/bson' });
    const reader = new FileReader();
    reader.onload=(theFile) => {
        if (theFile.target) {
            console.log(theFile.target.result);
        }

    } ;
    reader.readAsDataURL(file);
    //reader.readAsBinaryString(file);

What is the correct method to load a local binary file?加载本地二进制文件的正确方法是什么? Presumably, once I have the data, I can just call deserialize from the bson package.据推测,一旦我有了数据,我就可以从bson package 调用deserialize化。

Okay, I'm adding some corrections here.好的,我在这里添加一些更正。 My method to read the files is wrong.我读取文件的方法是错误的。 I know understand File will actually create a file.我知道了解File实际上会创建一个文件。 So when this is passed to FileReader it gets the value of the both bits passed in.因此,当它被传递给FileReader时,它会获取传入的两个bits的值。

I have since discovered I can get the local files 2 ways.从那以后我发现我可以通过两种方式获取本地文件。 I can use XMLHttpRequest as well as the raw-loader loader.我可以使用XMLHttpRequest以及raw-loader加载程序。

However once I do this.但是,一旦我这样做。 I cannot seem to convert the contents into JSON using bson .我似乎无法使用bson将内容转换为 JSON 。 Any variant of deserialize , serialize , parse , or stringify has some issue. deserializeserializeparsestringify的任何变体都有一些问题。

Does anyone happen to have the correct method to convert the BSON contents into either a Javascript Object?有没有人碰巧有正确的方法将 BSON 内容转换为 Javascript Object?

Note that the BSON is generated from python using pymongo .请注意,BSON 是使用pymongo从 python 生成的。 My code to generate the BSON is the following.我生成 BSON 的代码如下。

            with open(args.output_bson, 'wb') as fp:
                encoded = bson.encode(data_meta.to_dict())
                fp.write(encoded)

to_dict is a dictionary. to_dict是一个字典。 I output both the JSON (using json ) and BSON.我 output JSON (使用json )和 BSON。

I also tested the file with bsondump and it does indeed convert to JSON.我还使用bsondump测试了该文件,它确实转换为 JSON。 So it does appear the file I've loaded is valid.所以看起来我加载的文件是有效的。

you can use bson你可以使用bson

then call:然后调用:

BSON.deserialize(theFile.target.result);

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

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