BridgesUNCC / bridges-python

Python client library for Bridges
http://bridgesuncc.github.io
MIT License
3 stars 4 forks source link

Refactor JSON construction to use built in json library #26

Closed AlecGoncharow closed 5 years ago

AlecGoncharow commented 5 years ago

Not critical, more of a QOL/Readability/Refractor-ability issue.

I needed to use the json library to escape strings from assignment titles, so I went ahead and converted that entire instance of JSON creation to using json.dumps():

        ds = {
            "visual": self.vis_type,
            "title": self.title,
            "description": self.description,
            "coord_system_type": self.coord_system_type,
            "map_overlay": self.map_overlay,
        }
        ds_json = json.dumps(ds)[:-1] + ", "

which does the same work as

 ds_json = self.OPEN_CURLY + self.QUOTE + "visual" + self.QUOTE + self.COLON + self.QUOTE + self.vis_type + self.QUOTE + self.COMMA + self.QUOTE + "title" + self.QUOTE + self.COLON + self.QUOTE + self.title + self.QUOTE + self.COMMA + self.QUOTE + "description" + self.QUOTE + self.COLON + self.QUOTE + self.description + self.QUOTE + self.COMMA + self.QUOTE + "coord_system_type" + self.QUOTE + self.COLON + self.QUOTE + self.coord_system_type + self.QUOTE + self.COMMA + self.QUOTE + "map_overlay" + self.QUOTE + self.COLON + str(self.map_overlay).lower() + self.COMMA

I think this a lot more readable and handles the problems in escaping bad strings and the like. For now it is slicing off the end and adding a comma for compatibility with the rest of the JSON creation process, but I would imagine we could refactor to use it for all the data structures.

krs-world commented 5 years ago

Wow! Cool! Yeah, much better!

On 1/4/19 2:30 AM, Alec Goncharow wrote:

Not critical, more of a QOL/Readability/Refractor-ability issue.

I needed to use the json library to escape strings from assignment titles, so I went ahead and converted that entire instance of JSON creation to using json.dumps():

     ds=  {
         "visual":self.vis_type,
         "title":self.title,
         "description":self.description,
         "coord_system_type":self.coord_system_type,
         "map_overlay":self.map_overlay,
     }
     ds_json=  json.dumps(ds)[:-1]+  ", "

which does the same work as

ds_json= self.OPEN_CURLY + self.QUOTE + "visual" + self.QUOTE + self.COLON + self.QUOTE + self.vis_type+ self.QUOTE + self.COMMA + self.QUOTE + "title" + self.QUOTE + self.COLON + self.QUOTE + self.title+ self.QUOTE + self.COMMA + self.QUOTE + "description" + self.QUOTE + self.COLON + self.QUOTE + self.description+ self.QUOTE + self.COMMA + self.QUOTE + "coord_system_type" + self.QUOTE + self.COLON + self.QUOTE + self.coord_system_type+ self.QUOTE + self.COMMA + self.QUOTE + "map_overlay" + self.QUOTE + self.COLON + str(self.map_overlay).lower()+ self.COMMA

I think this a lot more readable and handles the problems in escaping bad strings and the like. For now it is slicing off the end and adding a comma for compatibility with the rest of the JSON creation process, but I would imagine we could refactor to use it for all the data structures.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/krs-world/bridges-python/issues/26, or mute the thread https://github.com/notifications/unsubscribe-auth/AFZSOPwMEvTeorgpxjyp5qJYzSi7-weaks5u_wMQgaJpZM4ZpQOB.

esaule commented 5 years ago

Indeed, we could improve readability of JSON formation in multiple instances in the different clients.