/
JsonProperties.cfc
208 lines (178 loc) · 6.78 KB
/
JsonProperties.cfc
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<cfcomponent output="false" mixin="model">
<cffunction name="init" output="false" access="public" returntype="any">
<cfset this.version = "1.0,1.1,1.1.8" />
<cfreturn this />
</cffunction>
<cffunction name="jsonProperty" output="false" access="public" returntype="void">
<cfargument name="properties" type="string" required="false" default="" />
<cfargument name="type" type="string" required="false" default="array" hint="The JSON type may be set to `array` or `struct`. The default is `array`. All other values will be ignored." />
<cfargument name="pack" type="boolean" required="false" default="false" hint="If you are storing homoginized query like data set pack to `true`." />
<cfargument name="gzip" type="boolean" required="false" default="false" />
<cfscript>
var loc = {};
if (!StructKeyExists(variables.wheels.class, "jsonProperties"))
variables.wheels.class.jsonProperties = {};
if (!StructKeyExists(variables.wheels.class, "gzip") and arguments.gzip)
variables.wheels.class.gzip = CreateObject("component", "plugins.jsonproperties.gzip.Gzip").init();
if (!StructKeyExists(variables.wheels.class, "jsonh") and arguments.pack)
variables.wheels.class.jsonh = CreateObject("component", "plugins.jsonproperties.jsonh.jsonh").init();
if (StructKeyExists(arguments, "property"))
arguments.properties = arguments.property;
for (loc.property in listToArray(arguments.properties))
variables.wheels.class.jsonProperties[loc.property] = { type = arguments.type, pack = arguments.pack, gzip = arguments.gzip };
afterFind(method="$deserializeJSONProperties");
afterInitialization(method="$deserializeJSONProperties");
afterSave(method="$deserializeJSONProperties");
beforeValidation(method="$serializeJSONProperties");
beforeDelete(method="$serializeJSONProperties");
</cfscript>
</cffunction>
<cffunction name="$serializeJSONProperties" output="false" access="public" returntype="boolean">
<cfscript>
var loc = {};
for (loc.item in variables.wheels.class.jsonProperties)
{
if (!StructKeyExists(this, loc.item))
this[loc.item] = $setDefaultObject(type=variables.wheels.class.jsonProperties[loc.item].type);
if (!IsSimpleValue(this[loc.item]) and !IsBinary(this[loc.item]))
{
if (variables.wheels.class.jsonProperties[loc.item].pack)
{
this[loc.item] = variables.wheels.class.jsonh.serialize(this[loc.item]);
}
else
{
this[loc.item] = SerializeJSON(this[loc.item]);
}
if (variables.wheels.class.jsonProperties[loc.item].gzip)
this[loc.item] = variables.wheels.class.gzip.deflate(this[loc.item]);
}
}
</cfscript>
<cfreturn true />
</cffunction>
<cffunction name="$deserializeJSONProperties" output="false" access="public" returntype="boolean">
<cfscript>
var loc = { connectionArgs = this.$hashedConnectionArgs() };
if (isNew() and (!StructKeyExists(request.wheels.transactions, loc.connectionArgs) or !request.wheels.transactions[loc.connectionArgs]))
return true;
if (IsInstance())
{
for (loc.item in variables.wheels.class.jsonProperties)
{
if (!StructKeyExists(this, loc.item))
this[loc.item] = $setDefaultObject(type=variables.wheels.class.jsonProperties[loc.item].type);
if (variables.wheels.class.jsonProperties[loc.item].gzip and isBinary(this[loc.item]))
this[loc.item] = variables.wheels.class.gzip.inflate(this[loc.item]);
if (variables.wheels.class.jsonProperties[loc.item].pack and IsSimpleValue(this[loc.item]) and Len(this[loc.item]) and isJSON(this[loc.item]))
this[loc.item] = variables.wheels.class.jsonh.deserialize(this[loc.item]);
if (IsSimpleValue(this[loc.item]) and Len(this[loc.item]) and isJSON(this[loc.item]))
this[loc.item] = DeserializeJSON(this[loc.item]);
}
}
</cfscript>
<cfreturn true />
</cffunction>
<cffunction name="$setDefaultObject" output="false" access="public" returntype="any">
<cfargument name="type" type="string" required="true" />
<cfscript>
var returnObject = [];
if (arguments.type == "struct")
returnObject = {};
</cfscript>
<cfreturn returnObject />
</cffunction>
<cffunction name="hasChanged" access="public" output="false" returntype="boolean">
<cfargument name="property" type="string" required="false" default="" />
<cfscript>
var loc = {};
// always return true if $persistedProperties does not exists
if (!StructKeyExists(variables, "$persistedProperties"))
return true;
if (!Len(arguments.property))
{
// they haven't specified a particular property so loop through
// them all
arguments.property = StructKeyList(variables.wheels.class.properties);
}
arguments.property = ListToArray(arguments.property);
loc.iEnd = ArrayLen(arguments.property);
for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
{
loc.key = arguments.property[loc.i];
if (StructKeyExists(this, loc.key))
{
if (!StructKeyExists(variables.$persistedProperties, loc.key))
{
return true;
}
else
{
// hehehehe... convert each datatype to a string
// for easier comparision
loc.a = $convertToString(this[loc.key]);
loc.b = $convertToString(variables.$persistedProperties[loc.key]);
if(Compare(loc.a, loc.b) neq 0)
{
return true;
}
}
}
}
</cfscript>
<cfreturn false>
</cffunction>
<cffunction name="$convertToString" returntype="string" access="public" output="false">
<cfargument name="value" type="Any" required="true">
<cfargument name="type" type="string" required="false" default="">
<cfscript>
var loc = {};
if (!len(arguments.type))
{
if (IsArray(arguments.value))
{
arguments.type = "array";
}
else if (IsStruct(arguments.value))
{
arguments.type = "struct";
}
else if (IsBinary(arguments.value))
{
arguments.type = "binary";
}
else if (IsNumeric(arguments.value))
{
arguments.type = "integer";
}
else if (IsDate(arguments.value))
{
arguments.type = "datetime";
}
}
switch (arguments.type)
{
case "array": case "struct":
arguments.value = serializeJSON(arguments.value);
break;
case "binary":
arguments.value = ToString(arguments.value);
break;
case "float": case "integer":
arguments.value = Val(arguments.value);
break;
case "boolean":
arguments.value = ( arguments.value IS true );
break;
case "datetime":
// createdatetime will throw an error
if(IsDate(arguments.value))
{
arguments.value = CreateDateTime(year(arguments.value), month(arguments.value), day(arguments.value), hour(arguments.value), minute(arguments.value), second(arguments.value));
}
break;
}
</cfscript>
<cfreturn arguments.value>
</cffunction>
</cfcomponent>