def to_json(self): obj=JsonObject() #print("to_json") for x in self.fields: #print(x.name) obj.putString(x.name,str(x.get_value())) #print("to_json end ") return obj
def send_status(self, status, message, json): if json is None: json = JsonObject() # if message.time_out: # vertx.cancel_timer(message.time_out) # message.time_out = None json.putString("status", status) message.reply(json)
def save_session(req,remember,session_dir,**kwargs): session=JsonObject() id=str(uuid.uuid1()) session.putString("id", id) for name,value in kwargs.iteritems(): session.putString(name, value) print("remember = "+remember) cookie = 'mvcx.sessionID=%s' % id.strip() if remember=="1": cookie += ";max-age=864000" req.response.put_header('set-cookie', cookie) session_file=File("%s/%s.json" % (session_dir,id)) print("session_path:"+str(session_file.getAbsolutePath())) session_file.getParentFile().mkdirs() Files.write(str(session),session_file)
def handler(msg): print "Received message" flights = JsonArray() for flight in msg.body: the_flight = JsonObject() #print str(flight) #the_flight.putString("name", flight.getString("callsign")) #the_flight.putString("planeType", flight.getString("equipment") ) the_flight.putString("speed", flight.get("properties").get("direction")) the_flight.putString("alt", flight.get("properties").get("route")) position_array = flight.get("geometry").get("coordinates") #print position_array #There can sometimes be two positions readings but I am not sure what they do so I am just going to take the first #position = position_array[0] the_flight.putNumber("lat", position_array[1]) the_flight.putNumber("lon", position_array[0]) flights.addObject(the_flight) #Sent the array on the EventBus - this is the one the page should subscribe to EventBus.publish('flights.updated', flights) print("published the flights")
def build_route(): router=RouteMatcher() cfg = vertx.config() config=JsonObject(cfg).getObject("router") for route in config.getArray("routes"): controller_description = route.getString("controller") def controller_name(): return str(controller_description).split(".")[0] if not route.getString("get") is None: print("get:"+str(route)) router.get( route.getString("get"),controller_renderer(config.getString("controller-path"),controller_name(),route.getString("view"),renderer(route,controller_description),cfg["session-dir"])) elif not route.getString("post") is None: print("post:"+str(route)) router.post( route.getString("post"),controller_renderer(config.getString("controller-path"),controller_name(),route.getString("view"),renderer(route,controller_description),cfg["session-dir"])) elif not route.getString("static") is None: print("static:"+str(route)) router.get(route.getString("static"),static_files_renderer(route.getString("folder")) ) return router
def _sendResults(self, message, rows, pages, total_rows, session): count = 0 #Set a timeout, if the user doesn't reply within 10 secs, close the cursor first_result = None collection = self.getMandatoryString("collection", message) results = JsonArray() for obj in rows.all(): json = self.to_json(obj,collection) if first_result is None: first_result = json results.addObject(json) count += 1 reply = JsonObject().putArray("rows", results).putNumber("pages", pages).putNumber("total_rows", total_rows) if not first_result is None: reply.putObject("result", first_result) session.close() self.send_ok(message, reply)
def handler(msg): print "Received message" flights = JsonArray() for flight in msg.body: the_flight = JsonObject() #print str(flight) #the_flight.putString("name", flight.getString("callsign")) #the_flight.putString("planeType", flight.getString("equipment") ) the_flight.putString("speed", flight.get("properties").get("direction")) the_flight.putString("alt", flight.get("properties").get("route")) position_array = flight.get("geometry").get("coordinates") #print position_array #There can sometimes be two positions readings but I am not sure what they do so I am just going to take the first #position = position_array[0] the_flight.putNumber("lat", position_array[1]) the_flight.putNumber("lon", position_array[0]) #build the object to persist to mongo forMongo = JsonObject() forMongo.putString("action", "save") forMongo.putString("collection", "buses") forMongo.putObject("document", the_flight) #persist it EventBus.publish('vertx.mongopersistor', forMongo) #add now to the array flights.addObject(the_flight) #Sent the array on the EventBus - this is the one the page should subscribe to EventBus.publish('flights.updated', flights) print("published the flights")
def to_json(self, obj,collection): try: json = JsonObject() for name, type in vars(getattr(self, obj.__class__.__name__)).iteritems(): if str(type.__class__) == "<class 'sqlalchemy.orm.attributes.InstrumentedAttribute'>": column_value = getattr(obj, name) if column_value: column_name = name class__ = column_value.__class__ table = getattr(self, collection) column_type =str(table.__table__.columns[name].type) print ("and type is... "+column_type) if isinstance(class__, DeclarativeMeta): json.putObject(column_name, self.to_json(getattr(obj, name),column_type)) elif column_type == "BOOLEAN": json.putBoolean(column_name, getattr(obj, column_name)) elif column_type in ["<class 'sqlalchemy.orm.collections.InstrumentedList'>"]: print("A LIST") array = JsonArray() for instance in column_value: print(str(instance)) array.addObject(self.to_json(instance,column_type)) json.putArray(column_name, array) print("A LIST COMPLETE") elif column_type == "DATETIME": attr = getattr(obj, column_name).strftime("%Y-%m-%d %H:%M:%S") json.putString(column_name,attr) elif column_type == "TIME": attr = getattr(obj, column_name).strftime("%H:%M:%S") json.putString(column_name,attr) elif column_type == "DATE": attr = getattr(obj, column_name).strftime("%Y-%m-%d") json.putString(column_name,attr) elif column_type in ["NUMERIC","FLOAT"]: attr = getattr(obj, column_name) column = table.__table__.columns[name] if not hasattr(column,"info"): precision = 2 elif not column.info.has_key("precision"): precision = 2 else: precision = column.info["precision"] attr=BigDecimal(str(attr)).setScale(precision, 5) #BigDecimal.ROUND_HALF_DOWN=5 json.putNumber(column_name, attr) elif column_type in ["SMALLINT","BIGINT","INTEGER","NUMERIC"]: attr = getattr(obj, column_name) json.putNumber(column_name, attr) else: json.putString(column_name, getattr(obj, column_name)) print ("JSON:" + str(json)) return json except Exception: exc = str(sys.exc_info()[0])+"\n"+str(sys.exc_info()[1])+"\n"+traceback.format_exc(sys.exc_info()[2]) print exc return None
def handler(msg): print 'Received message' flights = JsonArray() for flight in msg.body: the_flight = JsonObject() the_flight.putString("name", flight.getString("callsign")) the_flight.putString("planeType", flight.getString("equipment") ) position_array = flight.getArray("positions").toArray() #There can sometimes be two positions readings but I am not sure what they do so I am just going to take the first position = position_array[0] the_flight.putNumber("lat", position.get("lat")) the_flight.putNumber("lon", position.get("lon")) the_flight.putNumber("speed", position.get("speedMph")) the_flight.putNumber("alt", position.get("altitudeFt")) flights.addObject(the_flight) #Sent the array on the EventBus - this is the one the page should subscribe to EventBus.publish('flights.updated', flights) print("published the flights")
def offer(self, item, *args): EventBus.publish(self.channel, JsonObject(item))