JysonFaq
Version 1 (Alan Kennedy, 03/21/2009 12:14 pm)
| 1 | 1 | h1. Jyson FAQ |
|
|---|---|---|---|
| 2 | 1 | ||
| 3 | 1 | {{toc}} |
|
| 4 | 1 | ||
| 5 | 1 | h2. Why use Jyson instead of a java codec? |
|
| 6 | 1 | ||
| 7 | 1 | The advantage of using jyson instead of a java codec is that jyson produces a hierarchy of jython objects, namely "org.python.core.PyObject":http://www.jython.org/docs/javadoc/org/python/core/PyObject.html . These objects are custom designed to be manipulated by jython code. |
|
| 8 | 1 | ||
| 9 | 1 | On the other hand, a java codec might produce a "java.util.Map":http://java.sun.com/j2se/1.5.0/docs/api/java/util/Map.html or "java.util.List":http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html or other java objects, which it is possible to manipulate from jython code, but would not be natural jython objects. |
|
| 10 | 1 | ||
| 11 | 1 | Consider the following |
|
| 12 | 1 | ||
| 13 | 1 | <pre> |
|
| 14 | 1 | Jython 2.5b2+ (trunk, Mar 8 2009, 20:40:45) |
|
| 15 | 1 | [Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.5.0_17 |
|
| 16 | 1 | Type "help", "copyright", "credits" or "license" for more information. |
|
| 17 | 1 | >>> from com.xhaus.jyson import JysonCodec |
|
| 18 | 1 | >>> l = JysonCodec.loads('[3,1,4,1,5,9,2,7]') |
|
| 19 | 1 | >>> type(l) |
|
| 20 | 1 | <type 'list'> |
|
| 21 | 1 | >>> l |
|
| 22 | 1 | [3, 1, 4, 1, 5, 9, 2, 7] |
|
| 23 | 1 | >>> l.count(1) |
|
| 24 | 1 | 2 |
|
| 25 | 1 | >>> import java |
|
| 26 | 1 | >>> jl = java.util.ArrayList([3,1,4,1,5,9,2,7]) |
|
| 27 | 1 | >>> type(jl) |
|
| 28 | 1 | <type 'java.util.ArrayList'> |
|
| 29 | 1 | >>> jl |
|
| 30 | 1 | [3, 1, 4, 1, 5, 9, 2, 7] |
|
| 31 | 1 | >>> jl.count(1) |
|
| 32 | 1 | Traceback (most recent call last): |
|
| 33 | 1 | File "<stdin>", line 1, in <module> |
|
| 34 | 1 | AttributeError: 'java.util.ArrayList' object has no attribute 'count' |
|
| 35 | 1 | >>> |
|
| 36 | 1 | </pre> |
|
| 37 | 1 | ||
| 38 | 1 | h2. Why use jyson instead of a python codec? |
|
| 39 | 1 | ||
| 40 | 1 | Simple answer: speed. |
|
| 41 | 1 | ||
| 42 | 1 | When you run a pure python program under jython, there is an "interpretive overhead":http://en.wikipedia.org/wiki/Interpreter_(computing)#Efficiency which makes it likely that the pure python code will run slower, and use more memory, than the equivalent code written in java (although "advances in JVM optimization":http://blogs.sun.com/jrose/entry/bravo_for_the_dynamic_runtime mean that this performance difference is not guaranteed). |
|
| 43 | 1 | ||
| 44 | 1 | Jyson is written in pure java, and is thus highly likely to be significantly faster and more resource efficient than a pure python codec. |