All Projects → hiroaki-yamamoto → Mongoengine Goodjson

hiroaki-yamamoto / Mongoengine Goodjson

Licence: mit
More human-readable json serializer/deserializer for MongoEngine

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Mongoengine Goodjson

Panko serializer
High Performance JSON Serialization for ActiveRecord & Ruby Objects
Stars: ✭ 266 (+375%)
Mutual labels:  serialization, json-serialization
Thorsserializer
C++ Serialization library for JSON
Stars: ✭ 241 (+330.36%)
Mutual labels:  serialization, json-serialization
JsonFormatter
Easy, Fast and Lightweight Json Formatter. (Serializer and Deserializer)
Stars: ✭ 26 (-53.57%)
Mutual labels:  serialization, json-serialization
Eminim
JSON serialization framework for Nim, works from a Stream directly to any type and back. Depends only on stdlib.
Stars: ✭ 32 (-42.86%)
Mutual labels:  serialization, json-serialization
Savegamefree
Save Game Free is a free and simple but powerful solution for saving and loading game data in unity.
Stars: ✭ 279 (+398.21%)
Mutual labels:  serialization, json-serialization
Fastjson
A fast JSON parser/generator for Java.
Stars: ✭ 23,997 (+42751.79%)
Mutual labels:  serialization, json-serialization
Metastuff
Code I use in my game for all serialization/deserialization/introspection stuff
Stars: ✭ 337 (+501.79%)
Mutual labels:  serialization, json-serialization
Fasteasymapping
A tool for fast serializing & deserializing of JSON
Stars: ✭ 556 (+892.86%)
Mutual labels:  serialization, json-serialization
Nippy
High-performance serialization library for Clojure
Stars: ✭ 838 (+1396.43%)
Mutual labels:  serialization
Nesm
NESM stands for Nim's Easy Serialization Macro. The macro that allowing generation of serialization functions by one line of code! (It is a mirror of https://gitlab.com/xomachine/NESM)
Stars: ✭ 43 (-23.21%)
Mutual labels:  serialization
Tjson.rb
Ruby implementation of TJSON
Stars: ✭ 22 (-60.71%)
Mutual labels:  serialization
Jsondoc
JSON object for Delphi based on IUnknown and Variant
Stars: ✭ 20 (-64.29%)
Mutual labels:  json-serialization
Nameof
Nameof operator for modern C++, simply obtain the name of a variable, type, function, macro, and enum
Stars: ✭ 1,017 (+1716.07%)
Mutual labels:  serialization
Avsc
Avro for JavaScript ⚡️
Stars: ✭ 930 (+1560.71%)
Mutual labels:  serialization
Tech1 Benchmarks
Java JMH Benchmarks repository. No Longer Supported.
Stars: ✭ 50 (-10.71%)
Mutual labels:  serialization
Spray Json
A lightweight, clean and simple JSON implementation in Scala
Stars: ✭ 917 (+1537.5%)
Mutual labels:  json-serialization
Protobuf Convert
Macros for convenient serialization of Rust data structures into/from Protocol Buffers 3
Stars: ✭ 22 (-60.71%)
Mutual labels:  serialization
Batchman
This library for Android will take any set of events and batch them up before sending it to the server. It also supports persisting the events on disk so that no event gets lost because of an app crash. Typically used for developing any in-house analytics sdk where you have to make a single api call to push events to the server but you want to optimize the calls so that the api call happens only once per x events, or say once per x minutes. It also supports exponential backoff in case of network failures
Stars: ✭ 50 (-10.71%)
Mutual labels:  serialization
Beeschema
Binary Schema Library for C#
Stars: ✭ 46 (-17.86%)
Mutual labels:  serialization
Granola
Simple JSON serializers (Cereal-izers. GET IT?)
Stars: ✭ 39 (-30.36%)
Mutual labels:  json-serialization

More human readable JSON serializer/de-serializer for MongoEngine

Build Status Test Coverage Maintainability Documentation Status Image

What This?

This script has MongoEngine Document json serialization more-natural.

Why this invented?

Using MongoEngine to create something (e.g. RESTful API), sometimes you might want to serialize the data from the db into JSON, but some fields are weird and not suitable for frontend/api:

{
  "_id": {
    "$oid": "5700c32a1cbd5856815051ce"
  },
  "name": "Hiroaki Yamamoto",
  "registered_date": {
      "$date": 1459667811724
  }
}

The points are 2 points:

  • _id might not be wanted because jslint disagrees _ character unless declaring jslint nomen:true
  • There are sub-fields such $oid and $date. These fields are known as MongoDB Extended JSON. However, considering MongoEngine is ODM and therefore it has schema-definition methods, the fields shouldn't have the special fields. In particular problems, you might get No such property $oid of undefined error when you handle above generated data on frontend.

To solve the problems, the generated data should be like this:

{
  "id": "5700c32a1cbd5856815051ce",
  "name": "Hiroaki Yamamoto",
  "registered_date": 1459667811724
}

Making above structure can be possible by doing re-mapping, but if we do it on API's controller object, the code might get super-dirty:

"""Dirty code."""
import mongoengine as db


class User(db.Document):
  """User class."""
  name = db.StringField(required=True, unique=True)
  registered_date = db.DateTimeField()


def get_user(self):
  """Get user."""
  models = [
    {
      ("id" if key == "_id" else key): (
        value.pop("$oid") if "$oid" in value and isinstance(value, dict)
        else value.pop("$date") if "$date" in value and isinstance(value, dict)
        else value  #What if there are the special fields in child dict?
      )
      for (key, value) in doc.items()
    } for doc in User.objects(pk=ObjectId("5700c32a1cbd5856815051ce"))
  ]
  return json.dumps(models, indent=2)

To give the solution of this problem, I developed this scirpt. By using this script, you will not need to make the transform like above. i.e.


"""A little-bit clean code."""

import mongoengine as db
import mongoengine_goodjson as gj


class User(gj.Document):
  """User class."""
  name = db.StringField(required=True, unique=True)
  registered_date = db.DateTimeField()


def get_user(self):
  """Get user."""
  return model_cls.objects(
    pk=ObjectId("5700c32a1cbd5856815051ce")
  ).to_json(indent=2)

How to use it

Generally you can define the document as usual, but you might want to inherits mongoengine_goodjson.Document or mongoengine_goodjson.EmbeddedDocument.

Here is the example:

"""Example schema."""

import mongoengine_goodjson as gj
import mongoengine as db


class Address(gj.EmbeddedDocument):
    """Address schema."""

    street = db.StringField()
    city = db.StringField()
    state = db.StringField()


class User(gj.Document):
    """User data schema."""

    name = db.StringField()
    email = db.EmailField()
    address = db.EmbeddedDocumentListField(Address)

More details... there's the doc!

If you want to know more, there's read the doc that you want to read. You can now read the doc with drinking a cup of coffee!!

Contribute

Please read the doc for the detail.

License (MIT License)

See LICENSE.md

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].