# Copyright (c) 2016 ma-ha, The MIT License (MIT)
import json
import inspect

# Thanks to "tobige":
# ref http://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable#3768975
class ObjectEncoder(json.JSONEncoder):
    
    def default(self, obj):
        if hasattr(obj, "to_json"):
            return self.default(obj.to_json())
        elif hasattr(obj, "__dict__"):
            d = dict(
                (key, value)
                for key, value in inspect.getmembers(obj)
                if not key.startswith("__")
                and not inspect.isabstract(value)
                and not inspect.isbuiltin(value)
                and not inspect.isfunction(value)
                and not inspect.isgenerator(value)
                and not inspect.isgeneratorfunction(value)
                and not inspect.ismethod(value)
                and not inspect.ismethoddescriptor(value)
                and not inspect.isroutine(value)
            )
            return self.default(d)
        return obj