Coder Social home page Coder Social logo

pyjsoncpp's Introduction

PyJsonCpp: Python bindings for JsonCpp

The JsonCpp project provides an excellent in-memory JSON data structure as well as string writers and parsers. Here are Python bindings for JsonCpp using Cython.

Installation

Installing PyJsonCpp from source is easy. First, download or clone this repository. Then run the setup commands from the base directory:

cd pyne/
python setup.py install --user

These bindings require on Cython v0.17+. The tests rely on nose.

Usage Example

The Value object is the main interface for Values may be converted to and from regular Python types. These have the normal behavior for their type.

>>> from jsoncpp import Value, Reader, FastWriter, StyledWriter

>>> v = Value({'name': 'Terry Jones', 'age': 42.0})
>>> v['quest'] = "To find the grail."
>>> v.keys()
['age', 'name', 'quest']
>>> v['name']
'Terry Jones'

>>> v = Value([1, 2, 5, 3])
>>> v[1:-1]
[2, 5]
>>> v[1:-1] = [42, 65]
>>> v
[1, 42, 65, 3]

>>> v = Value("No one expects the Spanish Inquisition!!!!")
>>> len(v)
42

The Python Value class provides a view into the underlying C++ class. This allows you to create several views into the same data. For example, start with the following nested dictionary:

# make a nested dict and a view into a top-level item
>>> v = Value({'a': {'b': 14}})
>>> view_a = v['a']
>>> view_a
{"b":14}

# add an item to the view
>>> view_a['c'] = 16
>>> view_a
{"b":14,"c":16}

# this item is present in the original value
>>> v
{"a":{"b":14,"c":16}}

Furthermore, there is a Reader class for converting JSON strings or files into Value instances. There are also two writer classes, FastWriter and StyledWriter, for converting Value instances into compact and human-readable strings respectively. For example:

>>> v = Value({'hello': 1})
>>> fw = FastWriter()
>>> fw.write(v)
'{"hello":1}\n'

>>> sw = StyledWriter()
>>> print sw.write(v)
{
   "hello" : 1
}

>>> r = Reader()
>>> new_v = r.parse('{"hello":1}\n')
>>> isinstance(new_v, Value)
True

pyjsoncpp's People

Contributors

scopatz avatar erelson avatar chrisdembia avatar paulromano avatar

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.