mongoengine_goodjson package

Submodules

mongoengine_goodjson.decoder module

Human-readable JSON decoder for MongoEngine.

class mongoengine_goodjson.decoder.ObjectHook(cls)[source]

Bases: object

Human readable JSON decoder for MongoEngine.

Note the instance of this object is callable object, so you can create the instance of this class, then, put object_hook kwargs.

mongoengine_goodjson.decoder.generate_object_hook

alias of mongoengine_goodjson.decoder.ObjectHook

mongoengine_goodjson.document module

Documents implementing human-readable JSON serializer.

class mongoengine_goodjson.document.Document(*args, **values)[source]

Bases: mongoengine_goodjson.document.Helper, mongoengine.document.Document

Document implementing human-readable JSON serializer.

class mongoengine_goodjson.document.EmbeddedDocument(*args, **kwargs)[source]

Bases: mongoengine_goodjson.document.Helper, mongoengine.document.EmbeddedDocument

EmbeddedDocument implementing human-readable JSON serializer.

class mongoengine_goodjson.document.Helper[source]

Bases: object

Helper class to serialize / deserialize JSON document.

begin_goodjson(cur_depth=0)[source]

Enable GoodJSON Flag.

end_goodjson(cur_depth=0)[source]

Stop GoodJSON Flag.

classmethod from_json(json_str, created=False, *args, **kwargs)[source]

Decode from human-readable json.

Parameters
  • json_str – JSON string that should be passed to the serialized

  • created – a parameter that is passed to cls._from_son.

  • **kwargs (*args,) –

    Any additional arguments that is passed to json.loads.

to_json(*args, **kwargs)[source]

Encode to human-readable json.

Parameters
  • use_db_field – use_db_field that is passed to to_mongo.

  • follow_reference – set True to follow reference field.

  • max_depth – maximum recursion depth. If this value is set to None, the reference is followed until it is end. Setting 0 is the same meaning of follow_reference=0. By default, the value is 3.

  • current_depth – This is used internally to identify current recursion depth. Therefore, you should leave this value as-is. By default, the value is 0.

  • **kwargs (*args,) –

    Any arguments, and keyword arguments to tell json.dumps.

to_mongo(*args, **kwargs)[source]

Convert into mongodb compatible dict.

mongoengine_goodjson.encoder module

Encoder module.

class mongoengine_goodjson.encoder.GoodJSONEncoder(epoch_mode=False, *args, **kwargs)[source]

Bases: json.encoder.JSONEncoder

JSON Encoder for human and MongoEngine.

default(obj)[source]

Convert the object into JSON-serializable value.

Parameters

obj – Object to be converted.

encode(o, **kwargs)[source]

encode.

mongoengine_goodjson.queryset module

Queryset encoder.

class mongoengine_goodjson.queryset.QuerySet(document, collection)[source]

Bases: mongoengine.queryset.queryset.QuerySet

QuerySet that supports human-readable json.

as_pymongo(*args, **kwargs)[source]

Return pymongo encoded dict.

from_json(json_data)[source]

Convert from JSON.

to_json(*args, **kwargs)[source]

Convert to JSON.

mongoengine_goodjson.utils module

Utility functions.

mongoengine_goodjson.utils.id_first(dct)[source]

Return dict that comes id first.

Note that this func returns dct as it is if “id” is not found in dct.

mongoengine_goodjson.utils.method_dispatch(func)[source]

Dispatch class instance method with functools.singledispatch.

mongoengine_goodjson.utils.normalize_reference(ref_id, fld)[source]

Normalize Reference.

mongoengine_goodjson.utils.normalize_reference_dict(ref_id, fld)[source]

Normalize Reference for dict.

mongoengine_goodjson.utils.normalize_reference_list(ref_id, fld)[source]

Normalize Reference for list.

Module contents

JSON serializer/deserializer for humans and MongoEngine.

class mongoengine_goodjson.GoodJSONEncoder(epoch_mode=False, *args, **kwargs)[source]

Bases: json.encoder.JSONEncoder

JSON Encoder for human and MongoEngine.

default(obj)[source]

Convert the object into JSON-serializable value.

Parameters

obj – Object to be converted.

encode(o, **kwargs)[source]

encode.

mongoengine_goodjson.generate_object_hook

alias of mongoengine_goodjson.decoder.ObjectHook

class mongoengine_goodjson.Document(*args, **values)[source]

Bases: mongoengine_goodjson.document.Helper, mongoengine.document.Document

Document implementing human-readable JSON serializer.

class mongoengine_goodjson.EmbeddedDocument(*args, **kwargs)[source]

Bases: mongoengine_goodjson.document.Helper, mongoengine.document.EmbeddedDocument

EmbeddedDocument implementing human-readable JSON serializer.

class mongoengine_goodjson.QuerySet(document, collection)[source]

Bases: mongoengine.queryset.queryset.QuerySet

QuerySet that supports human-readable json.

as_pymongo(*args, **kwargs)[source]

Return pymongo encoded dict.

from_json(json_data)[source]

Convert from JSON.

to_json(*args, **kwargs)[source]

Convert to JSON.

class mongoengine_goodjson.FollowReferenceField(*args, **kwargs)[source]

Bases: mongoengine.fields.ReferenceField

Follow Reference Field.

This field can be treated as a field like ReferenceField, but generates the JSON/dict of the referenced document like embedded document.

Note

This field doesn’t check recursion level. Therefore, please be careful for self-referenced document.

to_mongo(document, **kwargs)[source]

Convert to python-typed dict.

Parameters

document – The document.

to_python(value)[source]

Convert to python-based object.

Parameters

value – The python-typed document.