Decoding json in jython using only default libraries

I have a question on how to enable json in a jython script. Here is my scenario:

  • I am running a python application on my laptop
  • This application must exchange data with a jython application running in a hosting environment in a third-party cloud environment.
  • I have no way to add third-party modules to this environment (so I can not install com.xhaus.jyson, for example)
  • This probably means I'm limited to functions that are native to java - org.json.JSONObject is possible

So, with these limitations, I want to take a dictionary object on my laptop, turn it into json, deliver it to a hosted jython application, and then use my own jython or java tools to return it back to this dictionary object so that I can continue work on it in my script hosted in the cloud.

I already know how to do this in "regular" python. It is easy. import json and go crazy. But my java kung fu is weak and I have never worked on jython before.

So I'm trying to figure out if this is possible, if it can be done reliably and easily, using the java underlying jython, or if I would be better off using something like ast and just send the dictionary as a string literal. I honestly would prefer to stick with json for all ordinary people like json, so any help using java libraries to do this job would be appreciated.

+6
source share
6 answers

I forgot about this question. My main problem was that I used a third-party cloud offering and they were the owners of the Jython installation, so I was limited to what I could change in the Jython environment. At that time, I thought I could use a JAVA library that would be available for jython to solve this problem, but it never worked.

While jython was out of my control, I controlled how I sent the data, so instead of using JSON, I sent formatted strings, and then used the python ast library that was in jython to turn these strings into python objects.

At the end, he looked something like this:

thestring = """['name', 'quest', 'favorite color']""" theobject = ast.literal_eval(thestring) 

This type of logic allows my python script on my local machine mail strings in a jython web application to convert these strings to python data types and then use them. That was exactly what I wanted to do with JSON, without actually using JSON - they were python dicts, so it looked very much like JSON if you didn't pay attention to it.

Thank you all for your suggestions.

+2
source

You can use Jackson or GSON . You can use anything listed in JSON.org under Java, perhaps you can use material under "Python" (for example simplejson ).

0
source

You can use simplejson , which can be used as pure python, so it will work on Jython. By including it in the same source folder as your other code, there is no need for a special installation.

0
source

Jyson seems like an open source project that implements python json codec in pure Java.

Download it here: http://opensource.xhaus.com/projects/jyson/files .

Then unzip and add jyson-1.0.2 / lib / jyson-1.0.2.jar to your CLASSPATH.

Then import like this:

  import com.xhaus.jyson.JysonCodec as json 

Found this information here: http://aholzner.wordpress.com/2010/07/31/using-json-from-jython/ . It works for me.

0
source

This question is a bit outdated, but I came across this having a similar problem:

https://support.xebialabs.com/hc/communities/public/questions/201998425-Use-json-with-Jython-script

Essentially, this is how I solved it (using simplejson ):

 try: sys.path.append('<PATH TO SIMPLEJSON ROOT>') import simplejson as json except Exception, e: print e 
0
source

If you are using the latest version of jython , you have access to the jyth python library. So:

 import json mydict_as_json = json.dumps(mydict) # send over the wire # on the remote side import json mydict = json.load(mydict_as_json_from_remote_as_file_like_object) 

Hope this helps ...

0
source

All Articles