mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nprimo
3152d76b75
|
2 years ago | |
---|---|---|
.. | ||
README.md | 2 years ago |
README.md
object_to_json
A lot of information on the web are shared in the JSON format. This exercise will be about transforming object to JSON and vice-versa.
Instructions
You just landed a new job, congrats! Your new task is to build two functions to allow new users to register to your new shiny website.
The new registration information comes as string formatted as JSON. You need to create a file object_to_json.py
that will have the following functions inside:
create_new_user
that will receive adict
and will return a new object of the classUser
provided below. To be valid, the inputdict
must have ausername
key and anemail
key. The newUser
will have the sameusername
andemail
of the inputdict
. If the inputdict
is invalid, the default user will be returned.
class User:
username = 'user'
email = 'something@email.com'
user_to_json
that will receive aUser
and will return the object as a string in JSON format. Be aware of the Python types that can be converted to JSON!
Usage
Here is a possible test.py
to test your functions:
from object_to_json import create_new_user, user_to_json
registration_0 = '{"username": "mario", "email": "mario@me.it"}'
registration_1 = '{"city": "Rome", "country": "Italy"}'
user_0 = create_new_user(registration_0)
user_1 = create_new_user(registration_1)
print(user_to_json(user_0))
print(user_to_json(user_1))
$ python3 test.py
{"username": "mario", "email": "mario@me.it"}
{}
$
Hints
- In python, it is possible to define a class and create a class object with the following syntax:
class MyCalss: # define a new class
attribute1 = 0
attribute2 = 'something else'
my_obj = MyClass() # create an object of MyClass
- It is possible to convert an object to
dict
using the__dict__
casting.
class C:
a = 1
b = 2
my_obj = C()
my_obj_dict = my_obj.__dict__ # my_obj_dict will be a dictionary with the object my_obj values