GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BasicOfPython/IntermediatePythonTutorial/Contents/MyJSON.py at main · vijay113/BasicOfPython

Expand file tree

/

MyJSON.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
89 lines (62 loc) · 1.93 KB
/

MyJSON.py

Copy path

File metadata and controls

89 lines (62 loc) · 1.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import json
person = {"Name":"Ram","Age":20,"IsMale":True}
person_json= json.dumps(person, indent=4,sort_keys=True) #separators=(":","=")
print(person_json)
person1 = json.loads(person_json)
print(person1)
##########################
# Read Json from file
#######################
with open("person.json","r") as file:
person = json.load(file)
print(person)
#########################
# write in file
##############
# with open("person.json","w") as file:
# json.dump(person,file,indent=4)
###############################
# Encode Class into JSON
####################
class User:
def __init__(self,name,age):
self.name = name
self.age = age
user = User("Raam",20)
# Encode using custom method:-
def encode_user(o):
if isinstance(o,User):
return {"Name":o.name, "Age": o.age, o.__class__.__name__: True}
else:
raise TypeError("Object of type User is not JSON serializable.")
user_json = json.dumps(user,default=encode_user)
print(user_json)
#######################
# Encode using Custom Class
from json import JSONEncoder
class UserCustomEncoder(JSONEncoder):
def default(self, o):
if isinstance(o,User):
return {"name":o.name,"age":o.age,o.__class__.__name__:True}
else:
super.default(self,o)
userjson1 = json.dumps(user, cls=UserCustomEncoder)
# OR
userJson2 = UserCustomEncoder().encode(user)
print(userjson1)
print(userJson2)
#########################
# Decode JSON into Python Object
########################
user_python = json.loads(userJson2)
print(type(user_python)) # this is not a object, this is Dictionary
def decode_user(dct):
if User.__name__ in dct:
return User(name=dct["name"],age=dct["age"])
return dct
user_py = json.loads(userJson2,object_hook=decode_user)
print(type(user_py))
print(user_py.name, user_py.age)
########################################
######################################
###########################
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.