Code Monkey home page Code Monkey logo

Comments (5)

mmerickel avatar mmerickel commented on August 26, 2024

It sounds like you've written a custom serializer and it's not returning bytes.

from webob.

russellballestrini avatar russellballestrini commented on August 26, 2024

@mmerickel thanks for the rapid call out, here is another snippet:

from pyramid.config import Configurator

# Cookie only session, not encrypted but signed to prevent tampering.
from pyramid.session import SignedCookieSessionFactory

from miscutils import get_children_settings


def session_serializer(serializer_name):
    if serializer_name == "json":
        import json
        return json
    import pickle
    return pickle


def main(global_config, **settings):
    """This function returns a Pyramid WSGI application."""

    # setup session factory to use unencrypted but signed cookies
    session_settings = get_children_settings(settings, "session")
    session_settings["serializer"] = session_serializer(
        session_settings.get("serializer")
    )
    session_factory = SignedCookieSessionFactory(**session_settings)

    with Configurator(settings=settings, session_factory=session_factory) as config:
        # all of the models for persisting data into our database.
        config.include(".models")
        # all of the web application routes.
        config.include(".routes")
        # all of the data we attach to each inbound request.
        config.include(".request_methods")
        # enable jinja2 templating engine.
        config.include(".config_jinja2")
        # scan each of these includes for additional configuration.
        config.scan()

    return config.make_wsgi_app()

from webob.

mmerickel avatar mmerickel commented on August 26, 2024

json.dumps() returns a unicode string, you'll need to json.dumps().encode('utf8') to conform to the serializer contract.

from webob.

russellballestrini avatar russellballestrini commented on August 26, 2024

Thanks, I figured as much. Here is a first pass at making json compatible with the serializer contract. Let me know if there is a better way.

First I created a new file / module which I may import and pass to the session configurator:

import json

loads = json.loads

def dumps(obj, *args, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw):
    result = json.dumps(obj, *args, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
    return result.encode("utf-8")

and then in my main function above, I changed the session_serializer function to look like so:

def session_serializer(serializer_name):
    if serializer_name == "json":
        import make_post_sell.lib.json_session_serializer as json_serializer
        return json_serializer
    import pickle
    return pickle

This appears to work so I will close this issue. Please feel free to comment after closing if my approach is bad.

from webob.

russellballestrini avatar russellballestrini commented on August 26, 2024

Actually looks like we already have a batteries included solution, see: https://docs.pylonsproject.org/projects/pyramid/en/latest/api/session.html#pyramid.session.JSONSerializer

def session_serializer(serializer_name):
    if serializer_name == "json":
        from pyramid.session import JSONSerializer
        return JSONSerializer()

    from pyramid.session import PickleSerializer
    return PickleSerializer()

from webob.

Related Issues (20)

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.