stefankoegl / python-json-pointer

Resolve JSON Pointers in Python
https://python-json-pointer.readthedocs.org/
Other
141 stars 43 forks source link

unicode issue using Python 2.7.2 #4

Closed lshabbot closed 11 years ago

lshabbot commented 11 years ago

I am using Python 2.7.2 and did a pip install jsonpatch

I am seeing the following error when trying to apply a patch to my json doc.

patch = jsonpatch.apply_patch(data['request_body'], myPatch) File "/Users/test/projects/venv/lib/python2.7/site-packages/jsonpatch.py", line 102, in apply_patch return patch.apply(doc, in_place) File "/Users/test/projects/venv/lib/python2.7/site-packages/jsonpatch.py", line 301, in apply obj = operation.apply(obj) File "/Users/test/projects/venv/lib/python2.7/site-packages/jsonpatch.py", line 393, in apply subobj, part = self.pointer.to_last(obj) File "/Users/test/projects/venv/lib/python2.7/site-packages/jsonpointer.py", line 129, in to_last return doc, self.get_part(doc, self.parts[-1]) File "/Users/test/projects/venv/lib/python2.7/site-packages/jsonpointer.py", line 168, in get_part raise JsonPointerException("Unknown document type '%s'" % (doc.class,)) JsonPointerException: Unknown document type '<type 'unicode'>'

stefankoegl commented 11 years ago

What values are data['request_body'] and myPatch? Can you provide a small, self-contained example?

lshabbot commented 11 years ago

pip install Flask

pip install flask-restful

pip install jsonpatch

Sample curl command

curl --insecure -H "Content-Type:application/json" -d "{ \"request_body\": \"{\"type\" : \"dog\",\"number\" : \"1108-bd031334\", \"owner_name\" : \"Joe Smith\",\"billing_address\" : {\"line1\" : \"49 N. Main St.\",\"city\" : \"Johnstown\",\"state\" : \"OH\",\"postal_code\" : \"43210\",\"country_code\" : \"US\"}}\" }" "http://127.0.0.1:5000/"

Sample Program

import json import jsonpatch from flask import Flask, request, abort from flask.ext.restful import Api, Resource

app = Flask(name) api = Api(app)

replacepatch = '[{"op": "replace", "path": "/number", "value" : "zzzz"}]'

class TestService(Resource): def post(self): if not request.json: abort(400) data = json.loads(request.data) print "data: ", data requestbody = data['request_body'] print "request_body: ", requestbody patch = jsonpatch.apply_patch(requestbody, replacepatch) print "patch: ", patch return 200

api.add_resource(TestService, '/')

if name == 'main': app.run(debug=True)

Output

Traceback (most recent call last): File "/Users/user/projects/venv/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request rv = self.dispatch_request() File "/Users/user/projects/venv/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request return self.view_functionsrule.endpoint File "/Users/user/projects/venv/lib/python2.7/site-packages/flask_restful/init.py", line 242, in wrapper resp = resource(_args, _kwargs) File "/Users/user/projects/venv/lib/python2.7/site-packages/flask/views.py", line 83, in view return self.dispatch_request(_args, _kwargs) File "/Users/user/projects/venv/lib/python2.7/site-packages/flask_restful/init.py", line 318, in dispatch_request resp = meth(_args, *_kwargs) File "test.py", line 19, in post patch = jsonpatch.apply_patch(requestbody, replacepatch) File "/Users/user/projects/venv/lib/python2.7/site-packages/jsonpatch.py", line 102, in apply_patch return patch.apply(doc, in_place) File "/Users/user/projects/venv/lib/python2.7/site-packages/jsonpatch.py", line 301, in apply obj = operation.apply(obj) File "/Users/user/projects/venv/lib/python2.7/site-packages/jsonpatch.py", line 393, in apply subobj, part = self.pointer.to_last(obj) File "/Users/user/projects/venv/lib/python2.7/site-packages/jsonpointer.py", line 129, in to_last return doc, self.get_part(doc, self.parts[-1]) File "/Users/user/projects/venv/lib/python2.7/site-packages/jsonpointer.py", line 168, in get_part raise JsonPointerException("Unknown document type '%s'" % (doc.class,)) JsonPointerException: Unknown document type '<type 'unicode'>'

stefankoegl commented 11 years ago

There is probably some issue that is not related to jsonpointer. Please reduce your example so that no unrelated code is used. In the end it should be as simple as

doc = something
patch = somethingelse
jsonpatch.apply_patch(doc, patch)

As long as I don't know what you're passing to the function, I can't tell you what's wrong.

lshabbot commented 11 years ago

I think it is something I introduced. I did the bare-bones app, and it works. I will investigate on my in. Sorry for the false alarm :)