How to use the bson.EJSON.parse function in bson

To help you get started, we’ve selected a few bson examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DistributedObjectProtocol / dop / test / index.js View on Github external
const { EJSON } = require('bson')
const text = '{ "int32": { "$numberInt": "10" } }'

// prints { int32: { [String: '10'] _bsontype: 'Int32', value: '10' } }
console.log(EJSON.parse(text, { relaxed: false }))

// prints { int32: 10 }
console.log(EJSON.parse(text))
github mongodb / stitch-js-sdk / packages / core / sdk / __tests__ / internal / net / StitchRequestClientUnitTests.ts View on Github external
.then(response => {
        expect(response.statusCode).toBe(200);
        const expected = {
          a: 42,
          hello: "world"
        };
        expect(expected).toEqual(EJSON.parse(response.body!, { relaxed: true }));

        // Error responses should be handled
        when(transportMock.roundTrip(anything())).thenResolve({
          headers: {},
          statusCode: 500
        });

        return stitchRequestClient.doRequest(builder.build());
      })
      .catch((error: StitchServiceError) => {
github mongodb / stitch-js-sdk / packages / core / sdk / __tests__ / internal / net / StitchRequestClientUnitTests.ts View on Github external
.then(response => {
        expect(response.statusCode).toEqual(200);
        const expected = {
          a: 42,
          hello: "world"
        };
        expect(EJSON.parse(response.body!, { relaxed: true })).toEqual(expected);

        // Error responses should be handled
        when(transportMock.roundTrip(anything())).thenResolve({
          headers: {},
          statusCode: 500
        });
        return stitchRequestClient.doRequest(builder.build());
      })
      .catch((error: StitchServiceError) => {
github mongodb / stitch-js-sdk / packages / core / sdk / src / internal / net / StitchEvent.ts View on Github external
/* 
           * parse the error as json
           * if it is not valid json, parse the body as seen in
           * StitchError#handleRequestError
           */
          const errorDoc = EJSON.parse(decodedData, { strict: false });
          errorMsg = errorDoc[ErrorFields.Error];
          errorCode = stitchServiceErrorCodeFromApi(errorDoc[ErrorFields.ErrorCode]);
        } catch (error) {
          errorMsg = decodedData;
          errorCode = StitchServiceErrorCode.Unknown;
        }
        this.error = new StitchServiceError(errorMsg, errorCode);
        break;
      case Event.MESSAGE_EVENT:
        this.data = EJSON.parse(decodedData, { strict: false });
        if (decoder) {
          this.data = decoder.decode(this.data);
        }
        break;
    }
  }
}
github mongodb / stitch-js-sdk / packages / react-native / services / http / __tests__ / HttpServiceClientIntTests.ts View on Github external
const response = await httpClient.execute(goodRequest);

      if (i !== retryAttempts && response.statusCode !== 200) {
        await sleep(5000);
        continue;
      }

      expect("200 OK").toEqual(response.status);
      expect(200).toEqual(response.statusCode);
      expect(response.contentLength).toBeGreaterThanOrEqual(300);
      expect(response.contentLength).toBeLessThanOrEqual(500);
      expect(response.body).toBeDefined();
      expect(response.headers['Content-Type'].length).toEqual(1);
      expect(response.headers['Content-Type'][0]).toEqual('application/json');
      
      const dataDoc = EJSON.parse(String(response.body!!), { relaxed: true });
      expect(body).toEqual(dataDoc.data);
      const headersDoc = dataDoc.headers;
      expect("value1,value2").toEqual(headersDoc.Myheader);
      expect("bob=barker").toEqual(headersDoc.Cookie);
    }
  });
});
github pkosiec / mongo-seeding / core / src / populator / filesystem.ts View on Github external
readFile(path: string): any {
    const fileExtension = extname(path);
    if (fileExtension === '.json') {
      const content = readFileSync(path, 'utf-8');
      return EJSON.parse(content, {
        relaxed: true,
      });
    }

    return require(path);
  }
}
github mongodb / stitch-js-sdk / packages / core / admin-client / src / Resources.ts View on Github external
.then(response =>
        deserialize(EJSON.parse(response.body!), type)
      );
github mongodb / stitch-js-sdk / packages / core / admin-client / src / apps / AppsResources.ts View on Github external
return this.adminAuth.doAuthenticatedRequest(req).then(response => {
      checkEmpty(response);
      return deserialize(EJSON.parse(response.body!), App);
    });
  }
github mongodb / stitch-js-sdk / packages / core / admin-client / src / Resources.ts View on Github external
.then(response =>
        EJSON.parse(response.body!).map(val => deserialize(val, type))
      );
github mongodb / stitch-js-sdk / packages / core / sdk / src / internal / net / StitchAppRequestClient.ts View on Github external
.then(resp => {
        this.appMetadata = AppMetadata.fromJSON(EJSON.parse(resp.body));
        return this.appMetadata;
      });
  }