This repository was archived by the owner on Apr 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSerialization.php
More file actions
172 lines (154 loc) · 3.75 KB
/
Copy pathSerialization.php
File metadata and controls
172 lines (154 loc) · 3.75 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
// converts an object tree to another that
// can be converted to json
function convertToJson($obj) {
if (is_null($obj)) {
return NULL;
}
else if ($obj instanceof FSharpList)
{
$array = [];
foreach($obj as $value)
{
$array[] = convertToJson($value);
}
return [ '_list' => $array];
}
else if ($obj instanceof Union)
{
$props = [];
foreach(get_object_vars($obj) as $prop => $value)
{
$props[] = convertToJson($value);
}
return ['_case' => $obj->get_Case(), 'fields' => $props ];
}
else if (is_array($obj))
{
$array = [];
foreach($obj as $key => $value)
{
$array[$key] = convertToJson($value);
}
return $array;
}
else if (is_object($obj))
{
$props = [];
$props['_type'] = get_class($obj);
foreach(get_object_vars($obj) as $prop => $value)
{
$props[$prop] = convertToJson($value);
}
return $props;
}
else
return $obj;
}
// converts a json parsed object tree
// to an actual F# object tree
function convertFromJson($json) {
if (is_null($json))
{
return NULL;
}
if (is_object($json))
{
if (property_exists($json, '_case'))
{
$case = $json->_case;
$args=[];
foreach($json->fields as $value)
{
$args[] = convertFromJson($value);
}
return new $case(...$args);
}
if (property_exists($json, '_type'))
{
$type = $json->_type;
$args=[];
foreach(get_object_vars($json) as $prop => $value)
{
if ($prop != '_type')
{
$args[] = convertFromJson($value);
}
}
return new $type(...$args);
}
if (property_exists($json, '_list'))
{
$array = [];
foreach($json->_list as $value)
{
$array[] = convertFromJson($value);
}
return FSharpList::ofArray($array);
}
$props = [];
foreach(get_object_vars($json) as $prop => $value)
{
$props[$prop] = convertFromJson($value);
}
return (object)$props;
}
if (is_array($json))
{
$array = [];
foreach($json as $value)
{
$array[] = convertFromJson($value);
}
return $array;
}
return $json;
}
// converts an object tree to another that
// can be converted to json
function convertToSimpleJson($obj) {
if (is_null($obj)) {
return NULL;
}
else if ($obj instanceof FSharpList)
{
$array = [];
foreach($obj as $value)
{
$array[] = convertToSimpleJson($value);
}
return $array;
}
else if ($obj instanceof FSharpUnion)
{
$vars = get_object_vars($obj);
if (empty($vars))
return $obj->get_FSharpCase();
$props = [$obj->get_FSharpCase()];
foreach($vars as $prop => $value)
{
$props[] = convertToSimpleJson($value);
}
return $props;
}
else if (is_array($obj))
{
$array = [];
foreach($obj as $key => $value)
{
$array[$key] = convertToSimpleJson($value);
}
return $array;
}
else if (is_object($obj))
{
$props = [];
foreach(get_object_vars($obj) as $prop => $value)
{
$props[$prop] = convertToSimpleJson($value);
}
return $props;
}
else
return $obj;
}