def onCompletion(self, json_str): from pyjamas.JSONParser import JSONParser response = JSONParser().decodeAsObject(json_str) if not response: self.request.handler.onRemoteError(0, "Server Error or Invalid Response", self.request) elif response.has_key("error") and response['error']: error = response["error"] self.request.handler.onRemoteError(0, error, self.request) else: self.request.handler.onRemoteResponse(response["result"], self.request)
class JSONService: def __init__(self, url, handler = None): """ Create a JSON remote service object. The url is the URL that will receive POST data with the JSON request. See the JSON-RPC spec for more information. The handler object should implement onRemoteResponse(value, requestInfo) to accept the return value of the remote method, and onRemoteError(code, message, requestInfo) to handle errors. """ from pyjamas.JSONParser import JSONParser self.parser = JSONParser() self.url = url self.handler = handler def callMethod(self, method, params, handler = None): if handler is None: handler = self.handler if handler is None: return self.__sendNotify(method, params) else: return self.__sendRequest(method, params, handler) def onCompletion(self): pass def __sendNotify(self, method, params): msg = {"id":None, "method":method, "params":params} msg_data = self.parser.encode(msg) if not HTTPRequest().asyncPost(self.url, msg_data, self): return -1 return 1 def __sendRequest(self, method, params, handler): id = pygwt.getNextHashId() msg = {"id":id, "method":method, "params":params} msg_data = self.parser.encode(msg) request_info = JSONRequestInfo(id, method, handler) if not HTTPRequest().asyncPost(self.url, msg_data, JSONResponseTextHandler(request_info)): return -1 return id
def wdecode(self,astring): anobj = None JS(""" anobj = unescape(astring); """) nastring = ''' [ { "body" : "escreva", "line" : 1 }, { "body" : "aqui", "line" : 2 } ]''' #JS(""" # anobj = JSON.parse(astring); # """) #return anobj astring = anobj.replace("'",'"') jparser = JSONParser() return jparser.decode(astring) #anobj
def parsePhotos(self, items): photo_list = JSONParser().jsObjectToPyObject(items) self.photos = [] for i in range(len(photo_list)): index = "%s" % i aphoto = {} aphoto['thumb'] = HTML('<img src="' + photo_list[index]["media$group"] ["media$thumbnail"]["1"]["url"] + '"/>') aphoto['full'] = photo_list[index]["media$group"]["media$content"][ "0"]["url"] self.photos.append(aphoto)
def parseAlbums(self, items): album_list = JSONParser().jsObjectToPyObject(items) self.albums = [] for i in range(len(album_list)): index = "%s" % i analbum = {} analbum['title'] = HTML(album_list[index]["title"]["$t"]) analbum['thumb'] = HTML('<img src="' + album_list[index]["media$group"] ["media$thumbnail"]["0"]["url"] + '"/>') url = album_list[index]["id"]["$t"] analbum['id'] = url.split('albumid/')[1].split('?alt')[0] self.albums.append(analbum)
def __init__(self, url, handler = None): """ Create a JSON remote service object. The url is the URL that will receive POST data with the JSON request. See the JSON-RPC spec for more information. The handler object should implement onRemoteResponse(value, requestInfo) to accept the return value of the remote method, and onRemoteError(code, message, requestInfo) to handle errors. """ from pyjamas.JSONParser import JSONParser self.parser = JSONParser() self.url = url self.handler = handler
import sys from HTTPRequest import HTTPRequest # jeeeez... try: # included in python 2.6... from json import dumps, loads except ImportError: try: # recommended library (python 2.5) from simplejson import dumps, loads except ImportError: # who's the pyjs daddy? from pyjamas.JSONParser import JSONParser parser = JSONParser() dumps = getattr(parser, 'encode') loads = getattr(parser, 'decodeAsObject') JSONDecodeException = None class JSONServiceError(Exception): pass __requestID = 0 __requestIDPrefix = 'ID' __lastRequestID = None def nextRequestID():
from pyjamas.ui.HorizontalPanel import HorizontalPanel from pyjamas.ui.AbsolutePanel import AbsolutePanel from pyjamas.Canvas.GWTCanvas import GWTCanvas import pyjamas.Canvas.Color as Color from pyjamas.dnd import makeDraggable from pyjamas.ui.DragWidget import DragWidget, DragContainer from pyjamas.ui.DropWidget import DropWidget from pyjamas.ui.Panel import Panel from pyjamas.dnd import getTypes from pyjamas.JSONParser import JSONParser from pyjamas import Window import random json = JSONParser() class DNDDemos(VerticalPanel): def __init__(self): VerticalPanel.__init__(self) self.width = '100%' self.setID('content') self.add(TopVerbage()) self.add(NewSchool()) self.add(Delegated()) self.add(ImageDrop()) self.add(DataTransferDemo()) self.add(DragEffects()) self.add(AbsolutePosition()) self.add(MultiTargetDemo())
def decode(self,astring): jparser = JSONParser() return jparser.decode(astring)