THIS PROJECT IS NOT MAINTAINED. IF YOU'RE INTERESTED, PLEASE FORK.
ALSO NOTE: THIS IS A WORK IN PROGRESS THAT IS NOT CURRENTLY FUNCTIONAL.
This is a complete implementation, for Python 2.7, of EJSON as specified in version 0.6.4 the Meteor documentation, found here.
The EJSON specification assumes JavaScript, so this version deviates from the specification as follows:
- Pythonic naming of methods (e.g.,
from_json_valueinstead offromJSONValue) - aliases where more natural Python method names exist (e.g.,
dumpsis aliased tostringify). - Python data types in place of the specified JavaScript data types (e.g.,
bytearrayinstead ofUint8Array). - virtual base class for user defined datatypes:
ejson.CustomType
Want this to be Python 3 compatible? Send pull requests! :)
EJSON is an extension of JSON to support more types. It supports all JSON-safe types, as well as:
- Date (Python
datetimewithejson.CustomTypemixin) - Binary (Python
bytearraywithejson.CustomTypemixin) - User-defined types (see
ejson.add_type)
All EJSON serializations are also valid JSON. For example an object with a date and a binary buffer would be serialized in EJSON as:
{
"d": {"$date": 1358205756553},
"b": {"$binary": "c3VyZS4="}
}
Parse a string into an EJSON value. Throws an error if the string is not valid ejson.
str (str)
A string to parse into an EJSON value.
Serialize a value to a string.
For EJSON values, the serialization fully represents the value. For non-EJSON
values, serializes the same way as json.dumps.
val (EJSON-compatible value)
A value to stringify.
Deserialize an EJSON value from its plain JSON representation (that is, a
dict).
val (JSON-compatible value)
A value to deserialize into ejson.
Serialize an EJSON-compatible value into its plain JSON representation (that
is, a dict).
val (EJSON-compatible value)
A value to serialize to plain JSON.
Return True if a and b are equal to each other. Return False
otherwise. Uses the __eq__ method on a if present, otherwise performs a
deep comparison.
If key_order_sensitive is True, then the order of the keys in a and b
must be identical for a and b to be equal.
a (EJSON-compatible object)
b (EJSON-compatible object)
key_order_sensitive (Boolean)
Return a deep copy of val.
val (EJSON-compatible value)
A value to copy.
Allocate a new buffer of binary data that EJSON can serialize.
size (int)
The number of bytes of binary data to allocate.
Buffers of binary data are represented by Binary instances containing
numbers ranging from 0 to 255.
Returns True if x is a buffer of binary data, as returned from
ejson.new_binary. Otherwise, returns False.
Add a custom datatype to EJSON.
Raises ValueError if name is not unique among custom data types
defined in your project.
name (String)
A tag for your custom type (e.g., '$my_type'); must be unique among custom data types defined in
your project, and must match the result of your type's type_name method.
factory (function)
A function that converts a JSON-compatible dict (something like {"$my_type": "some value"}) into an instance of your type. This should be the inverse of
the serialization performed by your type's to_json_value method. Your
factory must raise ValueError if it cannot process the value it receives.
Instances of your custom datatype must inheret and implement
ejson.CustomType, which is a virtual base class.
Return a value r such that r.__eq__(r) is always True, even if r is
later modified.
You must define both
equals(other)and__eq__(other), with one being an alias for the other.
Return True if other has a value equal to self; False otherwise.
other (object)
The object to which to compare self.
The equals method must define an equivalence relation. It must have the
following properties:
- Reflexivity - for any instance
a:a.equals(a)must beTrue. - Symmetry - for any two instances
aandb:a.equals(b)if and only ifb.equals(a). - Transitivity - for any three instances
a,b, andc:a.equals(b)andb.equals(c)impliesa.equals(c).
Return the tag used to identify this type. This must match the tag used to
register this type with ejson.add_type. Something like '$my_type'.
Serialize this instance into a JSON-compatible dict. Something like
{"$my_type": "some value"}.