Giter Club home page Giter Club logo

ejson's Introduction

ejson

Version npmCICoverage Status

ejson is an extension of JSON to support more types. It supports all JSON-safe types as well as

  • Date: It maintains a date instance instead of transforming it to a string.
  • Binary: Uint8Array
  • User defined types

The ejson module is port of Meteor's EJSON parser which has been made compatible for Node.js and browsers. This way, other real-time libraries can also make use of these extensions.

Installation

The module is released through npm:

npm install --save ejson

Usage

The API is exactly the same as the API that Meteor provides as it uses exactly the same code. The most important API's are:

  • EJSON.clone
  • EJSON.parse
  • EJSON.stringify

And some utility methods:

  • EJSON.toJSONValue
  • EJSON.fromJSONValue
  • EJSON.isBinary
  • EJSON.newBinary
  • EJSON.equals

See the meteor EJSON documentation for more detailed information.

License

This module is licensed under MIT. Same as Meteor.js is.

ejson's People

Contributors

3rd-eden avatar forbeslindesay avatar greenkeeper[bot] avatar lpinca avatar tombyrer avatar tpxp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ejson's Issues

EJSON produces a TypeError in momentjs

moment.js:129 Uncaught TypeError: m._d.getTime is not a function(โ€ฆ) valid__isValid @ moment.js:129 moment_valid__isValid @ moment.js:3215 toJSON @ moment.js:3211 EJSON.stringify @ ejson.js:343 stringify @ reactive-dict.js:6 set @ reactive-dict.js:61

Some test code to produce the issue

Session.set('date', moment() )

The root cause seems to be that EJSON changes the internal date object of momentjs. That makes it impossible for momentjs to properly implement its own toJSON serialization.

Fix a typo in the readme.md file

Hello!

First of all, this project is awesome. Thank you for bringing it live!

I have just found a little typo in the Readme.md, which can be confusing. At the very beginning, you talk about binary types and refer to the type Uinit8Array which doesn't exists as opposed to Uint8Array (you wrote init instead of int).

Thank your for correcting it!

Error when parsing mixed quotes

Stringifying an object or string that has content with multiple quotes throws an error when parsing back to object again.

const data = prompt("Type text with three double quotes"); // insert three quotes like this """
EJSON.stringify(EJSON(parse(data)));

Throws:

Uncaught SyntaxError: Unexpected string in JSON at position 2
    at JSON.parse (<anonymous>)
    at Object.EJSON.parse.item [as parse] (ejson.js:411)
    at <anonymous>:1:23

The browser's own JSON.stringify and JSON.parse combination works just fine.

EJSON.stringify with indent is broken

It would appear this version of EJSON is missing the _canonicalStringify function which is present in the original Meteor library. This makes something trivial like EJSON.stringify({test:1},{indent:true}) fail.

ObjectID rejected by native Mongo driver

Hello,

I would like to use ejson with native mongodb driver and nodeJS.

But if I create a model with _id : MongoDb.ObjectID Type. And convert to ejson and parse again, ObjectID will be rejected by mongo during save :

Original MongoDb.ObjectID object
image

toJSONValue
image

Then after parsing I will got
image

So the result is not exactly the same. And after trying to save in mongo I got

Error: object [{"_bsontype":"ObjectID","id":{"0":94,"1":231,"2":159,"3":38,"4":182,"5":28,"6":78,"7":125,"8":187,"9":83,"10":55,"11":123}}] is not a valid ObjectId
at serializeObjectId (/Users/mm/Documents/py1/node_modules/bson/lib/bson/parser/serializer.js:287:11)
at serializeInto (/Users/mm/Documents/py1/node_modules/bson/lib/bson/parser/serializer.js:931:17)
at serializeObject (/Users/mm/Documents/py1/node_modules/bson/lib/bson/parser/serializer.js:347:18)
at serializeInto (/Users/mm/Documents/py1/node_modules/bson/lib/bson/parser/serializer.js:727:17)
at serializeObject (/Users/mm/Documents/py1/node_modules/bson/lib/bson/parser/serializer.js:347:18)
at serializeInto (/Users/mm/Documents/py1/node_modules/bson/lib/bson/parser/serializer.js:937:17)
at BSON.serialize (/Users/mm/Documents/py1/node_modules/bson/lib/bson/bson.js:64:28)
at Msg.serializeBson (/Users/mm/Documents/py1/node_modules/mongodb/lib/core/connection/msg.js:125:22)
at Msg.makeDocumentSegment (/Users/mm/Documents/py1/node_modules/mongodb/lib/core/connection/msg.js:117:33)
at Msg.toBin (/Users/mm/Documents/py1/node_modules/mongodb/lib/core/connection/msg.js:103:25)
at serializeCommand (/Users/mm/Documents/py1/node_modules/mongodb/lib/core/connection/pool.js:740:41)
at Pool.write (/Users/mm/Documents/py1/node_modules/mongodb/lib/core/connection/pool.js:897:3)
at _command (/Users/mm/Documents/py1/node_modules/mongodb/lib/core/wireprotocol/command.js:107:10)
at command (/Users/mm/Documents/py1/node_modules/mongodb/lib/core/wireprotocol/command.js:25:5)
at writeCommand (/Users/mm/Documents/py1/node_modules/mongodb/lib/core/wireprotocol/write_command.js:47:3)
at Object.insert (/Users/mm/Documents/py1/node_modules/mongodb/lib/core/wireprotocol/index.js:6:5)

There is a way to specify the expected format for ObjectID ?

thank's

Remove underscore.js

72% of the size of EJSON is underscore yet only a small portion of it is used.

By removing the underscore requirement and replacing it with only the specific methods we need from lodash, we can reduce the size of EJSON by more than half.

https://bundlephobia.com/[email protected]

This won't affect the weight for users that are already using underscore very much and it would remove loading all of underscore for lodash users. And if you don't use either, it reduces the weight to a minimum.

For example, instead of

import _ from 'underscore'
_.has(object, path)

We can do

import has from 'lodash/has'
has(object, path)

I can make a PR if you are in agreement.

Adding support for Maps

I'd like to add support for Map objects. I have some working code, but it's not clear where I should add code for new types. I also saw an issue on the meteor repo for this, and would like to submit a PR here so I can EJSON Maps in plain Node apps as well.

Any thoughts on how to add additional types?

import { Meteor } from 'meteor/meteor';
import { EJSON } from 'meteor/ejson';

Map.prototype.toJSONValue = function () {
  return JSON.stringify([...this]);
};

Map.prototype.typeName = function () {
  return 'Map';
};

EJSON.addType('Map', (jsonStr) => {
  return new Map(JSON.parse(jsonStr));
});

Release v1.0.0

Can we release v1.0.0 as ejson now? I want to depend on it :)

Index.js is missing in npm package, causing build to fail

Package.json,

  "dependencies": {
    "ejson": "^2.1.0",
    "faye-websocket": "^0.10.0"
  },

Source,

var EJSON = require('ejson');

Running tests,

 npmt

> [email protected] test /Users/alexanderbeletsky/Development/Projects/ddp-server-event
> ./node_modules/mocha/bin/mocha

module.js:338
    throw err;
          ^
Error: Cannot find module 'ejson'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/alexanderbeletsky/Development/Projects/ddp-server-event/source/index.js:5:13)

The package.json has reference to index.js which is missing in package itself,

screenshot 2015-08-10 11 13 32

Update to latest EJSON version

Hey there!
We just wanted to use your package but it seems to be out of date and generate non-compatible EJSON compared to the latest Meteor version!

could publish a newer version with the latest code from Meteor?
thanks a lot!

Serializing types without monkeypatching

The current addType API, when applied to 3rd party classes, requires monkeypatching the 3rd party classes. It would be nice to devise an API for registering a full set of CustomType callbacks that would apply on a type. The library could still monkeypatch as a matter of implementation, but perhaps using Symbols so it cannot conflict with existing symbols.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.