GithubHelp home page GithubHelp logo

mbr / jsonext Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 4.0 23 KB

Well-structured helpers to help serializing commonly encountered structures to JSON.

License: MIT License

Python 100.00%

jsonext's Introduction

jsonext

https://travis-ci.org/mbr/jsonext.svg?branch=master

jsonext makes it easy to serialize objects outside of the standard Python primitives to JSON:

>>> import jsonext
>>> from datetime import datetime
>>> jsonext.dumps(datetime.now())
'"2014-03-22T22:17:18.304528+00:00"'
>>> jsonext.dumps(i**2 for i in range(10))
'[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]'

It uses mixins to the standard encoder to achieve this and is easily reuse- and extensible. Check out the documentation for details.

jsonext's People

Contributors

mbr avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jsonext's Issues

Unexpected encoding of pandas DataFrame

Not sure where to turn for help, but I'm having a small problem trying to encode a pandas DataFrame. Here's my encoder class:

class JSONEncoder(jsonext.JSONEncoder):
def default(self, obj):
# We make DataFrames look like the tabular data d3 expects.
if isinstance(obj, pd.DataFrame):
# values
arr = obj.to_records()
# keys
names = arr.dtype.names
result = [dict(zip(names, x)) for x in arr]
print ">>> result", result
return result
return super(JSONEncoder, self).default(obj)

(Note the print statement.)

Here's a real simple DataFrame:

import pandas as pd
df = pd.DataFrame(x)
df
a1 a2
0 3 6

and here's what happens:

JSONEncoder().encode(df)
result [{'a1': 3, 'index': 0, 'a2': 6}]
'[{"a1": "3", "index": "0", "a2": "6"}]'

There's that print statement output showing that the three values are indeed integers. However, in the generated JSON string, they have all been turned into strings. Is this a problem with my encoder or with jsonexit.JSONEncoder?

Thx...

Add a clear grant of license

Please add the full license text in a document (e.g. ‘LICENSE.Expat’ in the project root).

Also, add a text (maybe in the README) that clearly grants license to the recipient. A suggestion for the grant of license:

This is free software; you may copy, modify, and/or distribute this work
under the terms of the Expat license as published by James Clark.
See the ‘LICENSE’ file for full license conditions.

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.