Skip to content

Commit 8107ae1

Browse files
authored
Add files via upload
1 parent 8ccf3ed commit 8107ae1

10 files changed

+136
-0
lines changed

src/canada.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "Canada",
3+
"population": 37742154,
4+
"languages": [
5+
"English",
6+
"French"
7+
]
8+
}

src/custom_object_to_json.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import json
2+
3+
4+
class Country:
5+
def __init__(self, name, population, languages):
6+
self.name = name
7+
self.population = population
8+
self.languages = languages
9+
10+
11+
class CountryEncoder(json.JSONEncoder):
12+
def default(self, o):
13+
if isinstance(o, Country):
14+
# JSON object would be a dictionary.
15+
return {
16+
"name": o.name,
17+
"population": o.population,
18+
"languages": o.languages
19+
}
20+
else:
21+
# Base class will raise the TypeError.
22+
return super().default(o)
23+
24+
25+
canada = Country("Canada", 37742154, ["English", "French"])
26+
print(json.dumps(canada, cls=CountryEncoder))

src/file_to_object.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import json
2+
3+
with open('united_states.json') as f:
4+
data = json.load(f)
5+
6+
print(type(data))

src/json_to_custom_class.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import json
2+
3+
4+
class Country:
5+
def __init__(self, name, population, languages):
6+
self.name = name
7+
self.population = population
8+
self.languages = languages
9+
10+
11+
class CountryEncoder(json.JSONEncoder):
12+
def default(self, o):
13+
if isinstance(o, Country):
14+
# JSON object would be a dictionary.
15+
return {
16+
"name": o.name,
17+
"population": o.population,
18+
"languages": o.languages
19+
}
20+
else:
21+
# Base class will raise the TypeError.
22+
return super().default(o)
23+
24+
class CountryDecoder(json.JSONDecoder):
25+
def __init__(self, object_hook=None, *args, **kwargs):
26+
super().__init__(object_hook=self.object_hook, *args, **kwargs)
27+
28+
def object_hook(self, o):
29+
decoded_country = Country(
30+
o.get('name'),
31+
o.get('population'),
32+
o.get('languages'),
33+
)
34+
return decoded_country
35+
36+
with open('canada.json','r') as f:
37+
country_object = json.load(f, cls=CountryDecoder)
38+
39+
print(type(country_object))

src/object_to_json_file.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import json
2+
3+
languages = ["English","French"]
4+
country_dict = {
5+
"name": "Canada",
6+
"population": 37742154,
7+
"languages": languages,
8+
"president": None,
9+
}
10+
11+
12+
with open('countries_exported.json', 'w') as f:
13+
json.dump(country_dict, f, indent=4)

src/object_to_json_str.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import json
2+
3+
languages = ["English","French"]
4+
country_dict = {
5+
"name": "Canada",
6+
"population": 37742154,
7+
"languages": languages,
8+
"president": None,
9+
}
10+
11+
12+
country_string = json.dumps(country_dict)
13+
print(country_string)

src/string_to_bool.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import json
2+
3+
bool_string = 'true'
4+
bool_type = json.loads(bool_string)
5+
print(bool_type)
6+
print(type(bool_type))

src/string_to_dict.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import json
2+
3+
country = '{"name": "United States", "population": 331002651}'
4+
country_dict = json.loads(country)
5+
6+
print(type(country))
7+
print(type(country_dict))
8+
print(country_dict['name'])

src/string_to_list.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import json
2+
3+
countries = '["United States", "Canada"]'
4+
counties_list= json.loads(countries)
5+
6+
print(type(counties_list))
7+
print(counties_list[0])
8+

src/united_states.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "United States",
3+
"population": 331002651,
4+
"capital": "Washington D.C.",
5+
"languages": [
6+
"English",
7+
"Spanish"
8+
]
9+
}

0 commit comments

Comments
 (0)