-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_ironpython_example.py
More file actions
40 lines (30 loc) · 1.08 KB
/
json_ironpython_example.py
File metadata and controls
40 lines (30 loc) · 1.08 KB
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
import clr
clr.AddReferenceToFileAndPath('JSONIronPythonExample.dll')
import JSONIronPythonExample as js # this is the name of the Namespace, NOT the DLL filename.
class SomeClass:
def __init__(self):
self.data = js.ParentDataClass()
def some_method(self):
return self.data.AnInteger * len(self.data.AList)
def some_other_method(self):
return self.data.ACustomType.AString
def store_in_file(self, filename):
with open(filename, 'wb') as f:
f.write(self.data.ToJSONString())
@staticmethod
def read_from_file(filename):
with open(filename, 'rb') as f:
filestr = f.read()
new_c = SomeClass()
new_c.data = js.ParentDataClass.FromJSONString(filestr)
return new_c
sc1 = SomeClass()
sc1.data.AnInteger = 3;
sc1.data.AList.Add('hello') # note that in this case, it's NOT a python list, it's a .NET IList
sc1.data.ACustomType.AString = 'some string'
sc1.store_in_file('class_instance.json')
sc2 = SomeClass.read_from_file('class_instance.json')
for attr in dir(sc2.data):
print '%s: %s' %(attr, getattr(sc2.data, attr))
print sc2.some_method()
print sc2.some_other_method()